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.7 KB
Raw Blame
'use client'

import type { Role } from '@/models/access-control'
import { Field, FieldError } from '@langgenius/dify-ui/field'
import {
  Select,
  SelectItem,
  SelectItemIndicator,
  SelectItemText,
  SelectLabel,
  SelectList,
  SelectPopup,
  SelectPortal,
  SelectPositioner,
  SelectTrigger,
  SelectValue,
} from '@langgenius/dify-ui/select'
import { useInfiniteQuery } from '@tanstack/react-query'
import { useCallback, useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useLocale } from '#i18n'
import { getAccessControlTemplateLanguage } from '@/i18n/language'
import { workspaceRoleListInfiniteOptions } from '../../workspace-role-list-query'

type RoleSelectorProps = {
  hasServerError?: boolean
  disabled?: boolean
}

const PAGE_SIZE = 20
const LEGACY_ROLE_DESCRIPTION_KEY_MAP = {
  admin: 'members.adminTip',
  editor: 'members.editorTip',
  normal: 'members.normalTip',
  dataset_operator: 'members.datasetOperatorTip',
} as const

type LegacyRoleKey = keyof typeof LEGACY_ROLE_DESCRIPTION_KEY_MAP

function normalizeLegacyRoleKey(value: string) {
  return value.trim().toLowerCase()
}

function isLegacyRoleKey(value: string): value is LegacyRoleKey {
  return Object.prototype.hasOwnProperty.call(LEGACY_ROLE_DESCRIPTION_KEY_MAP, value)
}

function getLegacyRoleDescriptionKey(role: Role) {
  const candidateKeys = [normalizeLegacyRoleKey(role.name), normalizeLegacyRoleKey(role.id)]
  return candidateKeys.find(isLegacyRoleKey)
}

export function RoleSelector({ hasServerError = false, disabled = false }: RoleSelectorProps) {
  const { t } = useTranslation(['common', 'permission', 'workspaceMembers'])
  const locale = useLocale()
  const [open, setOpen] = useState(false)
  const listRef = useRef<HTMLDivElement>(null)
  const anchorRef = useRef<HTMLDivElement>(null)
  const [observerReadyKey, setObserverReadyKey] = useState(0)
  const language = getAccessControlTemplateLanguage(locale)

  const {
    data: rolesData,
    isLoading: rolesLoading,
    error: rolesError,
    hasNextPage,
    isFetchingNextPage,
    fetchNextPage,
  } = useInfiniteQuery(
    workspaceRoleListInfiniteOptions({
      page: 1,
      limit: PAGE_SIZE,
      language,
    }),
  )

  useEffect(() => {
    const hasMore = hasNextPage ?? true
    let observer: IntersectionObserver | undefined

    if (rolesError || !open) return

    if (anchorRef.current && listRef.current) {
      const listHeight = listRef.current.clientHeight
      const dynamicMargin = Math.max(100, Math.min(listHeight * 0.2, 200))

      observer = new IntersectionObserver(
        (entries) => {
          if (
            entries[0]!.isIntersecting &&
            !rolesLoading &&
            !isFetchingNextPage &&
            !rolesError &&
            hasMore
          )
            fetchNextPage()
        },
        {
          root: listRef.current,
          rootMargin: `${dynamicMargin}px`,
        },
      )
      observer.observe(anchorRef.current)
    }

    return () => observer?.disconnect()
  }, [
    rolesLoading,
    isFetchingNextPage,
    fetchNextPage,
    rolesError,
    hasNextPage,
    open,
    observerReadyKey,
  ])

  const roles = rolesData?.pages.flatMap((page) => page.data) ?? []

  const setListElement = useCallback((node: HTMLDivElement | null) => {
    listRef.current = node
    setObserverReadyKey((key) => key + 1)
  }, [])

  const setAnchorElement = useCallback((node: HTMLDivElement | null) => {
    anchorRef.current = node
    setObserverReadyKey((key) => key + 1)
  }, [])

  const getRoleDescription = (role: Role) => {
    if (role.description) return role.description

    switch (getLegacyRoleDescriptionKey(role)) {
      case 'admin':
        return t(($) => $['members.adminTip'], { ns: 'workspaceMembers' })
      case 'editor':
        return t(($) => $['members.editorTip'], { ns: 'workspaceMembers' })
      case 'normal':
        return t(($) => $['members.normalTip'], { ns: 'workspaceMembers' })
      case 'dataset_operator':
        return t(($) => $['members.datasetOperatorTip'], { ns: 'workspaceMembers' })
    }

    return t(($) => $['role.noDescription'], { ns: 'permission' })
  }

  return (
    <Field name="role">
      <Select<Role>
        name="role"
        required
        disabled={disabled}
        open={open}
        onOpenChange={setOpen}
        itemToStringLabel={(role) => role.name}
        itemToStringValue={(role) => role.id}
        isItemEqualToValue={(role, selectedRole) => role.id === selectedRole.id}
      >
        <SelectLabel>{t(($) => $['members.role'], { ns: 'workspaceMembers' })}</SelectLabel>
        <SelectTrigger>
          <SelectValue
            placeholder={t(($) => $['members.selectRole'], { ns: 'workspaceMembers' })}
          />
        </SelectTrigger>
        <SelectPortal>
          <SelectPositioner>
            <SelectPopup>
              <SelectList className="max-h-70" ref={setListElement}>
                {rolesLoading ? (
                  <div className="px-3 py-6 text-center system-sm-regular text-text-tertiary">
                    {t(($) => $.loading, { ns: 'common' })}
                  </div>
                ) : rolesError && roles.length === 0 ? (
                  <div className="px-3 py-6 text-center system-sm-regular text-text-destructive-secondary">
                    {t(($) => $['dynamicSelect.error'], { ns: 'common' })}
                  </div>
                ) : roles.length === 0 ? (
                  <div className="px-3 py-6 text-center system-sm-regular text-text-tertiary">
                    {t(($) => $['dynamicSelect.noData'], { ns: 'common' })}
                  </div>
                ) : (
                  <>
                    {roles.map((role) => (
                      <SelectItem key={role.id} value={role} className="h-auto items-start py-2">
                        <SelectItemText className="grid gap-0.5">
                          <span className="truncate text-sm leading-5 text-text-secondary">
                            {role.name}
                          </span>
                          <span className="line-clamp-2 text-xs leading-4.5 text-text-tertiary">
                            {getRoleDescription(role)}
                          </span>
                        </SelectItemText>
                        <SelectItemIndicator className="mt-0.5" />
                      </SelectItem>
                    ))}
                    <div ref={setAnchorElement} className="h-0" />
                  </>
                )}
              </SelectList>
            </SelectPopup>
          </SelectPositioner>
        </SelectPortal>
      </Select>
      {hasServerError ? (
        <FieldError />
      ) : (
        <FieldError match="valueMissing">
          {t(($) => $['members.selectRole'], { ns: 'workspaceMembers' })}
        </FieldError>
      )}
    </Field>
  )
}