simplify
This commit is contained in:
@@ -3,7 +3,6 @@ import type { MutableRefObject } from 'react';
|
||||
import { resolveDocumentAssetUrl } from '../asset_manager';
|
||||
import { useDetailPanel } from '../app/useDetailPanel';
|
||||
import { DEFAULT_FOLDER_NAME } from '../app/workspaceUtils';
|
||||
import { getEntryId, isDocumentEntry } from '../app/entryKey';
|
||||
import type { DocumentInfoPanelProps } from '../documents/DocumentInfoPanel';
|
||||
import type { EnsureAssetUrl, GetDocumentAsset } from '../utils/ocr';
|
||||
import type { Identifier } from '../types/identifiers';
|
||||
@@ -17,12 +16,10 @@ interface FolderNode {
|
||||
|
||||
interface UseDetailWorkspaceArgs {
|
||||
documents: Document[];
|
||||
selectionOrder: string[];
|
||||
selectedDocumentIds: Identifier[];
|
||||
documentLookup: Map<Identifier, Document>;
|
||||
folderNodes: Map<Identifier | 'root', FolderNode>;
|
||||
ensureFolderData: (folderId: Identifier | 'root', options?: { force?: boolean; includeDocuments?: boolean }) => Promise<void>;
|
||||
detailPanelControlRef: MutableRefObject<{ open?: (args?: { documentIds?: Identifier[] }) => void; close?: () => void } | null>;
|
||||
detailPanelControlRef: MutableRefObject<{ open?: (documentId: Identifier) => void; close?: () => void } | null>;
|
||||
detailFolderFetchRef: MutableRefObject<Set<Identifier | 'root'>>;
|
||||
previewDocumentId?: Identifier | null;
|
||||
activePreviewId?: Identifier | null;
|
||||
@@ -56,8 +53,6 @@ interface UseDetailWorkspaceResult {
|
||||
|
||||
const useDetailWorkspace = ({
|
||||
documents,
|
||||
selectionOrder,
|
||||
selectedDocumentIds,
|
||||
documentLookup,
|
||||
folderNodes,
|
||||
ensureFolderData,
|
||||
@@ -79,37 +74,6 @@ const useDetailWorkspace = ({
|
||||
tags,
|
||||
tagLookupById,
|
||||
}: UseDetailWorkspaceArgs): UseDetailWorkspaceResult => {
|
||||
const orderedSelectedDocuments = useMemo(() => {
|
||||
const ordered = [];
|
||||
const seen = new Set();
|
||||
|
||||
const pushDoc = (doc) => {
|
||||
if (doc?.id && !seen.has(doc.id)) {
|
||||
ordered.push(doc);
|
||||
seen.add(doc.id);
|
||||
}
|
||||
};
|
||||
|
||||
selectionOrder.forEach((key) => {
|
||||
if (!isDocumentEntry(key)) {
|
||||
return;
|
||||
}
|
||||
const docId = getEntryId(key);
|
||||
const doc = documentLookup.get(docId) || null;
|
||||
pushDoc(doc);
|
||||
});
|
||||
|
||||
selectedDocumentIds.forEach((docId) => {
|
||||
if (seen.has(docId)) {
|
||||
return;
|
||||
}
|
||||
const doc = documentLookup.get(docId) || null;
|
||||
pushDoc(doc);
|
||||
});
|
||||
|
||||
return ordered;
|
||||
}, [selectionOrder, documentLookup, selectedDocumentIds]);
|
||||
|
||||
const {
|
||||
detailPanelOpen,
|
||||
detailPanelDocument,
|
||||
@@ -117,7 +81,6 @@ const useDetailWorkspace = ({
|
||||
closeDetailPanel,
|
||||
} = useDetailPanel({
|
||||
documentLookup,
|
||||
orderedSelectedDocuments,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
@@ -127,52 +90,46 @@ const useDetailWorkspace = ({
|
||||
};
|
||||
}, [detailPanelControlRef, openDetailPanel, closeDetailPanel]);
|
||||
|
||||
// Prefetch folder ancestors for breadcrumb display
|
||||
useEffect(() => {
|
||||
if (!orderedSelectedDocuments.length) {
|
||||
const folderId = detailPanelDocument?.folder_id;
|
||||
if (!folderId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const visited = new Set();
|
||||
let currentId = folderId;
|
||||
let guard = 0;
|
||||
|
||||
orderedSelectedDocuments.forEach((doc) => {
|
||||
const folderId = doc?.folder_id;
|
||||
if (!folderId) {
|
||||
return;
|
||||
while (currentId && currentId !== 'root' && guard < 32) {
|
||||
guard += 1;
|
||||
if (visited.has(currentId)) {
|
||||
break;
|
||||
}
|
||||
visited.add(currentId);
|
||||
|
||||
const node = folderNodes.get(currentId);
|
||||
if (!node) {
|
||||
if (!detailFolderFetchRef.current.has(currentId)) {
|
||||
detailFolderFetchRef.current.add(currentId);
|
||||
ensureFolderData(currentId, { force: false, includeDocuments: false })
|
||||
.catch((error) => {
|
||||
console.warn('Failed to preload folder metadata for detail path', currentId, error);
|
||||
})
|
||||
.finally(() => {
|
||||
detailFolderFetchRef.current.delete(currentId);
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
let currentId = folderId;
|
||||
let guard = 0;
|
||||
|
||||
while (currentId && currentId !== 'root' && guard < 32) {
|
||||
guard += 1;
|
||||
if (visited.has(currentId)) {
|
||||
break;
|
||||
}
|
||||
visited.add(currentId);
|
||||
|
||||
const node = folderNodes.get(currentId);
|
||||
if (!node) {
|
||||
if (!detailFolderFetchRef.current.has(currentId)) {
|
||||
detailFolderFetchRef.current.add(currentId);
|
||||
ensureFolderData(currentId, { force: false, includeDocuments: false })
|
||||
.catch((error) => {
|
||||
console.warn('Failed to preload folder metadata for detail path', currentId, error);
|
||||
})
|
||||
.finally(() => {
|
||||
detailFolderFetchRef.current.delete(currentId);
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
const parentId = node.parentId ?? 'root';
|
||||
if (!parentId || parentId === 'root') {
|
||||
break;
|
||||
}
|
||||
currentId = parentId;
|
||||
const parentId = node.parentId ?? 'root';
|
||||
if (!parentId || parentId === 'root') {
|
||||
break;
|
||||
}
|
||||
});
|
||||
}, [orderedSelectedDocuments, folderNodes, ensureFolderData, detailFolderFetchRef]);
|
||||
currentId = parentId;
|
||||
}
|
||||
}, [detailPanelDocument, folderNodes, ensureFolderData, detailFolderFetchRef]);
|
||||
|
||||
const resolveFolderPath = useCallback(
|
||||
(folderId) => {
|
||||
@@ -240,11 +197,11 @@ const useDetailWorkspace = ({
|
||||
);
|
||||
|
||||
const inspectDocument = useCallback(
|
||||
(documentId) => {
|
||||
(documentId: Identifier) => {
|
||||
if (!documentId) {
|
||||
return;
|
||||
}
|
||||
openDetailPanel({ documentIds: [documentId] });
|
||||
openDetailPanel(documentId);
|
||||
},
|
||||
[openDetailPanel],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user