Skip to content

Build your personal knowledge base with Trilium Notes

fix(autocomplete): drop a held search and the key handle once a list closes

Elian Doranfbf57c1a40fe
diff --git a/apps/client/src/widgets/react/FormAutocomplete.tsx b/apps/client/src/widgets/react/FormAutocomplete.tsx
index ed5190a91f..c24727d95a 100644
--- a/apps/client/src/widgets/react/FormAutocomplete.tsx
+++ b/apps/client/src/widgets/react/FormAutocomplete.tsx
@@ -236,6 +236,10 @@ export function useForwardedKeys<T>(autocomplete: ReturnType<typeof useAutocompl
return e.defaultPrevented;
}
};
+ // Lets go of the list, so a host that outlives it holds none of its entries.
+ return () => {
+ handleRef.current = null;
+ };
});
}
@@ -387,6 +391,12 @@ export function useAutocomplete<T>({ query, source, onPick, inputRef, disabled,
setItems([]);
}, []);
+ // A lookup a scheduler still holds supersedes nothing once the list is gone, so it is dropped
+ // rather than run for a list no longer there.
+ useEffect(() => () => {
+ latestQuery.current++;
+ }, []);
+
// Fetch suggestions for the current query, debounced. The previous items stay visible while
// the request is in flight, so refining a query does not make the dropdown flicker.
useEffect(() => {
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index e94c9000b9..2ab903fd93 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -1167,6 +1167,23 @@ describe("NoteMentionList", () => {
await unmount();
});
+ it("runs no search held back for a query once the list is closed, and lets go of its keys", async () => {
+ const handleRef = createRef<AutocompleteListHandle>() as { current: AutocompleteListHandle | null };
+ const host = document.createElement("div");
+ const draw = (text: string) => act(async () => {
+ preactRender(<NoteMentionList query={text} anchor={anchor} onPick={() => {}} handleRef={handleRef} />, host);
+ });
+
+ await draw("al");
+ // Typed within the scheduler's window, so its search waits.
+ await draw("alp");
+ await act(() => preactRender(null, host));
+ await act(async () => { await vi.advanceTimersByTimeAsync(1000); });
+
+ expect(getNoteSuggestions).not.toHaveBeenCalledWith("alp", expect.anything());
+ expect(handleRef.current).toBeNull();
+ });
+
it("takes the highlighted note on Tab as on Enter", async () => {
const { onPick, press, unmount } = await show("al");