simplify shit
This commit is contained in:
@@ -18,7 +18,7 @@ import useApiError from '../../hooks/useApiError';
|
||||
import TagManager from '../../lib/assets/TagManager';
|
||||
import { useManagementModals } from '../../app/useManagementModals';
|
||||
import { useAppDispatch, useAppState } from '../../lib/store/appState';
|
||||
import { fetchAsset } from '../../lib/api/apiClient';
|
||||
import { fetchAsset, listFolderContents } from '../../lib/api/apiClient';
|
||||
import { useApi } from '../../lib/context/ApiContext';
|
||||
import { useWorkspaceSelection } from '../../app/useWorkspaceSelection';
|
||||
import { useEntryPointer as useEntryPointerCore } from '../features/selection/useEntryPointer';
|
||||
@@ -34,7 +34,12 @@ import {
|
||||
createRootNode,
|
||||
mergeAssetIntoDocument,
|
||||
} from '../../app/workspaceUtils';
|
||||
import { createDocumentEntryKey, createFolderEntryKey } from '../../app/entryKey';
|
||||
import {
|
||||
createDocumentEntryKey,
|
||||
createFolderEntryKey,
|
||||
isFolderEntry,
|
||||
isDocumentEntry
|
||||
} from '../../app/entryKey';
|
||||
import useDocumentsSearch from '../../app/useDocumentsSearch';
|
||||
import { useStatusToast, type ToastVariant } from '../../lib/context/StatusToastContext';
|
||||
import useAuthManager from './useAuthManager';
|
||||
@@ -281,27 +286,6 @@ const useDocumentsWorkspace = ({
|
||||
configureSelectionEnvironment,
|
||||
} = selection;
|
||||
|
||||
const selectionHelpers = useMemo(
|
||||
() => ({
|
||||
setSelectedEntries,
|
||||
setSelectionOrder,
|
||||
selectionOrderRef,
|
||||
selectionAnchorRef,
|
||||
setFocusedDocumentId,
|
||||
focusedDocumentId,
|
||||
selectionInitializedRef,
|
||||
}),
|
||||
[
|
||||
setSelectedEntries,
|
||||
setSelectionOrder,
|
||||
selectionOrderRef,
|
||||
selectionAnchorRef,
|
||||
setFocusedDocumentId,
|
||||
focusedDocumentId,
|
||||
selectionInitializedRef,
|
||||
],
|
||||
);
|
||||
|
||||
const [folderContents, setFolderContents] = useState<Map<FolderNodeId, FolderContentsEntry>>(
|
||||
() => new Map(),
|
||||
);
|
||||
@@ -339,27 +323,102 @@ const useDocumentsWorkspace = ({
|
||||
setFolderNodes,
|
||||
selectedFolder,
|
||||
setSelectedFolder,
|
||||
currentFolder,
|
||||
setCurrentFolder,
|
||||
currentSubfolders,
|
||||
setCurrentSubfolders,
|
||||
currentFolderName,
|
||||
folderOptions,
|
||||
folderLabelMap,
|
||||
applySelectedFolder,
|
||||
ensureFolderData,
|
||||
isInvalidFolderDrop,
|
||||
} = useFolderTree({
|
||||
initialSelectedFolder: routeFolderId || 'root',
|
||||
documentsSortFieldRef: activeSortFieldRef,
|
||||
documentsSortDirectionRef: activeSortDirectionRef,
|
||||
selectionHelpers,
|
||||
setDocuments,
|
||||
setFolderContents,
|
||||
folderContentsRef,
|
||||
foldersManager,
|
||||
});
|
||||
|
||||
const [currentFolder, setCurrentFolder] = useState<FolderContentsEntry['folder'] | null>(null);
|
||||
const [currentSubfolders, setCurrentSubfolders] = useState<FolderContentsEntry['subfolders']>([]);
|
||||
|
||||
const ensureFolderData = useCallback(
|
||||
async (
|
||||
folderId: FolderNodeId,
|
||||
options: { includeDocuments?: boolean } = {}
|
||||
) => {
|
||||
try {
|
||||
const path = folderId === 'root' ? 'root' : folderId;
|
||||
const includeDocuments = options.includeDocuments ?? true;
|
||||
const params: Record<string, unknown> = {
|
||||
include_documents: includeDocuments,
|
||||
sort: activeSortFieldRef.current,
|
||||
dir: activeSortDirectionRef.current,
|
||||
};
|
||||
|
||||
const data = await listFolderContents(path, params);
|
||||
|
||||
// Only update UI state if we are fetching for the currently selected folder
|
||||
if (folderId === selectedFolder) {
|
||||
// Update documents state if included
|
||||
if (includeDocuments) {
|
||||
setDocuments((data.documents || []) as Document[]);
|
||||
}
|
||||
setCurrentSubfolders((data.subfolders || []) as any[]);
|
||||
setCurrentFolder(data.folder || null);
|
||||
|
||||
// Update selection state based on new documents
|
||||
if (includeDocuments) {
|
||||
const docs = (data.documents || []) as Document[];
|
||||
const subfolders = (data.subfolders || []) as any[];
|
||||
|
||||
const availableDocKeys = docs
|
||||
.map((doc) => createDocumentEntryKey(doc?.id as Identifier))
|
||||
.filter(Boolean);
|
||||
const availableDocKeySet = new Set(availableDocKeys);
|
||||
const availableFolderKeys = new Set(
|
||||
subfolders
|
||||
.map((folder) => createFolderEntryKey(folder?.id as Identifier))
|
||||
.filter(Boolean),
|
||||
);
|
||||
|
||||
setSelectedEntries((previous) => {
|
||||
const previousFolderKeys = previous
|
||||
.filter(isFolderEntry)
|
||||
.filter((key) => availableFolderKeys.has(key));
|
||||
const previousDocKeys = previous.filter(isDocumentEntry);
|
||||
const nextDocKeys = previousDocKeys.filter((key) => availableDocKeySet.has(key));
|
||||
const mergedSelection = [...previousFolderKeys, ...nextDocKeys];
|
||||
return mergedSelection;
|
||||
});
|
||||
}
|
||||
}
|
||||
return data; // Return data for consumers (e.g. useDocumentMutations)
|
||||
} catch (error) {
|
||||
notifyApiError(error, 'Failed to fetch folder contents');
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[
|
||||
activeSortFieldRef,
|
||||
activeSortDirectionRef,
|
||||
setDocuments,
|
||||
setCurrentFolder,
|
||||
setSelectedEntries,
|
||||
notifyApiError,
|
||||
setCurrentSubfolders,
|
||||
selectedFolder,
|
||||
]
|
||||
);
|
||||
|
||||
// Reactively fetch documents when selectedFolder changes
|
||||
useEffect(() => {
|
||||
if (selectedFolder) {
|
||||
ensureFolderData(selectedFolder);
|
||||
}
|
||||
}, [selectedFolder, ensureFolderData]);
|
||||
|
||||
// Also re-fetch when sort changes
|
||||
useEffect(() => {
|
||||
if (selectedFolder && sortRefreshReadyRef?.current) {
|
||||
ensureFolderData(selectedFolder);
|
||||
}
|
||||
}, [documentsSortField, documentsSortDirection, selectedFolder, ensureFolderData, sortRefreshReadyRef]);
|
||||
|
||||
|
||||
const {
|
||||
searchQuery,
|
||||
setSearchQuery,
|
||||
@@ -426,7 +485,7 @@ const useDocumentsWorkspace = ({
|
||||
() =>
|
||||
showingSearchResults
|
||||
? []
|
||||
: currentSubfolders
|
||||
: (currentSubfolders || [])
|
||||
.map((folder) => createFolderEntryKey(folder.id))
|
||||
.filter(Boolean),
|
||||
[showingSearchResults, currentSubfolders],
|
||||
@@ -577,9 +636,10 @@ const useDocumentsWorkspace = ({
|
||||
);
|
||||
|
||||
const refreshCurrentFolder = useCallback(async () => {
|
||||
const contents = await ensureFolderData(selectedFolder);
|
||||
applySelectedFolder(selectedFolder, contents);
|
||||
}, [selectedFolder, ensureFolderData, applySelectedFolder]);
|
||||
if (selectedFolder) {
|
||||
await ensureFolderData(selectedFolder);
|
||||
}
|
||||
}, [selectedFolder, ensureFolderData]);
|
||||
|
||||
const {
|
||||
handleBulkTagAddFromDetail,
|
||||
@@ -601,15 +661,16 @@ const useDocumentsWorkspace = ({
|
||||
uploadQueue,
|
||||
clearUploadQueue,
|
||||
resetUploadsState,
|
||||
handleFileSelection,
|
||||
} = useDocumentUploads({
|
||||
token,
|
||||
selectedFolder,
|
||||
currentFolderName,
|
||||
ensureFolderData,
|
||||
refreshCurrentFolder,
|
||||
shellRef,
|
||||
notifyApiError,
|
||||
setStatusMessage,
|
||||
shellRef,
|
||||
});
|
||||
|
||||
const {
|
||||
@@ -618,15 +679,15 @@ const useDocumentsWorkspace = ({
|
||||
handleFolderDragStart,
|
||||
handleFolderDragEnd,
|
||||
} = useDocumentDragHandlers({
|
||||
documentLookup,
|
||||
setDraggedDocumentIds,
|
||||
setDraggedFolderId,
|
||||
documentsViewMode,
|
||||
selectedEntries,
|
||||
selectedDocumentIds,
|
||||
selectedFolderIds,
|
||||
applySelection,
|
||||
handleEntrySelection,
|
||||
documentLookup,
|
||||
setDraggedDocumentIds,
|
||||
setDraggedFolderId,
|
||||
documentsViewMode,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
@@ -649,7 +710,6 @@ const useDocumentsWorkspace = ({
|
||||
const resetWorkspaceState = useCallback(() => {
|
||||
const rootNode = createRootNode();
|
||||
setFolderNodes(new Map([[rootNode.id, rootNode]]));
|
||||
setFolderContents(new Map());
|
||||
setSelectedFolder('root');
|
||||
setCurrentFolder(null);
|
||||
setCurrentSubfolders([]);
|
||||
@@ -687,7 +747,7 @@ const useDocumentsWorkspace = ({
|
||||
setSelectedEntries,
|
||||
setSelectionOrder,
|
||||
setFolderNodes,
|
||||
setFolderContents,
|
||||
// setFolderContents,
|
||||
setSelectedFolder,
|
||||
setCurrentFolder,
|
||||
setCurrentSubfolders,
|
||||
@@ -730,35 +790,11 @@ const useDocumentsWorkspace = ({
|
||||
return filtered.length === prev.length ? prev : filtered;
|
||||
});
|
||||
|
||||
setFolderContents((prev: Map<FolderNodeId, FolderContentsEntry>) => {
|
||||
if (!prev.size) {
|
||||
return prev;
|
||||
}
|
||||
let changed = false;
|
||||
const next = new Map<FolderNodeId, FolderContentsEntry>();
|
||||
prev.forEach((contents, key) => {
|
||||
const docs = Array.isArray(contents?.documents) ? contents.documents : null;
|
||||
if (!docs || docs.length === 0) {
|
||||
next.set(key, contents);
|
||||
return;
|
||||
}
|
||||
const filtered = docs.filter((doc) => !idSet.has(doc.id));
|
||||
if (filtered.length !== docs.length) {
|
||||
changed = true;
|
||||
next.set(key, { ...contents, documents: filtered });
|
||||
} else {
|
||||
next.set(key, contents);
|
||||
}
|
||||
});
|
||||
return changed ? next : prev;
|
||||
});
|
||||
|
||||
removeDocumentsFromLookup(Array.from(idSet));
|
||||
},
|
||||
[
|
||||
setDocuments,
|
||||
setSearchResultIds,
|
||||
setFolderContents,
|
||||
removeDocumentsFromLookup,
|
||||
],
|
||||
);
|
||||
@@ -780,7 +816,6 @@ const useDocumentsWorkspace = ({
|
||||
selectedFolder,
|
||||
setSelectedFolder,
|
||||
setDocuments,
|
||||
setFolderContents,
|
||||
setSearchResultIds,
|
||||
setSelectedEntries,
|
||||
setSelectionOrder,
|
||||
@@ -793,7 +828,6 @@ const useDocumentsWorkspace = ({
|
||||
notifyApiError,
|
||||
setStatusMessage,
|
||||
mapDocumentCaches,
|
||||
applySelectedFolder,
|
||||
folderNodes,
|
||||
setFolderNodes,
|
||||
removeDocumentsFromCaches,
|
||||
@@ -822,14 +856,9 @@ const useDocumentsWorkspace = ({
|
||||
setFolderNodes,
|
||||
selectedFolder,
|
||||
setSelectedFolder,
|
||||
ensureFolderData,
|
||||
applySelectedFolder,
|
||||
notifyApiError,
|
||||
setStatusMessage,
|
||||
setFolderContents,
|
||||
setCurrentFolder,
|
||||
setSearchResultIds,
|
||||
isFilterActive,
|
||||
navigate,
|
||||
handleFileDrop,
|
||||
moveDocumentsToFolder,
|
||||
@@ -883,8 +912,8 @@ const useDocumentsWorkspace = ({
|
||||
return;
|
||||
}
|
||||
|
||||
const hasData = folderContents.has(targetParam);
|
||||
if (targetParam !== selectedFolder || !hasData) {
|
||||
// Checking cache (folderContents) is removed, now we rely on selectedFolder effect to fetch.
|
||||
if (targetParam !== selectedFolder) {
|
||||
selectFolder(targetParam, { immediate: true });
|
||||
}
|
||||
}, [
|
||||
@@ -893,7 +922,6 @@ const useDocumentsWorkspace = ({
|
||||
routeFolderId,
|
||||
routeDocumentId,
|
||||
selectedFolder,
|
||||
folderContents,
|
||||
isFilterActive,
|
||||
selectFolder,
|
||||
]);
|
||||
@@ -1302,6 +1330,7 @@ const useDocumentsWorkspace = ({
|
||||
tenants: tenantOptions,
|
||||
tenantOptions,
|
||||
handleTenantSelect,
|
||||
handleFileSelection,
|
||||
};
|
||||
|
||||
// hook callers handle rendering / routing
|
||||
|
||||
Reference in New Issue
Block a user