'use client' import type { ResourceUserAccessSetting } from '@/models/access-control' import { Avatar } from '@langgenius/dify-ui/avatar' import { Checkbox } from '@langgenius/dify-ui/checkbox' import { cn } from '@langgenius/dify-ui/cn' import { Select, SelectContent, SelectItem, SelectItemIndicator, SelectItemText, SelectTrigger, SelectValue, } from '@langgenius/dify-ui/select' import { memo, useCallback } from 'react' import { useTranslation } from 'react-i18next' import { ACCESS_RULE_TABLE_GRID, DEFAULT_ACCESS_POLICY_ID } from './constants' type PolicyOption = { id: string name: string } type UserAccessPolicyRowProps = { setting: ResourceUserAccessSetting policyOptions: PolicyOption[] disabled: boolean membershipChangesDisabled?: boolean selectionDisabled?: boolean isProtected?: boolean isMaintainer?: boolean selected?: boolean className?: string onSelectedChange?: (accountId: string, selected: boolean) => void onChange?: (accountId: string, accessPolicyIds: string[]) => void onRemove?: (accountId: string, accessPolicyId: string) => void } function UserAccessPolicyRow({ setting, policyOptions, disabled, membershipChangesDisabled = false, selectionDisabled = false, isProtected = false, isMaintainer = false, selected = false, className, onSelectedChange, onChange, onRemove, }: UserAccessPolicyRowProps) { const { t } = useTranslation() const accountId = setting.account.account_id const selectedPolicy = setting.access_policies[0] const selectedPolicyId = selectedPolicy?.id ?? DEFAULT_ACCESS_POLICY_ID const isPolicySelectDisabled = disabled || isProtected || !onChange const isRemoveDisabled = disabled || membershipChangesDisabled || isProtected || !onRemove const isSelectionDisabled = disabled || membershipChangesDisabled || selectionDisabled || isProtected || !onSelectedChange const defaultAccessPolicyName = t(($) => $['accessRule.defaultPermission'], { ns: 'permission' }) const accountEmail = setting.account.email || setting.account.account_name const isWorkspaceOwner = setting.roles.some((role) => role.role_tag === 'owner') const handlePolicyChange = useCallback( (nextPolicyId: string | null) => { if (isPolicySelectDisabled || !nextPolicyId || nextPolicyId === selectedPolicyId) return onChange?.(accountId, [nextPolicyId]) }, [accountId, isPolicySelectDisabled, onChange, selectedPolicyId], ) const handleRemove = useCallback(() => { if (isRemoveDisabled) return onRemove?.(accountId, selectedPolicyId) }, [accountId, isRemoveDisabled, onRemove, selectedPolicyId]) return ( onSelectedChange?.(accountId, checked)} />
{setting.account.account_name} {isMaintainer && ( {t(($) => $['accessRule.maintainer'], { ns: 'permission' })} )} {isWorkspaceOwner && ( {t(($) => $['accessRule.workspaceOwner'], { ns: 'permission' })} )}

{accountEmail}

) } export default memo(UserAccessPolicyRow)