Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board/search & filtering: improve performance

Adorian Dorandd13df31293b
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 10023c9161..f27ba2f044 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -3426,6 +3426,62 @@ describe("Board filtering", () => {
expect(cardTitles(0)).toEqual([ "Second", "Third" ]);
});
+ /**
+ * A board opened onto a stored query draws nothing until the query has said what it matches,
+ * rather than drawing every card and then taking most of them away.
+ */
+ it("draws no cards until the stored query has resolved", async () => {
+ let resolveSearch: (response: unknown) => void = () => {};
+ searchInSubtree.mockReset();
+ searchInSubtree.mockReturnValue(
+ new Promise(resolve => { resolveSearch = resolve; }) as never);
+
+ const note = buildNote({
+ title: "Board",
+ "#collection": "",
+ "#viewType": "board",
+ children: [
+ { id: "held1", title: "First", "#status": "To Do" },
+ { id: "held2", title: "Second", "#status": "To Do" }
+ ]
+ });
+
+ host = new Component();
+ container = document.body.appendChild(document.createElement("div"));
+ await act(async () => {
+ render(
+ <ParentComponent.Provider value={host}>
+ <Harness
+ note={note}
+ noteIds={[ ...note.getChildNoteIds() ]}
+ initialConfig={{
+ columns: [ { value: "To Do" } ],
+ filterQuery: "#urgent"
+ }}
+ />
+ </ParentComponent.Provider>,
+ container
+ );
+ });
+ await act(async () => { await flush(); });
+ await act(async () => { await flush(); });
+
+ expect(container.querySelectorAll(".board-column")).toHaveLength(0);
+ expect(container.querySelectorAll(".board-note")).toHaveLength(0);
+
+ await act(async () => {
+ resolveSearch({
+ searchResultNoteIds: [ "held2" ],
+ highlightedTokens: [],
+ error: null
+ });
+ await flush();
+ });
+ await act(async () => { await flush(); });
+
+ expect(cardTitles(0)).toEqual([ "Second" ]);
+ });
+
it("marks the matched tokens in the card titles", async () => {
await setup({
matched: [ "filtered1" ],
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index 4da5c08ac1..2dd46aa09d 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -352,9 +352,13 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
});
// The cards as drawn. Derived rather than held, so that what is shown cannot fall behind what
// the filter matches: a card move and a search resolving in the same moment both land here.
+ // Withheld until a query stored in `board.json` has said what it matches, so a filtered board
+ // is not drawn whole and then narrowed.
const byColumn = useMemo(
- () => allByColumn && filterColumnMap(allByColumn, filter.matchedNoteIds),
- [ allByColumn, filter.matchedNoteIds ]);
+ () => (allByColumn && !filter.isResolvingStoredQuery
+ ? filterColumnMap(allByColumn, filter.matchedNoteIds)
+ : undefined),
+ [ allByColumn, filter.matchedNoteIds, filter.isResolvingStoredQuery ]);
if (!apiRef.current || apiRef.current.board !== boardIdentity) {
apiRef.current = {
diff --git a/apps/client/src/widgets/collections/collection_filter.spec.tsx b/apps/client/src/widgets/collections/collection_filter.spec.tsx
index 85e8723d42..86c0a230d1 100644
--- a/apps/client/src/widgets/collections/collection_filter.spec.tsx
+++ b/apps/client/src/widgets/collections/collection_filter.spec.tsx
@@ -160,6 +160,47 @@ describe("useCollectionFilter", () => {
expect(currentFilter?.matchedNoteIds?.has("n1")).toBe(true);
});
+ it("holds while a stored query resolves, and not for one submitted afterwards", async () => {
+ const stored = deferred<ReturnType<typeof matchResponse>>();
+ searchInSubtree.mockReturnValueOnce(stored);
+ await mount({ persistedQuery: "#done" });
+
+ // Nothing may be drawn yet: the matches the board would narrow to are still unknown.
+ expect(currentFilter?.isResolvingStoredQuery).toBe(true);
+
+ await act(async () => {
+ stored.resolve(matchResponse([ "n1" ]));
+ await flushMicrotasks();
+ });
+ expect(currentFilter?.isResolvingStoredQuery).toBe(false);
+
+ // A later submit narrows a board already on screen, which must not be taken away.
+ const submitted = deferred<ReturnType<typeof matchResponse>>();
+ searchInSubtree.mockReturnValueOnce(submitted);
+ await act(async () => { currentFilter?.setQuery("#other"); });
+
+ expect(currentFilter?.isResolvingStoredQuery).toBe(false);
+ await act(async () => {
+ submitted.resolve(matchResponse([ "n2" ]));
+ await flushMicrotasks();
+ });
+ });
+
+ it("stops holding when a stored query cannot be run", async () => {
+ searchInSubtree.mockRejectedValueOnce(new Error("offline"));
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ await mount({ persistedQuery: "#done" });
+ await settle();
+
+ expect(currentFilter?.isResolvingStoredQuery).toBe(false);
+ expect(currentFilter?.error).toBe("collection_filter.fetch-error");
+ });
+
+ it("does not hold when nothing was stored", async () => {
+ await mount({});
+ expect(currentFilter?.isResolvingStoredQuery).toBe(false);
+ });
+
it("keeps the previous matches when a query comes back with an error", async () => {
searchInSubtree.mockResolvedValueOnce(matchResponse([ "n1" ]));
await mount({});
@@ -280,6 +321,7 @@ describe("CollectionFilterInput", () => {
matchedNoteIds: null,
highlightedTokens: null,
error: null,
+ isResolvingStoredQuery: false,
setQuery: vi.fn(),
...overrides
};
diff --git a/apps/client/src/widgets/collections/collection_filter.tsx b/apps/client/src/widgets/collections/collection_filter.tsx
index be6eb2e04d..d87b616fba 100644
--- a/apps/client/src/widgets/collections/collection_filter.tsx
+++ b/apps/client/src/widgets/collections/collection_filter.tsx
@@ -34,6 +34,11 @@ export interface CollectionFilter {
highlightedTokens: HighlightedTokenInfo[] | null;
/** What is wrong with the query, or null. An erroneous query keeps the previous matches. */
error: string | null;
+ /**
+ * Whether a stored query is still being resolved, for the view to hold its first draw on. Only
+ * a stored one: a query submitted later narrows a collection that is already on screen.
+ */
+ isResolvingStoredQuery: boolean;
setQuery: (query: string) => void;
}
@@ -64,10 +69,15 @@ export function useCollectionFilter(note: FNote, {
const [ query, setQuery ] = useState(persistedQuery ?? "");
const [ matches, setMatches ] = useState<FilterMatches>(NO_MATCHES);
const [ error, setError ] = useState<string | null>(null);
+ // Set before anything is drawn, so a collection opened onto a stored query waits for its
+ // matches instead of drawing every note and then taking most of them away.
+ const [ isResolvingStoredQuery, setIsResolvingStoredQuery ] =
+ useState(() => !!persistedQuery?.trim());
// A run resolves on the server, so an older one can land after a newer one. Only the latest
// run sets the result.
const runSeqRef = useRef(0);
const rerunTimerRef = useRef<number>();
+ const previousNoteIdRef = useRef(note.noteId);
const collectionSet = useMemo(() => new Set(collectionNoteIds), [ collectionNoteIds ]);
const collectionSetRef = useRef(collectionSet);
collectionSetRef.current = collectionSet;
@@ -81,6 +91,7 @@ export function useCollectionFilter(note: FNote, {
console.error("Failed to run the collection filter:", e);
if (seq === runSeqRef.current) {
setError(t("collection_filter.fetch-error"));
+ setIsResolvingStoredQuery(false);
}
return;
}
@@ -89,6 +100,10 @@ export function useCollectionFilter(note: FNote, {
return;
}
+ // The wait is over whatever the run came back with: a stored query that cannot be run must
+ // not hold the collection back from being drawn.
+ setIsResolvingStoredQuery(false);
+
if (response.error) {
// The previous matches stay shown: a broken query must not blank the collection.
setError(response.error);
@@ -105,6 +120,14 @@ export function useCollectionFilter(note: FNote, {
// Adopt a query stored elsewhere: the persisted one on mount, or one another view submitted.
useEffect(() => {
setQuery(persistedQuery ?? "");
+
+ // Another collection brings its own stored query, and this instance is reused across them,
+ // so the wait is armed again here rather than only at mount. Not for a query submitted in
+ // this collection, which also arrives as a new `persistedQuery`.
+ if (previousNoteIdRef.current !== note.noteId) {
+ previousNoteIdRef.current = note.noteId;
+ setIsResolvingStoredQuery(!!persistedQuery?.trim());
+ }
}, [ note.noteId, persistedQuery ]);
// Resolve the submitted query, or drop the filter when it is cleared.
@@ -114,6 +137,7 @@ export function useCollectionFilter(note: FNote, {
runSeqRef.current++;
setError(null);
setMatches(NO_MATCHES);
+ setIsResolvingStoredQuery(false);
return;
}
run(query);
@@ -134,6 +158,7 @@ export function useCollectionFilter(note: FNote, {
query,
...matches,
error,
+ isResolvingStoredQuery,
setQuery: (newQuery: string) => {
const trimmed = newQuery.trim();
setQuery(trimmed);