feat: refactor document filtering logic to useWorkspaceViewData.
This commit is contained in:
@@ -402,33 +402,18 @@ const useDocumentsWorkspace = ({
|
|||||||
documentsManager,
|
documentsManager,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const showingSearchResults = documentsSearch.searchResultIds !== null;
|
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 {
|
const {
|
||||||
viewDocuments,
|
viewDocuments,
|
||||||
visibleEntryKeySet,
|
visibleEntryKeySet,
|
||||||
} = useWorkspaceViewData({
|
} = useWorkspaceViewData({
|
||||||
documents: liveFilteredDocuments,
|
documents,
|
||||||
documentLookup,
|
documentLookup,
|
||||||
searchResultIds: documentsSearch.searchResultIds,
|
searchResultIds: documentsSearch.searchResultIds,
|
||||||
showingSearchResults,
|
showingSearchResults,
|
||||||
currentSubfolders: visibleSubfolders,
|
currentSubfolders: visibleSubfolders,
|
||||||
|
selectedFolder: selectedFolder,
|
||||||
});
|
});
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useEffect, useMemo, useState } from 'react';
|
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 type { Document } from '../../types/documents';
|
||||||
import { createDocumentEntryKey, createFolderEntryKey } from '../../app/entryKey';
|
import { createDocumentEntryKey, createFolderEntryKey } from '../../app/entryKey';
|
||||||
|
|
||||||
@@ -9,6 +9,7 @@ interface UseWorkspaceViewDataArgs {
|
|||||||
searchResultIds: DocumentId[] | null;
|
searchResultIds: DocumentId[] | null;
|
||||||
showingSearchResults: boolean;
|
showingSearchResults: boolean;
|
||||||
currentSubfolders: any[];
|
currentSubfolders: any[];
|
||||||
|
selectedFolder: FolderId;
|
||||||
}
|
}
|
||||||
|
|
||||||
const useWorkspaceViewData = ({
|
const useWorkspaceViewData = ({
|
||||||
@@ -17,9 +18,22 @@ const useWorkspaceViewData = ({
|
|||||||
searchResultIds,
|
searchResultIds,
|
||||||
showingSearchResults,
|
showingSearchResults,
|
||||||
currentSubfolders,
|
currentSubfolders,
|
||||||
|
selectedFolder,
|
||||||
}: UseWorkspaceViewDataArgs) => {
|
}: UseWorkspaceViewDataArgs) => {
|
||||||
const [visibleDocumentIds, setVisibleDocumentIds] = useState<DocumentId[]>([]);
|
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(() => {
|
useEffect(() => {
|
||||||
const arraysEqual = (a: DocumentId[], b: DocumentId[]) =>
|
const arraysEqual = (a: DocumentId[], b: DocumentId[]) =>
|
||||||
a.length === b.length && a.every((value, index) => value === b[index]);
|
a.length === b.length && a.every((value, index) => value === b[index]);
|
||||||
@@ -30,11 +44,11 @@ const useWorkspaceViewData = ({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const folderIds = documents
|
const folderIds = filteredDocuments
|
||||||
.map((doc) => (doc?.id ?? null) as DocumentId | null)
|
.map((doc) => (doc?.id ?? null) as DocumentId | null)
|
||||||
.filter((id): id is DocumentId => id != null);
|
.filter((id): id is DocumentId => id != null);
|
||||||
setVisibleDocumentIds((prev) => (arraysEqual(prev, folderIds) ? prev : folderIds));
|
setVisibleDocumentIds((prev) => (arraysEqual(prev, folderIds) ? prev : folderIds));
|
||||||
}, [showingSearchResults, searchResultIds, documents]);
|
}, [showingSearchResults, searchResultIds, filteredDocuments]);
|
||||||
|
|
||||||
const viewDocuments = useMemo(
|
const viewDocuments = useMemo(
|
||||||
() =>
|
() =>
|
||||||
|
|||||||
Reference in New Issue
Block a user