import { useCallback, useMemo } from 'react'; import type { MutableRefObject } from 'react'; type Identifier = string | number; interface UseWorkspaceDeskPropsArgs { viewDocuments: any[]; inspectDocumentForDesk: (doc: any) => void; handleEntryPointer: (params: { rowKey?: string | null; id?: Identifier | null; type?: string; event?: any }) => void; selectedEntries: Array; selectionAnchorRef: MutableRefObject; applySelection: (rowKeys: Array, options?: { anchor?: Identifier | string | null; interactedKeys?: Array }) => void; resolveDocumentRowKey: (id?: Identifier | null) => string | null; showingSearchResults: boolean; searchQuery: string; activeTagFilters: Array; activeCorrespondentFilters: Array; selectedFolder: Identifier | 'root' | null; promoteSelectionOrder: () => void; handleDocumentTagDrop: (docId: Identifier, tagId: Identifier) => Promise | void; ensureAssetUrl: (docId: Identifier, asset: any, options?: Record) => Promise | null; getDocumentAsset: (doc: any, type: string) => any; currentTenantId: Identifier | null; documentLinks: Map | null; ensureDownloadUrl?: (docId: Identifier, options?: { force?: boolean }) => Promise; } const useWorkspaceDeskProps = ({ viewDocuments, inspectDocumentForDesk, handleEntryPointer, selectedEntries, selectionAnchorRef, applySelection, resolveDocumentRowKey, showingSearchResults, searchQuery, activeTagFilters, activeCorrespondentFilters, selectedFolder, promoteSelectionOrder, handleDocumentTagDrop, ensureAssetUrl, getDocumentAsset, currentTenantId, documentLinks, ensureDownloadUrl, }: UseWorkspaceDeskPropsArgs) => { const handleDeskDocumentStackSelect = useCallback( (docIds: Array) => { if (!Array.isArray(docIds) || docIds.length === 0) { return; } const rowKeys = docIds .map((id) => resolveDocumentRowKey(id as Identifier)) .filter((value): value is string => typeof value === 'string'); if (!rowKeys.length) { return; } const nextKeys = [...selectedEntries]; rowKeys.forEach((key) => { if (!nextKeys.includes(key)) { nextKeys.push(key); } }); const anchor = (rowKeys[0] || selectionAnchorRef.current || nextKeys[nextKeys.length - 1]) as Identifier | string | null; applySelection(nextKeys, { anchor, interactedKeys: rowKeys, }); }, [applySelection, resolveDocumentRowKey, selectedEntries, selectionAnchorRef], ); const deskViewId = useMemo(() => { if (showingSearchResults) { const trimmedQuery = searchQuery.trim(); const tagsKey = [...activeTagFilters].sort().join(','); const correspondentsKey = [...activeCorrespondentFilters].sort().join(','); return `search:${trimmedQuery}|tags:${tagsKey}|corr:${correspondentsKey}`; } const folderKey = selectedFolder && selectedFolder !== '' ? selectedFolder : 'root'; return `folder:${folderKey}`; }, [ showingSearchResults, searchQuery, activeTagFilters, activeCorrespondentFilters, selectedFolder, ]); const deskWorkspaceProps = useMemo( () => ({ entries: viewDocuments, onDocumentActivate: inspectDocumentForDesk, onDocumentClick: handleEntryPointer, onDocumentStackSelect: handleDeskDocumentStackSelect, onPromoteSelection: promoteSelectionOrder, onDocumentTagDrop: handleDocumentTagDrop, ensureAssetUrl, getDocumentAsset, activeTagFilters, tenantId: currentTenantId, viewId: deskViewId, documentLinks, ensureDownloadUrl, }), [ viewDocuments, inspectDocumentForDesk, handleEntryPointer, handleDeskDocumentStackSelect, promoteSelectionOrder, handleDocumentTagDrop, ensureAssetUrl, getDocumentAsset, activeTagFilters, currentTenantId, deskViewId, documentLinks, ensureDownloadUrl, ], ); return deskWorkspaceProps; }; export default useWorkspaceDeskProps;