import { useTranslation } from 'react-i18next' import cn from 'classnames' import React, { useMemo, useState } from 'react' import { useDebounceFn } from 'ahooks' import { RiArrowDownSLine } from '@remixicon/react' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger, } from '@/app/components/base/portal-to-follow-elem' import Avatar from '@/app/components/base/avatar' import Input from '@/app/components/base/input' import { Check } from '@/app/components/base/icons/src/vender/line/general' import { Users01, UsersPlus } from '@/app/components/base/icons/src/vender/solid/users' import type { DatasetPermission } from '@/models/datasets' import { useAppContext } from '@/context/app-context' import type { Member } from '@/models/common' export type RoleSelectorProps = { disabled?: boolean permission?: DatasetPermission value: string[] memberList: Member[] onChange: (permission?: DatasetPermission) => void onMemberSelect: (v: string[]) => void } const PermissionSelector = ({ disabled, permission, value, memberList, onChange, onMemberSelect }: RoleSelectorProps) => { const { t } = useTranslation() const { userProfile } = useAppContext() const [open, setOpen] = useState(false) const [keywords, setKeywords] = useState('') const [searchKeywords, setSearchKeywords] = useState('') const { run: handleSearch } = useDebounceFn(() => { setSearchKeywords(keywords) }, { wait: 500 }) const handleKeywordsChange = (value: string) => { setKeywords(value) handleSearch() } const selectMember = (member: Member) => { if (value.includes(member.id)) onMemberSelect(value.filter(v => v !== member.id)) else onMemberSelect([...value, member.id]) } const selectedMembers = useMemo(() => { return [ userProfile, ...memberList.filter(member => member.id !== userProfile.id).filter(member => value.includes(member.id)), ].map(member => member.name).join(', ') }, [userProfile, value, memberList]) const showMe = useMemo(() => { return userProfile.name.includes(searchKeywords) || userProfile.email.includes(searchKeywords) }, [searchKeywords, userProfile]) const filteredMemberList = useMemo(() => { return memberList.filter(member => (member.name.includes(searchKeywords) || member.email.includes(searchKeywords)) && member.id !== userProfile.id && ['owner', 'admin', 'editor', 'dataset_operator'].includes(member.role)) }, [memberList, searchKeywords, userProfile]) return (
!disabled && setOpen(v => !v)} className='block' > {permission === 'only_me' && (
{t('datasetSettings.form.permissionsOnlyMe')}
{!disabled && }
)} {permission === 'all_team_members' && (
{t('datasetSettings.form.permissionsAllMember')}
{!disabled && }
)} {permission === 'partial_members' && (
{selectedMembers}
{!disabled && }
)}
{ onChange('only_me') setOpen(false) }}>
{t('datasetSettings.form.permissionsOnlyMe')}
{permission === 'only_me' && }
{ onChange('all_team_members') setOpen(false) }}>
{t('datasetSettings.form.permissionsAllMember')}
{permission === 'all_team_members' && }
{ onChange('partial_members') onMemberSelect([userProfile.id]) }}>
{t('datasetSettings.form.permissionsInvitedMembers')}
{permission === 'partial_members' && }
{permission === 'partial_members' && (
handleKeywordsChange(e.target.value)} onClear={() => handleKeywordsChange('')} />
{showMe && (
{userProfile.name} {t('datasetSettings.form.me')}
{userProfile.email}
)} {filteredMemberList.map(member => (
selectMember(member)}>
{member.name}
{member.email}
{value.includes(member.id) && }
))}
)}
) } export default PermissionSelector