fix
This commit is contained in:
@@ -1,10 +1,6 @@
|
|||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import type { Dispatch, SetStateAction } from 'react';
|
import type { Dispatch, SetStateAction } from 'react';
|
||||||
import {
|
import { TAG_FILTER_UNTAGGED } from './appLayoutUtils';
|
||||||
TAG_FILTER_UNTAGGED,
|
|
||||||
resolveDocumentRowKey,
|
|
||||||
isDocumentRowKey,
|
|
||||||
} from './appLayoutUtils';
|
|
||||||
|
|
||||||
type Identifier = string | number;
|
type Identifier = string | number;
|
||||||
|
|
||||||
@@ -18,24 +14,6 @@ type AssetManagerLike = {
|
|||||||
hydrateDocuments: (payload: unknown[]) => DocumentLike[];
|
hydrateDocuments: (payload: unknown[]) => DocumentLike[];
|
||||||
};
|
};
|
||||||
|
|
||||||
type SelectionHelpers = {
|
|
||||||
setSelectedEntries: (
|
|
||||||
updater:
|
|
||||||
| Array<Identifier | string>
|
|
||||||
| ((prev: Array<Identifier | string>) => Array<Identifier | string>),
|
|
||||||
) => void;
|
|
||||||
setSelectionOrder: (
|
|
||||||
updater:
|
|
||||||
| Array<Identifier | string>
|
|
||||||
| ((prev: Array<Identifier | string>) => Array<Identifier | string>),
|
|
||||||
) => void;
|
|
||||||
selectionOrderRef: { current: Array<Identifier | string> };
|
|
||||||
selectionAnchorRef: { current: Identifier | string | null };
|
|
||||||
setFocusedDocumentId: (
|
|
||||||
updater: Identifier | null | ((prev: Identifier | null) => Identifier | null),
|
|
||||||
) => void;
|
|
||||||
};
|
|
||||||
|
|
||||||
interface UseDocumentsSearchArgs {
|
interface UseDocumentsSearchArgs {
|
||||||
api: ApiClient;
|
api: ApiClient;
|
||||||
assetManager: AssetManagerLike;
|
assetManager: AssetManagerLike;
|
||||||
@@ -44,7 +22,6 @@ interface UseDocumentsSearchArgs {
|
|||||||
navigate?: (path: string, options?: { replace?: boolean }) => void;
|
navigate?: (path: string, options?: { replace?: boolean }) => void;
|
||||||
locationPathname?: string;
|
locationPathname?: string;
|
||||||
isDocumentsRoute?: boolean;
|
isDocumentsRoute?: boolean;
|
||||||
selectionHelpers: SelectionHelpers;
|
|
||||||
searchIncludeDescendants?: boolean;
|
searchIncludeDescendants?: boolean;
|
||||||
documentsSortField?: string;
|
documentsSortField?: string;
|
||||||
documentsSortDirection?: string;
|
documentsSortDirection?: string;
|
||||||
@@ -80,7 +57,6 @@ const useDocumentsSearch = ({
|
|||||||
navigate,
|
navigate,
|
||||||
locationPathname,
|
locationPathname,
|
||||||
isDocumentsRoute,
|
isDocumentsRoute,
|
||||||
selectionHelpers,
|
|
||||||
searchIncludeDescendants,
|
searchIncludeDescendants,
|
||||||
documentsSortField,
|
documentsSortField,
|
||||||
documentsSortDirection,
|
documentsSortDirection,
|
||||||
@@ -206,45 +182,8 @@ const useDocumentsSearch = ({
|
|||||||
|
|
||||||
if (!results.length) {
|
if (!results.length) {
|
||||||
setSearchLoading(false);
|
setSearchLoading(false);
|
||||||
selectionHelpers.setSelectedEntries([]);
|
|
||||||
selectionHelpers.setFocusedDocumentId(null);
|
|
||||||
selectionHelpers.selectionOrderRef.current = [];
|
|
||||||
selectionHelpers.setSelectionOrder([]);
|
|
||||||
selectionHelpers.selectionAnchorRef.current = null;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const resultKeys = results
|
|
||||||
.map((doc) => resolveDocumentRowKey(doc.id as Identifier))
|
|
||||||
.filter(Boolean) as Array<Identifier | string>;
|
|
||||||
|
|
||||||
let targetKey: Identifier | string | null = null;
|
|
||||||
let nextSelectionKeys: Array<Identifier | string> = [];
|
|
||||||
|
|
||||||
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) {
|
} catch (error) {
|
||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
notifyApiError(error, 'Search failed. Please try again.');
|
notifyApiError(error, 'Search failed. Please try again.');
|
||||||
@@ -278,7 +217,6 @@ const useDocumentsSearch = ({
|
|||||||
selectedFolder,
|
selectedFolder,
|
||||||
notifyApiError,
|
notifyApiError,
|
||||||
assetManager,
|
assetManager,
|
||||||
selectionHelpers,
|
|
||||||
setLoading,
|
setLoading,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
@@ -377,7 +377,6 @@ const useDocumentsWorkspace = ({
|
|||||||
navigate,
|
navigate,
|
||||||
locationPathname: location.pathname,
|
locationPathname: location.pathname,
|
||||||
isDocumentsRoute,
|
isDocumentsRoute,
|
||||||
selectionHelpers,
|
|
||||||
searchIncludeDescendants,
|
searchIncludeDescendants,
|
||||||
documentsSortField,
|
documentsSortField,
|
||||||
documentsSortDirection,
|
documentsSortDirection,
|
||||||
@@ -493,6 +492,25 @@ const useDocumentsWorkspace = ({
|
|||||||
return map;
|
return map;
|
||||||
}, [documents, searchResults, previewDocuments]);
|
}, [documents, searchResults, previewDocuments]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!showingSearchResults) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setSelectedEntries([]);
|
||||||
|
setSelectionOrder([]);
|
||||||
|
selectionOrderRef.current = [];
|
||||||
|
selectionAnchorRef.current = null;
|
||||||
|
setFocusedDocumentId(null);
|
||||||
|
}, [
|
||||||
|
showingSearchResults,
|
||||||
|
searchQuery,
|
||||||
|
setSelectedEntries,
|
||||||
|
setSelectionOrder,
|
||||||
|
selectionOrderRef,
|
||||||
|
selectionAnchorRef,
|
||||||
|
setFocusedDocumentId,
|
||||||
|
]);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
tags,
|
tags,
|
||||||
refreshTags,
|
refreshTags,
|
||||||
|
|||||||
Reference in New Issue
Block a user