import type { AssetInfo } from '@shumai/dtos' import type { CollectionInfo } from '@shumai/dtos' import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos' import type { SearchCondition, SearchSort } from '@shumai/dtos' import { client } from '@/ui/api/client' import { useMutation, useQuery } from '@tanstack/react-query' import type { InferRequestType, InferResponseType } from 'hono/client' import { useState } from 'react' import { FieldsManager } from '../fields-manager' import { FolderTree } from '../folder-tree' import { ManageFieldsDialog } from '../manage-fields-dialog' import { MembersDialog } from '../members-dialog' import { FilterPanel } from '../search/filter-panel' import { SearchFilterDialog } from '../search/search-filter-dialog' import { SortControl } from '../search/sort-control' import { Avatar, AvatarFallback } from '../ui/avatar' import { Button } from '../ui/button' import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover' import { Separator } from '../ui/separator' type FileBrowserToolbarProps = { teamId: string projectId: string assetId: string fields: MetadataFieldInfo[] filterConditions: SearchCondition[] onFilterChange: (conditions: SearchCondition[]) => void sort?: SearchSort onSortChange: (sort?: SearchSort) => void isRecentlyDeleted?: boolean collection?: CollectionInfo // eslint-disable-next-line @typescript-eslint/no-explicit-any onUpdateCollection?: (updates: { name?: string; filter?: any }) => void rootFolderId?: string } export function FileBrowserToolbar({ teamId, projectId, assetId, fields, filterConditions, onFilterChange, sort, onSortChange, isRecentlyDeleted, collection, onUpdateCollection, rootFolderId, }: FileBrowserToolbarProps) { const [popoverOpen, setPopoverOpen] = useState(false) const [searchDialogOpen, setSearchDialogOpen] = useState(false) const [manageDialogOpen, setManageDialogOpen] = useState(false) const [isMembersDialogOpen, setIsMembersDialogOpen] = useState(false) const [isFolderSelectorOpen, setIsFolderSelectorOpen] = useState(false) const activeFiltersCount = filterConditions.length const isCollection = !!collection const { data: folderInfo } = useQuery({ queryKey: ['folders', assetId], queryFn: async () => { const res = await client.api.folders[':folderId'].$get({ param: { folderId: assetId }, }) if (!res.ok) throw new Error('failed to fetch folder') return (await res.json()) as unknown as AssetInfo }, enabled: isCollection && !!assetId, }) const { data: members } = useQuery({ queryKey: ['projects', projectId, 'members'], queryFn: async () => { const res = await client.api.projects[':projectId'].members.$get({ param: { projectId: projectId }, query: {}, }) if (!res.ok) throw new Error('Failed to fetch members') return await res.json() }, }) const { data: teamInfo } = useQuery({ queryKey: ['projects', projectId, 'team'], queryFn: async () => { const res = await client.api.projects[':projectId'].team.$get({ param: { projectId: projectId }, }) if (!res.ok) throw new Error('Failed to fetch team info') return await res.json() }, }) const { data: me } = useQuery({ queryKey: ['teams', teamInfo?.teamId, 'me'], queryFn: async () => { const res = await client.api.teams[':teamId'].me.$get({ param: { teamId: teamInfo?.teamId || '' }, }) if (!res.ok) throw new Error('Failed to fetch me') return await res.json() }, enabled: !!teamInfo?.teamId, }) const $inviteProject = client.api.projects[':projectId'].invite.$post const inviteMutation = useMutation< InferResponseType, Error, InferRequestType >({ mutationFn: async (args) => { const res = await $inviteProject(args) if (!res.ok) { throw new Error('Failed to invite') } return await res.json() }, }) const handleManageFields = () => { setPopoverOpen(false) setManageDialogOpen(true) } const handleInvite = async (role: 'editor' | 'reviewer') => { const res = await inviteMutation.mutateAsync({ param: { projectId: projectId }, json: { role }, }) return res.code } const getInitials = (name?: string) => { if (!name) return '??' return name .split(' ') .map((n) => n[0]) .slice(0, 2) .join('') .toUpperCase() } return (
setPopoverOpen(false)} />
{isCollection ? ( <>
Select Source Folder
{ onUpdateCollection?.({ filter: { ...collection.filter, sourceFolderId: folder.id, }, }) setIsFolderSelectorOpen(false) }} selectedFolderId={collection.filter.sourceFolderId} hideCollections hideShares />
{ onFilterChange(newConditions) onUpdateCollection?.({ filter: { ...collection.filter, searchFilter: { ...collection.filter.searchFilter, conditions: newConditions, }, }, }) }} excludeFields={[]} hidePrefix />
) : ( )}
{!isRecentlyDeleted && (
setIsMembersDialogOpen(true)} > {members?.slice(0, 3).map((member) => ( {getInitials(member.name)} ))} {members && members.length > 3 && (
+{members.length - 3}
)}
)}
) }