Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

6.1 KB
Raw Blame
'use client'

import { Avatar, AvatarFallback, AvatarImage } from '@langgenius/dify-ui/avatar'
import { Button } from '@langgenius/dify-ui/button'
import { cn } from '@langgenius/dify-ui/cn'
import {
  Menu,
  MenuContent,
  MenuEmpty,
  MenuFilterProvider,
  MenuInput,
  MenuInputGroup,
  MenuItem,
  MenuList,
  MenuTrigger,
} from '@langgenius/dify-ui/menu'
import { useQuery } from '@tanstack/react-query'
import { memo } from 'react'
import { useTranslation } from 'react-i18next'
import { LoadingPlaceholder } from '@/app/components/base/loading-placeholder'
import { consoleQuery } from '@/service/console'
import { getAvatarInitial } from '@/utils/avatar'
import { DEFAULT_ACCESS_POLICY_ID } from './constants'

type AddAccessSubjectMenuProps = {
  disabled?: boolean
  existingAccountIds?: string[]
  updatingAccountId: string | null
  onAddAccessSubject: (accountId: string, accessPolicyIds: string[]) => void
}

type AddAccessSubjectMenuContentProps = {
  existingAccountIds: string[]
  updatingAccountId: string | null
  onAddAccessSubject: (accountId: string, accessPolicyIds: string[]) => void
}

// Mounted only while the menu is open, so the member list loads on demand. A failed load has no
// retry item: closing and reopening the menu mounts this again, which loads again.
function AddAccessSubjectMenuContent({
  existingAccountIds,
  updatingAccountId,
  onAddAccessSubject,
}: AddAccessSubjectMenuContentProps) {
  const { t } = useTranslation(['common', 'permission'])
  const {
    data: membersData,
    isPending: isLoading,
    error,
  } = useQuery(consoleQuery.workspaces.current.members.get.queryOptions({ input: {} }))
  const existingAccountIdSet = new Set(existingAccountIds)
  const members = membersData?.accounts ?? []

  const addLabel = t(($) => $['operation.add'], { ns: 'common' })
  const addedLabel = t(($) => $['operation.added'], { ns: 'common' })

  return (
    <>
      <MenuInputGroup>
        <span
          aria-hidden="true"
          className="i-ri-search-line size-4 shrink-0 text-components-input-text-placeholder"
        />
        <MenuInput
          aria-label={t(($) => $['operation.search'], { ns: 'common' })}
          placeholder={t(($) => $['placeholder.search'], { ns: 'common' })}
        />
      </MenuInputGroup>
      {error && (
        <div role="alert" className="px-3 py-2 system-sm-regular text-text-tertiary">
          {t(($) => $['api.actionFailed'], { ns: 'common' })}
        </div>
      )}
      {isLoading && <LoadingPlaceholder className="h-20 p-1" />}
      {!isLoading && !error && (
        <MenuEmpty>{t(($) => $['accessRule.noAvailableMembers'], { ns: 'permission' })}</MenuEmpty>
      )}
      {/* ARIA requires a `menu` that has no items yet because they are loading to be marked busy. */}
      <MenuList aria-busy={isLoading || undefined}>
        {members.map((member) => {
          const isAdded = existingAccountIdSet.has(member.id)
          const isUpdating = updatingAccountId === member.id
          const memberName = member.name || member.email

          return (
            <MenuItem
              key={member.id}
              // The query matches the name and email, not the row's action text.
              label={`${member.name || ''} ${member.email || ''}`}
              disabled={isAdded || updatingAccountId !== null}
              closeOnClick={false}
              // Only an added member reads as spent; a pending add blocks the others without dimming them.
              className={cn(
                'h-auto min-h-10 gap-2 py-1 pr-3 pl-2',
                isAdded ? 'data-disabled:opacity-50' : 'data-disabled:opacity-100',
              )}
              onClick={() => onAddAccessSubject(member.id, [DEFAULT_ACCESS_POLICY_ID])}
            >
              <Avatar size="sm" aria-hidden="true">
                <AvatarFallback className="bg-components-icon-bg-blue-solid">
                  {getAvatarInitial(memberName)}
                </AvatarFallback>
                <AvatarImage src={(member.avatar_url ?? member.avatar) || undefined} alt="" />
              </Avatar>
              <span className="min-w-0 flex-1">
                <span className="block truncate system-sm-medium text-text-secondary">
                  {memberName}
                </span>
                <span className="block truncate system-xs-regular text-text-tertiary">
                  {member.email}
                </span>
              </span>
              {isAdded ? (
                <span className="shrink-0 system-xs-regular text-text-tertiary">{addedLabel}</span>
              ) : isUpdating ? (
                <span className="i-ri-loader-2-line size-3.5 shrink-0 animate-spin" aria-hidden />
              ) : (
                <span className="shrink-0 system-xs-medium text-text-accent">{`+ ${addLabel}`}</span>
              )}
            </MenuItem>
          )
        })}
      </MenuList>
    </>
  )
}

function AddAccessSubjectMenu({
  disabled = false,
  existingAccountIds,
  updatingAccountId,
  onAddAccessSubject,
}: AddAccessSubjectMenuProps) {
  const { t } = useTranslation(['common', 'permission'])
  const isLoadingExisting = existingAccountIds === undefined

  return (
    <MenuFilterProvider>
      <Menu>
        <MenuTrigger
          disabled={disabled}
          render={
            <Button
              variant="primary"
              size="medium"
              disabled={disabled}
              loading={!disabled && isLoadingExisting}
            />
          }
        >
          <span className="i-ri-add-line size-3.5" aria-hidden />
          <span>{t(($) => $['operation.add'], { ns: 'common' })}</span>
        </MenuTrigger>
        <MenuContent
          sideOffset={8}
          aria-label={t(($) => $['accessRule.addMembersTitle'], { ns: 'permission' })}
          className="w-86 max-w-[calc(100vw-32px)]"
        >
          {existingAccountIds !== undefined && (
            <AddAccessSubjectMenuContent
              existingAccountIds={existingAccountIds}
              updatingAccountId={updatingAccountId}
              onAddAccessSubject={onAddAccessSubject}
            />
          )}
        </MenuContent>
      </Menu>
    </MenuFilterProvider>
  )
}

export default memo(AddAccessSubjectMenu)