Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(quick_search): keep typing from re-rendering the results
Elian Doran7f6e4e6c696b
diff --git a/apps/client/src/widgets/quick_search.spec.tsx b/apps/client/src/widgets/quick_search.spec.tsxindex 97cd718205..2c094eee4a 100644--- a/apps/client/src/widgets/quick_search.spec.tsx+++ b/apps/client/src/widgets/quick_search.spec.tsx@@ -1,5 +1,6 @@import type { EditorView } from "@codemirror/view";import type { QuickSearchResponse } from "@triliumnext/commons";+import { options, type VNode } from "preact";import { act } from "preact/test-utils";import { afterEach, describe, expect, it, vi } from "vitest";@@ -129,6 +130,30 @@ describe("QuickSearch", () => {await vi.waitFor(() => expect(menu()).toBeNull());});+ it("leaves the open results alone while the query is typed", async () => {+ vi.spyOn(server, "get").mockResolvedValue(response(15, []));+ const { editor } = await mount();++ typeQuery(editor, "h");+ pressEnter(editor);+ await waitForResults(15);++ let resultRenders = 0;+ const previousDiffed = options.diffed;+ options.diffed = (vnode: VNode) => {+ if (typeof vnode.type === "function" && vnode.type.name === "QuickSearchResult") resultRenders++;+ previousDiffed?.(vnode);+ };+ try {+ typeQuery(editor, "he");+ typeQuery(editor, "hel");+ typeQuery(editor, "hell");+ } finally {+ options.diffed = previousDiffed;+ }+ expect(resultRenders).toBe(0);+ });+it("stands the results under the search box, which a press does not close them from", async () => {vi.spyOn(server, "get").mockResolvedValue(response(3, []));// happy-dom lays nothing out, so the viewport is given a size to place the popup in.diff --git a/apps/client/src/widgets/quick_search.tsx b/apps/client/src/widgets/quick_search.tsxindex 0812b23206..6214f40f00 100644--- a/apps/client/src/widgets/quick_search.tsx+++ b/apps/client/src/widgets/quick_search.tsx@@ -21,7 +21,9 @@ type SearchState =| { status: "done", results: SearchResultDetails[], viewScope: ViewScope | undefined };export default function QuickSearch() {- const [ searchString, setSearchString ] = useState("");+ // In a ref rather than state, so typing re-renders nothing; only its emptiness is state.+ const searchStringRef = useRef("");+ const [ hasQuery, setHasQuery ] = useState(false);const [ searchState, setSearchState ] = useState<SearchState>();const editorRef = useRef<FieldEditor>();const dropdownRef = useRef<DropdownHandle | null>(null);@@ -33,7 +35,7 @@ export default function QuickSearch() {useTriliumEvent("quickSearch", () => editorRef.current?.focus());async function search() {- const query = searchString.trim();+ const query = searchStringRef.current.trim();if (!query) {dropdownRef.current?.hide();return;@@ -55,7 +57,7 @@ export default function QuickSearch() {}return (- <div className={clsx("quick-search", searchString && "has-query")}>+ <div className={clsx("quick-search", hasQuery && "has-query")}><div ref={boxRef} className="quick-search-box"><DropdownPanelclassName="quick-search-toggle"@@ -85,11 +87,13 @@ export default function QuickSearch() {<SearchStringEditorclassName="search-string"- currentValue={searchString}placeholder={t("quick-search.placeholder")}singleLineeditorRef={editorRef}- onChange={setSearchString}+ onChange={(value) => {+ searchStringRef.current = value;+ setHasQuery(value.length > 0);+ }}onEnter={() => {// Opening the results runs the search; open results are refreshed.if (isOpenRef.current) {diff --git a/apps/client/src/widgets/ribbon/SearchStringEditor.tsx b/apps/client/src/widgets/ribbon/SearchStringEditor.tsxindex 91d2239045..2d01f29e8a 100644--- a/apps/client/src/widgets/ribbon/SearchStringEditor.tsx+++ b/apps/client/src/widgets/ribbon/SearchStringEditor.tsx@@ -9,7 +9,11 @@ import { useContextualShortcutHints } from "../react/hooks";import { createSearchFieldEditor, SEARCH_FIELD_EDITOR_CLASS } from "../search_field_editor";interface SearchStringEditorProps {- currentValue: string;+ /**+ * The query to show. Without it, the editor owns its document and only reports edits through+ * `onChange`, for a caller that re-renders nothing as the query is typed.+ */+ currentValue?: string;/** The note `currentValue` belongs to, if any. A change to it replaces the document. */noteId?: string;placeholder?: string;@@ -123,7 +127,8 @@ export default function SearchStringEditor({ currentValue, noteId, placeholder,const switchedNote = shownNoteId.current !== noteId;shownNoteId.current = noteId;- if (!editor || editor.state.doc.toString() === currentValue || (editor.hasFocus && !switchedNote)) {+ if (!editor || currentValue === undefined || editor.state.doc.toString() === currentValue+ || (editor.hasFocus && !switchedNote)) {return;}