diff --git a/frontend/src/documents/panel/DocumentsPanel.jsx b/frontend/src/documents/panel/DocumentsPanel.jsx index 5692736..496fc38 100644 --- a/frontend/src/documents/panel/DocumentsPanel.jsx +++ b/frontend/src/documents/panel/DocumentsPanel.jsx @@ -59,6 +59,34 @@ const DocumentsPanel = ({ const showingSearchResults = searchResults !== null; const rows = showingSearchResults ? searchResults : documents; + const currentFolderId = useMemo(() => { + if (showingSearchResults) { + return null; + } + const trail = Array.isArray(breadcrumbs) ? breadcrumbs : []; + if (trail.length === 0) { + return 'root'; + } + return trail[trail.length - 1]?.id || 'root'; + }, [breadcrumbs, showingSearchResults]); + + const selectionContextRef = useRef(null); + useEffect(() => { + const nextContext = showingSearchResults + ? { type: 'search', marker: searchResults } + : { type: 'folder', marker: currentFolderId || 'root' }; + const previous = selectionContextRef.current; + selectionContextRef.current = nextContext; + if (!previous) { + return; + } + const changed = previous.type !== nextContext.type + || previous.marker !== nextContext.marker; + if (changed) { + onClearSelection?.(); + } + }, [showingSearchResults, currentFolderId, searchResults, onClearSelection]); + const entries = useMemo(() => { const list = []; if (!showingSearchResults) { diff --git a/frontend/src/hooks/documents/useDocumentsWorkspace.js b/frontend/src/hooks/documents/useDocumentsWorkspace.js index 6fe4baa..f17a52c 100644 --- a/frontend/src/hooks/documents/useDocumentsWorkspace.js +++ b/frontend/src/hooks/documents/useDocumentsWorkspace.js @@ -796,7 +796,6 @@ const useDocumentsWorkspace = ({ setDraggedFolderId, isInvalidFolderDrop, setCreatingFolder, - clearSelection, }); const { diff --git a/frontend/src/hooks/documents/useFolderTree.js b/frontend/src/hooks/documents/useFolderTree.js index 55ea0be..b0f7675 100644 --- a/frontend/src/hooks/documents/useFolderTree.js +++ b/frontend/src/hooks/documents/useFolderTree.js @@ -1,4 +1,4 @@ -import { useCallback, useMemo, useRef, useState } from 'react'; +import { useCallback, useMemo, useState } from 'react'; import { DEFAULT_FOLDER_NAME, createRootNode, @@ -30,8 +30,6 @@ const useFolderTree = ({ const [currentFolder, setCurrentFolder] = useState(null); const [currentSubfolders, setCurrentSubfolders] = useState([]); - const lastAppliedFolderIdRef = useRef(null); - const { focusedDocumentId, setFocusedDocumentId, @@ -43,9 +41,6 @@ const useFolderTree = ({ const applySelectedFolder = useCallback( (folderId, contents) => { - const folderChanged = lastAppliedFolderIdRef.current !== folderId; - lastAppliedFolderIdRef.current = folderId; - const subfolders = contents?.subfolders ?? []; const docs = assetManager.hydrateDocuments(contents?.documents ?? []); const folderInfo = contents?.folder ?? null; @@ -68,11 +63,6 @@ const useFolderTree = ({ let mergedSelection = []; setSelectedEntries((previous) => { - if (folderChanged) { - nextDocKeys = []; - mergedSelection = []; - return []; - } const previousFolderKeys = previous .filter(isFolderRowKey) .filter((key) => availableFolderKeys.has(key)); @@ -82,14 +72,6 @@ const useFolderTree = ({ return mergedSelection; }); - if (folderChanged) { - setFocusedDocumentId(null); - selectionAnchorRef.current = null; - selectionOrderRef.current = []; - setSelectionOrder([]); - return; - } - const nextFocus = (() => { const currentFocusedKey = resolveDocumentRowKey(focusedDocumentId); if (currentFocusedKey && availableDocKeySet.has(currentFocusedKey)) { diff --git a/frontend/src/hooks/documents/useFolderTreeActions.js b/frontend/src/hooks/documents/useFolderTreeActions.js index ebb7001..29da7b1 100644 --- a/frontend/src/hooks/documents/useFolderTreeActions.js +++ b/frontend/src/hooks/documents/useFolderTreeActions.js @@ -28,7 +28,6 @@ const useFolderTreeActions = ({ setDraggedFolderId, isInvalidFolderDrop, setCreatingFolder, - clearSelection, }) => { const moveFolder = useCallback( async (folderId, targetFolderId) => { @@ -174,10 +173,6 @@ const useFolderTreeActions = ({ async (folderId, { replace = false, immediate = false } = {}) => { const targetId = folderId && folderId !== 'root' ? folderId : 'root'; - if (typeof clearSelection === 'function') { - clearSelection(); - } - await ensureFolderAncestorsLoaded(targetId); expandFolderAncestors(targetId); @@ -191,7 +186,6 @@ const useFolderTreeActions = ({ navigate(path, { replace }); }, [ - clearSelection, ensureFolderAncestorsLoaded, expandFolderAncestors, isFilterActive,