feat: refactor document filtering logic to useWorkspaceViewData.
This commit is contained in:
@@ -402,33 +402,18 @@ const useDocumentsWorkspace = ({
|
||||
documentsManager,
|
||||
});
|
||||
|
||||
|
||||
const showingSearchResults = documentsSearch.searchResultIds !== null;
|
||||
|
||||
// Live Filter: Ensure we only show documents that actually belong to the current folder.
|
||||
// Since 'documents' is reactive, if a document is moved, its folder_id updates immediately.
|
||||
// We must filter out any documents that no longer match the selectedFolder.
|
||||
const liveFilteredDocuments = useMemo(() => {
|
||||
if (showingSearchResults) {
|
||||
return documents;
|
||||
}
|
||||
const targetFolder = selectedFolder || 'root';
|
||||
return documents.filter((doc) => {
|
||||
if (!doc) return false;
|
||||
const docFolder = doc.folder_id || 'root';
|
||||
return docFolder === targetFolder;
|
||||
});
|
||||
}, [documents, showingSearchResults, selectedFolder]);
|
||||
|
||||
const {
|
||||
viewDocuments,
|
||||
visibleEntryKeySet,
|
||||
} = useWorkspaceViewData({
|
||||
documents: liveFilteredDocuments,
|
||||
documents,
|
||||
documentLookup,
|
||||
searchResultIds: documentsSearch.searchResultIds,
|
||||
showingSearchResults,
|
||||
currentSubfolders: visibleSubfolders,
|
||||
selectedFolder: selectedFolder,
|
||||
});
|
||||
|
||||
const {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import type { DocumentId } from '../../types/identifiers';
|
||||
import type { DocumentId, FolderId } from '../../types/identifiers';
|
||||
import type { Document } from '../../types/documents';
|
||||
import { createDocumentEntryKey, createFolderEntryKey } from '../../app/entryKey';
|
||||
|
||||
@@ -9,6 +9,7 @@ interface UseWorkspaceViewDataArgs {
|
||||
searchResultIds: DocumentId[] | null;
|
||||
showingSearchResults: boolean;
|
||||
currentSubfolders: any[];
|
||||
selectedFolder: FolderId;
|
||||
}
|
||||
|
||||
const useWorkspaceViewData = ({
|
||||
@@ -17,9 +18,22 @@ const useWorkspaceViewData = ({
|
||||
searchResultIds,
|
||||
showingSearchResults,
|
||||
currentSubfolders,
|
||||
selectedFolder,
|
||||
}: UseWorkspaceViewDataArgs) => {
|
||||
const [visibleDocumentIds, setVisibleDocumentIds] = useState<DocumentId[]>([]);
|
||||
|
||||
const filteredDocuments = useMemo(() => {
|
||||
if (showingSearchResults) {
|
||||
return documents;
|
||||
}
|
||||
const targetFolder = selectedFolder || 'root';
|
||||
return documents.filter((doc) => {
|
||||
if (!doc) return false;
|
||||
const docFolder = doc.folder_id || 'root';
|
||||
return docFolder === targetFolder;
|
||||
});
|
||||
}, [documents, showingSearchResults, selectedFolder]);
|
||||
|
||||
useEffect(() => {
|
||||
const arraysEqual = (a: DocumentId[], b: DocumentId[]) =>
|
||||
a.length === b.length && a.every((value, index) => value === b[index]);
|
||||
@@ -30,11 +44,11 @@ const useWorkspaceViewData = ({
|
||||
return;
|
||||
}
|
||||
|
||||
const folderIds = documents
|
||||
const folderIds = filteredDocuments
|
||||
.map((doc) => (doc?.id ?? null) as DocumentId | null)
|
||||
.filter((id): id is DocumentId => id != null);
|
||||
setVisibleDocumentIds((prev) => (arraysEqual(prev, folderIds) ? prev : folderIds));
|
||||
}, [showingSearchResults, searchResultIds, documents]);
|
||||
}, [showingSearchResults, searchResultIds, filteredDocuments]);
|
||||
|
||||
const viewDocuments = useMemo(
|
||||
() =>
|
||||
|
||||
Reference in New Issue
Block a user