refactor: streamline hook integration and context object creation in useDocumentsWorkspace by directly using hook return values and consolidating related state.

This commit is contained in:
2025-12-10 02:07:44 +01:00
parent f0b58317b9
commit ed8450c0e2
2 changed files with 99 additions and 175 deletions
@@ -58,7 +58,7 @@ import { useApi } from '../../lib/context/ApiContext';
import { useWorkspaceSelection } from '../../app/useWorkspaceSelection'; import { useWorkspaceSelection } from '../../app/useWorkspaceSelection';
import useDocumentPreview from '../../app/useDocumentPreview'; import useDocumentPreview from '../../app/useDocumentPreview';
import type { DocumentId, FolderNodeId, Identifier } from '../../types/identifiers'; import type { DocumentId, FolderNodeId, Identifier } from '../../types/identifiers';
import type { Document, Tag } from '../../types/documents'; import type { Document } from '../../types/documents';
const EntryType = Object.freeze({ const EntryType = Object.freeze({
document: 'document', document: 'document',
@@ -394,7 +394,6 @@ const useDocumentsWorkspace = ({
setActiveTagFilters, setActiveTagFilters,
activeCorrespondentFilters, activeCorrespondentFilters,
setActiveCorrespondentFilters, setActiveCorrespondentFilters,
isFilterActive,
documentsFilterValue, documentsFilterValue,
} = useDocumentsSearch({ } = useDocumentsSearch({
api: apiClient, api: apiClient,
@@ -484,57 +483,21 @@ const useDocumentsWorkspace = ({
selectionInitializedRef, selectionInitializedRef,
}); });
const tagsStateRaw = useTags({ const tagsState = useTags({
tenantIdRef, tenantIdRef,
tagManager, tagManager,
setActiveTagFilters, setActiveTagFilters,
documentsManager, documentsManager,
}); });
useEffect(() => { // Correspondents state
tagManager.ensureAll().catch((err) => console.warn('Failed to bootstrap tags', err)); const correspondentsState = useCorrespondents({
}, [tagManager]);
const {
tags,
refreshTags,
handleTagCreate,
handleTagUpdate,
handleTagDelete,
} = tagsStateRaw;
// tagLookupById is derived locally
useEffect(() => {
tenantIdRef.current = currentTenantId;
}, [currentTenantId, tenantIdRef]);
const tagLookupById = new Map<Identifier, Tag>();
tags.forEach((tag) => {
if (tag?.id) {
tagLookupById.set(tag.id, tag);
}
});
const tagsState = {
...tagsStateRaw,
tags,
tagLookupById,
tagManager,
};
const correspondentsStateRaw = useCorrespondents({
correspondentManager, correspondentManager,
documentsManager, documentsManager,
}); });
const { const { refreshTags } = tagsState;
correspondents, const { refreshCorrespondents } = correspondentsState;
correspondentLookupById,
refreshCorrespondents,
handleCorrespondentCreate,
handleCorrespondentUpdate,
handleCorrespondentDelete,
} = correspondentsStateRaw;
// Prefetch tags/correspondents when tenant changes // Prefetch tags/correspondents when tenant changes
useEffect(() => { useEffect(() => {
@@ -542,16 +505,7 @@ const useDocumentsWorkspace = ({
refreshCorrespondents(); refreshCorrespondents();
}, [refreshTags, refreshCorrespondents, currentTenantId]); }, [refreshTags, refreshCorrespondents, currentTenantId]);
const { const passkeys = usePasskeys({});
passkeys,
passkeysSupported,
passkeysLoading,
registeringPasskey,
revokingPasskeyId,
refreshPasskeys,
registerPasskey,
revokePasskey,
} = usePasskeys({});
const resolveTargetDocumentIds = useCallback( const resolveTargetDocumentIds = useCallback(
(candidateIds) => { (candidateIds) => {
@@ -588,21 +542,14 @@ const useDocumentsWorkspace = ({
handleBulkTagRemoveFromDetail, handleBulkTagRemoveFromDetail,
handleBulkSelectionReanalyze, handleBulkSelectionReanalyze,
} = useDocumentTagActions({ } = useDocumentTagActions({
tags, tags: tagsState.tags,
tagManager, tagManager,
refreshTags, refreshTags: tagsState.refreshTags,
resolveTargetDocumentIds, resolveTargetDocumentIds,
documentsManager, documentsManager,
}); });
const { const upload = useDocumentUploads({
dropOverlayState,
handleFileDrop,
uploadQueue,
clearUploadQueue,
resetUploadsState,
handleFileSelection,
} = useDocumentUploads({
selectedFolder, selectedFolder,
currentFolderName, currentFolderName,
refreshCurrentFolder: refreshFolderData, refreshCurrentFolder: refreshFolderData,
@@ -645,8 +592,8 @@ const useDocumentsWorkspace = ({
detailPanelControlRef.current.close(); detailPanelControlRef.current.close();
assetManager.reset(); assetManager.reset();
resetPreviewState(); resetPreviewState();
resetUploadsState(); upload.resetUploadsState();
clearUploadQueue(); upload.clearUploadQueue();
detailFolderFetchRef.current = new Set(); detailFolderFetchRef.current = new Set();
bootstrapInitializedRef.current = false; bootstrapInitializedRef.current = false;
@@ -671,8 +618,7 @@ const useDocumentsWorkspace = ({
setActiveCorrespondentFilters, setActiveCorrespondentFilters,
setActivePreviewId, setActivePreviewId,
resetPreviewState, resetPreviewState,
resetUploadsState, upload,
clearUploadQueue,
]); ]);
useEffect(() => { useEffect(() => {
@@ -695,6 +641,22 @@ const useDocumentsWorkspace = ({
closeDocumentPreview, closeDocumentPreview,
}; };
const documentMutationsResult = useDocumentMutations({
documentsState,
folderState,
selectionState,
tagsState: {
...tagsState,
tagManager,
},
correspondentsState: {
...correspondentsState,
correspondentManager,
},
actions: actionsState,
previewDocumentId,
});
const { const {
moveDocumentsToFolder, moveDocumentsToFolder,
handleThumbnailRegeneration, handleThumbnailRegeneration,
@@ -708,20 +670,14 @@ const useDocumentsWorkspace = ({
handleDocumentCorrespondentDetach, handleDocumentCorrespondentDetach,
handleDocumentCorrespondentAdd, handleDocumentCorrespondentAdd,
correspondentLookupByName, correspondentLookupByName,
} = useDocumentMutations({ } = documentMutationsResult;
documentsState,
folderState, const mutations = {
selectionState, ...documentMutationsResult,
tagsState, handleDocumentDragStart,
correspondentsState: { handleDocumentDragEnd,
correspondents, draggedDocumentIds,
correspondentLookupById, };
refreshCorrespondents,
correspondentManager,
},
actions: actionsState,
previewDocumentId,
});
const dragState = { const dragState = {
draggedDocumentIds, draggedDocumentIds,
@@ -730,18 +686,11 @@ const useDocumentsWorkspace = ({
setDraggedFolderId, setDraggedFolderId,
}; };
const { const folderActions = useFolderTreeActions({
loadFolder,
selectFolder,
handleFolderRename,
handleFolderCreate,
handleFolderDelete,
folderClickHandlers,
} = useFolderTreeActions({
folderState, folderState,
dragState, dragState,
actions: { actions: {
handleFileDrop, handleFileDrop: upload.handleFileDrop,
moveDocumentsToFolder, moveDocumentsToFolder,
}, },
utils: { utils: {
@@ -750,8 +699,13 @@ const useDocumentsWorkspace = ({
}); });
const { const {
clearDocumentSelection, loadFolder,
} = useDocumentsSelection({ selectFolder,
handleFolderCreate,
handleFolderDelete,
} = folderActions;
const selectionContext = useDocumentsSelection({
showingSearchResults, showingSearchResults,
currentSubfolders: visibleSubfolders, currentSubfolders: visibleSubfolders,
visibleDocuments: viewDocuments, visibleDocuments: viewDocuments,
@@ -767,6 +721,7 @@ const useDocumentsWorkspace = ({
setFocusedEntryKey, setFocusedEntryKey,
focusedEntryKey, focusedEntryKey,
}); });
const initializeAfterLogin = useCallback(async () => { const initializeAfterLogin = useCallback(async () => {
await Promise.all([ await Promise.all([
refreshTags(), refreshTags(),
@@ -801,7 +756,6 @@ const useDocumentsWorkspace = ({
routeFolderId, routeFolderId,
routeDocumentId, routeDocumentId,
selectedFolder, selectedFolder,
isFilterActive,
selectFolder, selectFolder,
]); ]);
@@ -849,12 +803,12 @@ const useDocumentsWorkspace = ({
} = useBulkDocumentActions({ } = useBulkDocumentActions({
resolveTargetDocumentIds, resolveTargetDocumentIds,
correspondentLookupByName, correspondentLookupByName,
handleCorrespondentCreate, handleCorrespondentCreate: correspondentsState.handleCorrespondentCreate,
selectedDocumentIds, selectedDocumentIds,
selectedFolderIds, selectedFolderIds,
handleDocumentsDelete, handleDocumentsDelete,
handleFolderDelete, handleFolderDelete,
clearDocumentSelection, clearDocumentSelection: selectionContext.clearDocumentSelection,
documentsManager, documentsManager,
}); });
@@ -913,17 +867,17 @@ const useDocumentsWorkspace = ({
const { managementModals, openTagsModal, openCorrespondentsModal } = useManagementModals({ const { managementModals, openTagsModal, openCorrespondentsModal } = useManagementModals({
locationPathname: location.pathname, locationPathname: location.pathname,
tags, tags: tagsState.tags,
refreshTags, refreshTags: tagsState.refreshTags,
onTagCreate: handleTagCreate, onTagCreate: tagsState.handleTagCreate,
onTagUpdate: async (tagId: string, changes: any) => { await handleTagUpdate(tagId, changes); }, onTagUpdate: async (tagId: string, changes: any) => { await tagsState.handleTagUpdate(tagId, changes); },
onTagDelete: async (tagId: string) => { await handleTagDelete(tagId); }, onTagDelete: async (tagId: string) => { await tagsState.handleTagDelete(tagId); },
correspondents, correspondents: correspondentsState.correspondents,
correspondentLookupById, correspondentLookupById: correspondentsState.correspondentLookupById,
refreshCorrespondents, refreshCorrespondents: correspondentsState.refreshCorrespondents,
onCorrespondentCreate: handleCorrespondentCreate, onCorrespondentCreate: correspondentsState.handleCorrespondentCreate,
onCorrespondentUpdate: handleCorrespondentUpdate, onCorrespondentUpdate: correspondentsState.handleCorrespondentUpdate,
onCorrespondentDelete: handleCorrespondentDelete, onCorrespondentDelete: correspondentsState.handleCorrespondentDelete,
correspondentManager, correspondentManager,
}); });
@@ -971,13 +925,13 @@ const useDocumentsWorkspace = ({
handleDocumentTagDetach, handleDocumentTagDetach,
ensureAssetUrl, ensureAssetUrl,
getAsset: getDocumentAsset, getAsset: getDocumentAsset,
correspondents, correspondents: correspondentsState.correspondents,
handleCorrespondentAdd: handleDocumentCorrespondentAdd, handleCorrespondentAdd: handleDocumentCorrespondentAdd,
handleCorrespondentRemove: handleDocumentCorrespondentDetach, handleCorrespondentRemove: handleDocumentCorrespondentDetach,
selectFolder, selectFolder,
tags, tags: tagsState.tags,
tagLookupById, tagLookupById: tagsState.tagLookupById,
correspondentLookupById, correspondentLookupById: correspondentsState.correspondentLookupById,
}); });
const handleEntryPointerCore = useEntryPointerCore({ const handleEntryPointerCore = useEntryPointerCore({
@@ -1009,7 +963,7 @@ const useDocumentsWorkspace = ({
handleDocumentsViewModeChange, handleDocumentsViewModeChange,
}); });
const sessionContext = { const session = {
token, token,
appStatus, appStatus,
handleLogout, handleLogout,
@@ -1019,7 +973,7 @@ const useDocumentsWorkspace = ({
handleTenantSelect, handleTenantSelect,
}; };
const uiContext = { const ui = {
notifyApiError, notifyApiError,
settingsOpen, settingsOpen,
openSettings, openSettings,
@@ -1028,21 +982,11 @@ const useDocumentsWorkspace = ({
refreshCurrentFolder: handleManualRefresh, refreshCurrentFolder: handleManualRefresh,
}; };
const uploadContext = { const tags = {
dropOverlayState, ...tagsState,
uploadQueue, tagManager,
clearUploadQueue,
handleFileSelection,
};
const tagsContext = {
tags,
refreshTags,
tagLookupById,
activeTagFilters, activeTagFilters,
handleTagUpdate, // Add derived/action handlers that were previously in tagsContext
handleTagCreate,
handleTagDelete,
handleDocumentTagAttach, handleDocumentTagAttach,
handleDocumentTagDetach, handleDocumentTagDetach,
handleBulkTagAddFromDetail, handleBulkTagAddFromDetail,
@@ -1050,14 +994,10 @@ const useDocumentsWorkspace = ({
openTagsModal, openTagsModal,
}; };
const correspondentsContext = { const correspondents = {
correspondents, ...correspondentsState,
correspondentLookupById,
refreshCorrespondents,
activeCorrespondentFilters, activeCorrespondentFilters,
handleCorrespondentUpdate, // Add derived/action handlers
handleCorrespondentCreate,
handleCorrespondentDelete,
handleDocumentCorrespondentAttach, handleDocumentCorrespondentAttach,
handleDocumentCorrespondentDetach, handleDocumentCorrespondentDetach,
handleDocumentCorrespondentAdd, handleDocumentCorrespondentAdd,
@@ -1066,18 +1006,7 @@ const useDocumentsWorkspace = ({
openCorrespondentsModal, openCorrespondentsModal,
}; };
const passkeysContext = { const preview = {
passkeys,
passkeysSupported,
passkeysLoading,
registeringPasskey,
revokingPasskeyId,
refreshPasskeys,
registerPasskey,
revokePasskey,
};
const previewContext = {
previewActive, previewActive,
previewWorkspaceDocument, previewWorkspaceDocument,
previewDocumentId, previewDocumentId,
@@ -1087,13 +1016,13 @@ const useDocumentsWorkspace = ({
handleThumbnailRegeneration, handleThumbnailRegeneration,
}; };
const detailPanelContext = { const detailPanel = {
detailPanelProps, detailPanelProps,
detailPanelOpen, detailPanelOpen,
openDetailPanel, openDetailPanel,
}; };
const searchContext = { const search = {
searchQuery, searchQuery,
documentsFilter, documentsFilter,
searchLoading, searchLoading,
@@ -1107,18 +1036,18 @@ const useDocumentsWorkspace = ({
documents: viewDocuments, documents: viewDocuments,
}; };
const folderTreeContext = { const folderTree = {
foldersManager, foldersManager,
selectedFolder, selectedFolder,
currentFolderName, currentFolderName,
folderOptions, folderOptions,
handleBreadcrumbNavigate, handleBreadcrumbNavigate,
resolveFolderPath, resolveFolderPath,
selectFolder, selectFolder: folderActions.selectFolder,
moveDocumentsToFolder, moveDocumentsToFolder,
folderClickHandlers, folderClickHandlers: folderActions.folderClickHandlers,
handleFolderRename, handleFolderRename: folderActions.handleFolderRename,
handleFolderDelete, handleFolderDelete: folderActions.handleFolderDelete,
handleFolderDragStart, handleFolderDragStart,
handleFolderDragEnd, handleFolderDragEnd,
draggedFolderId, draggedFolderId,
@@ -1128,40 +1057,33 @@ const useDocumentsWorkspace = ({
breadcrumbs, breadcrumbs,
}; };
const selectionContext = { const selection = {
clearDocumentSelection, clearDocumentSelection: selectionContext.clearDocumentSelection,
handleDeleteSelection, handleDeleteSelection,
handleEntryPointerCore, handleEntryPointerCore,
handleBulkSelectionReanalyze, handleBulkSelectionReanalyze,
selectionValue: selectionState, selectionValue: selectionState,
}; };
const documentMutations = {
handleDocumentTitleUpdate,
handleDocumentDragStart,
handleDocumentDragEnd,
draggedDocumentIds,
};
const managers = { const managers = {
documentsManager, documentsManager,
documentLookup, documentLookup,
}; };
const contextValue = { const contextValue = {
session: sessionContext, session,
ui: uiContext, ui,
upload: uploadContext, upload,
tags: tagsContext, tags,
correspondents: correspondentsContext, correspondents,
passkeys: passkeysContext, passkeys,
preview: previewContext, preview,
detailPanel: detailPanelContext, detailPanel,
search: searchContext, search,
folderTree: folderTreeContext, folderTree,
selection: selectionContext, selection,
mutations: documentMutations, mutations,
managers: managers, managers,
}; };
// hook callers handle rendering / routing // hook callers handle rendering / routing
@@ -1169,7 +1091,7 @@ const useDocumentsWorkspace = ({
appStatus, appStatus,
location, location,
shellRef, shellRef,
dropOverlayState, dropOverlayState: upload.dropOverlayState,
managementModals, managementModals,
contextValue, contextValue,
settingsOpen, settingsOpen,
+2
View File
@@ -14,6 +14,7 @@ interface UseTagsOptions {
interface UseTagsResult { interface UseTagsResult {
tags: Tag[]; tags: Tag[];
tagLookupById: Map<TagId, Tag>;
refreshTags: () => Promise<void>; refreshTags: () => Promise<void>;
handleTagUpdate: (tagId: TagId, changes: { label?: string; color?: string | null }) => Promise<boolean>; handleTagUpdate: (tagId: TagId, changes: { label?: string; color?: string | null }) => Promise<boolean>;
handleTagCreate: (payload?: { label?: string; color?: string | null }) => Promise<void>; handleTagCreate: (payload?: { label?: string; color?: string | null }) => Promise<void>;
@@ -130,6 +131,7 @@ const useTags = ({
return { return {
tags, tags,
tagLookupById: tagsSnapshot,
refreshTags, refreshTags,
handleTagUpdate, handleTagUpdate,
handleTagCreate, handleTagCreate,