diff --git a/frontend/src/app/useDocumentsSearch.ts b/frontend/src/app/useDocumentsSearch.ts index b8e688c..1e4b3be 100644 --- a/frontend/src/app/useDocumentsSearch.ts +++ b/frontend/src/app/useDocumentsSearch.ts @@ -1,10 +1,6 @@ import { useCallback, useEffect, useMemo, useState } from 'react'; import type { Dispatch, SetStateAction } from 'react'; -import { - TAG_FILTER_UNTAGGED, - resolveDocumentRowKey, - isDocumentRowKey, -} from './appLayoutUtils'; +import { TAG_FILTER_UNTAGGED } from './appLayoutUtils'; type Identifier = string | number; @@ -18,24 +14,6 @@ type AssetManagerLike = { hydrateDocuments: (payload: unknown[]) => DocumentLike[]; }; -type SelectionHelpers = { - setSelectedEntries: ( - updater: - | Array - | ((prev: Array) => Array), - ) => void; - setSelectionOrder: ( - updater: - | Array - | ((prev: Array) => Array), - ) => void; - selectionOrderRef: { current: Array }; - selectionAnchorRef: { current: Identifier | string | null }; - setFocusedDocumentId: ( - updater: Identifier | null | ((prev: Identifier | null) => Identifier | null), - ) => void; -}; - interface UseDocumentsSearchArgs { api: ApiClient; assetManager: AssetManagerLike; @@ -44,7 +22,6 @@ interface UseDocumentsSearchArgs { navigate?: (path: string, options?: { replace?: boolean }) => void; locationPathname?: string; isDocumentsRoute?: boolean; - selectionHelpers: SelectionHelpers; searchIncludeDescendants?: boolean; documentsSortField?: string; documentsSortDirection?: string; @@ -80,7 +57,6 @@ const useDocumentsSearch = ({ navigate, locationPathname, isDocumentsRoute, - selectionHelpers, searchIncludeDescendants, documentsSortField, documentsSortDirection, @@ -206,45 +182,8 @@ const useDocumentsSearch = ({ if (!results.length) { setSearchLoading(false); - selectionHelpers.setSelectedEntries([]); - selectionHelpers.setFocusedDocumentId(null); - selectionHelpers.selectionOrderRef.current = []; - selectionHelpers.setSelectionOrder([]); - selectionHelpers.selectionAnchorRef.current = null; return; } - - const resultKeys = results - .map((doc) => resolveDocumentRowKey(doc.id as Identifier)) - .filter(Boolean) as Array; - - let targetKey: Identifier | string | null = null; - let nextSelectionKeys: Array = []; - - selectionHelpers.setSelectedEntries((previous) => { - const previousDocKeys = previous.filter(isDocumentRowKey); - const filtered = previousDocKeys.filter((key) => resultKeys.includes(key)); - if (filtered.length) { - targetKey = filtered[filtered.length - 1]; - nextSelectionKeys = filtered; - return filtered; - } - targetKey = null; - nextSelectionKeys = []; - return []; - }); - - selectionHelpers.selectionOrderRef.current = nextSelectionKeys; - selectionHelpers.setSelectionOrder(nextSelectionKeys); - - selectionHelpers.setFocusedDocumentId((previous) => { - if (previous && resultKeys.includes(resolveDocumentRowKey(previous as Identifier))) { - return previous; - } - return null; - }); - - selectionHelpers.selectionAnchorRef.current = targetKey; } catch (error) { if (cancelled) return; notifyApiError(error, 'Search failed. Please try again.'); @@ -278,7 +217,6 @@ const useDocumentsSearch = ({ selectedFolder, notifyApiError, assetManager, - selectionHelpers, setLoading, ]); diff --git a/frontend/src/hooks/documents/useDocumentsWorkspace.ts b/frontend/src/hooks/documents/useDocumentsWorkspace.ts index da4796c..9f71864 100644 --- a/frontend/src/hooks/documents/useDocumentsWorkspace.ts +++ b/frontend/src/hooks/documents/useDocumentsWorkspace.ts @@ -377,7 +377,6 @@ const useDocumentsWorkspace = ({ navigate, locationPathname: location.pathname, isDocumentsRoute, - selectionHelpers, searchIncludeDescendants, documentsSortField, documentsSortDirection, @@ -493,6 +492,25 @@ const useDocumentsWorkspace = ({ return map; }, [documents, searchResults, previewDocuments]); + useEffect(() => { + if (!showingSearchResults) { + return; + } + setSelectedEntries([]); + setSelectionOrder([]); + selectionOrderRef.current = []; + selectionAnchorRef.current = null; + setFocusedDocumentId(null); + }, [ + showingSearchResults, + searchQuery, + setSelectedEntries, + setSelectionOrder, + selectionOrderRef, + selectionAnchorRef, + setFocusedDocumentId, + ]); + const { tags, refreshTags,