feat: Refactor frontend authentication and tenant switching to use global state
This commit is contained in:
@@ -145,15 +145,8 @@ const useDocumentsWorkspace = ({
|
||||
: [];
|
||||
const { showToast } = useStatusToast();
|
||||
const notifyApiError = useNotifyApiError();
|
||||
|
||||
const [creatingFolder, setCreatingFolder] = useState(false);
|
||||
|
||||
const { tokenRef, handleLogout } = useAuthManager({
|
||||
token,
|
||||
appStatus,
|
||||
appDispatch,
|
||||
});
|
||||
|
||||
const { handleLogout } = useAuthManager({});
|
||||
|
||||
const tagRemovalCursorActiveRef = useRef(false);
|
||||
const tenantIdRef = useRef(currentTenantId);
|
||||
@@ -287,63 +280,59 @@ const useDocumentsWorkspace = ({
|
||||
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);
|
||||
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,
|
||||
};
|
||||
|
||||
// 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[]);
|
||||
const data = await listFolderContents(path, params);
|
||||
|
||||
// Update selection state based on new documents
|
||||
if (includeDocuments) {
|
||||
const docs = (data.documents || []) as Document[];
|
||||
const subfolders = (data.subfolders || []) as any[];
|
||||
// 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[]);
|
||||
|
||||
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),
|
||||
);
|
||||
// Update selection state based on new documents
|
||||
if (includeDocuments) {
|
||||
const docs = (data.documents || []) as Document[];
|
||||
const subfolders = (data.subfolders || []) as any[];
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
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;
|
||||
}
|
||||
return data; // Return data for consumers (e.g. useDocumentMutations)
|
||||
|
||||
},
|
||||
[
|
||||
activeSortFieldRef,
|
||||
activeSortDirectionRef,
|
||||
setDocuments,
|
||||
setSelectedEntries,
|
||||
notifyApiError,
|
||||
setCurrentSubfolders,
|
||||
selectedFolder,
|
||||
]
|
||||
@@ -351,9 +340,11 @@ const useDocumentsWorkspace = ({
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedFolder) {
|
||||
ensureFolderData(selectedFolder);
|
||||
ensureFolderData(selectedFolder).catch((error) => {
|
||||
notifyApiError(error, 'Failed to fetch folder contents');
|
||||
});
|
||||
}
|
||||
}, [selectedFolder, documentsSortField, documentsSortDirection, ensureFolderData]);
|
||||
}, [selectedFolder, documentsSortField, documentsSortDirection, ensureFolderData, notifyApiError]);
|
||||
|
||||
const {
|
||||
searchQuery,
|
||||
@@ -369,7 +360,6 @@ const useDocumentsWorkspace = ({
|
||||
documentsFilterValue,
|
||||
} = useDocumentsSearch({
|
||||
api: apiClient,
|
||||
token,
|
||||
selectedFolder,
|
||||
locationPathname: location.pathname,
|
||||
isDocumentsRoute,
|
||||
@@ -542,9 +532,7 @@ const useDocumentsWorkspace = ({
|
||||
refreshPasskeys,
|
||||
registerPasskey,
|
||||
revokePasskey,
|
||||
} = usePasskeys({
|
||||
token,
|
||||
});
|
||||
} = usePasskeys({});
|
||||
|
||||
const resolveTargetDocumentIds = useCallback(
|
||||
(candidateIds) => {
|
||||
@@ -585,7 +573,6 @@ const useDocumentsWorkspace = ({
|
||||
resetUploadsState,
|
||||
handleFileSelection,
|
||||
} = useDocumentUploads({
|
||||
token,
|
||||
selectedFolder,
|
||||
currentFolderName,
|
||||
ensureFolderData,
|
||||
@@ -709,7 +696,6 @@ const useDocumentsWorkspace = ({
|
||||
handleDocumentIssuedUpdate,
|
||||
handleTagRemove,
|
||||
} = useDocumentMutations({
|
||||
token,
|
||||
documentLookup,
|
||||
folderLabelMap,
|
||||
ensureFolderData,
|
||||
@@ -749,7 +735,6 @@ const useDocumentsWorkspace = ({
|
||||
handleFolderDelete,
|
||||
folderClickHandlers,
|
||||
} = useFolderTreeActions({
|
||||
token,
|
||||
folderNodes,
|
||||
setFolderNodes,
|
||||
selectedFolder,
|
||||
@@ -1078,13 +1063,7 @@ const useDocumentsWorkspace = ({
|
||||
|
||||
const { handleTenantSelect } = useTenantManager({
|
||||
currentTenantId,
|
||||
resetWorkspaceState,
|
||||
refreshTags,
|
||||
refreshCorrespondents,
|
||||
loadFolder,
|
||||
handleDocumentsViewModeChange,
|
||||
tokenRef,
|
||||
tenantIdRef,
|
||||
});
|
||||
|
||||
const sessionContext = {
|
||||
|
||||
Reference in New Issue
Block a user