137 lines
4.2 KiB
TypeScript
137 lines
4.2 KiB
TypeScript
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<string | number>;
|
|
selectionAnchorRef: MutableRefObject<Identifier | string | null>;
|
|
applySelection: (rowKeys: Array<string | number>, options?: { anchor?: Identifier | string | null; interactedKeys?: Array<string | number> }) => void;
|
|
resolveDocumentRowKey: (id?: Identifier | null) => string | null;
|
|
showingSearchResults: boolean;
|
|
searchQuery: string;
|
|
activeTagFilters: Array<string | number>;
|
|
activeCorrespondentFilters: Array<string | number>;
|
|
selectedFolder: Identifier | 'root' | null;
|
|
promoteSelectionOrder: () => void;
|
|
handleDocumentTagDrop: (docId: Identifier, tagId: Identifier) => Promise<void> | void;
|
|
ensureAssetUrl: (docId: Identifier, asset: any, options?: Record<string, unknown>) => Promise<any> | null;
|
|
getDocumentAsset: (doc: any, type: string) => any;
|
|
currentTenantId: Identifier | null;
|
|
documentLinks: Map<Identifier, unknown> | null;
|
|
ensureDownloadUrl?: (docId: Identifier, options?: { force?: boolean }) => Promise<unknown>;
|
|
}
|
|
|
|
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<Identifier | string>) => {
|
|
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;
|