Skip to content

Build your personal knowledge base with Trilium Notes

refactor(quick_search): render every result instead of paging them in

Elian Doran3f684904c2db
diff --git a/apps/client/src/widgets/quick_search.spec.tsx b/apps/client/src/widgets/quick_search.spec.tsx
index 01ef5acb99..00d53a93d5 100644
--- a/apps/client/src/widgets/quick_search.spec.tsx
+++ b/apps/client/src/widgets/quick_search.spec.tsx
@@ -43,7 +43,7 @@ describe("QuickSearch", () => {
expect(root?.classList.contains("has-query")).toBe(false);
});
- it("searches on Enter and lists the first results with their highlighted snippets", async () => {
+ it("searches on Enter and lists every result with its highlighted snippets", async () => {
const get = vi.spyOn(server, "get").mockResolvedValue(response(30, [ "hello" ]));
const { editor } = await mount();
@@ -51,7 +51,7 @@ describe("QuickSearch", () => {
pressEnter(editor);
expect(get).toHaveBeenCalledWith("quick-search/hello");
- const [ first ] = await waitForResults(15);
+ const [ first ] = await waitForResults(30);
expect(first.getAttribute("href")).toBe(calculateHash({
notePath: "root/note0",
@@ -210,36 +210,6 @@ describe("QuickSearch", () => {
expect(editor.hasFocus).toBe(true);
});
- it("shows more results as the list is scrolled to its end, and starts over on a new search", async () => {
- vi.spyOn(server, "get").mockResolvedValue(response(30, []));
- const { editor } = await mount();
-
- typeQuery(editor, "hello");
- pressEnter(editor);
- await waitForResults(15);
-
- const scroller = menu()?.querySelector<HTMLElement>(".quick-search-results");
- if (!scroller) throw new Error("The results did not render.");
- const scrollTo = (scrollTop: number) => act(() => {
- setScrollMetrics(scroller, { scrollTop, clientHeight: 400, scrollHeight: 1000 });
- scroller.dispatchEvent(new Event("scroll"));
- });
-
- scrollTo(500);
- expect(resultItems()).toHaveLength(15);
-
- // Within 50px of the end.
- scrollTo(560);
- await waitForResults(25);
- scrollTo(600);
- await waitForResults(30);
- scrollTo(600);
- expect(resultItems()).toHaveLength(30);
-
- pressEnter(editor);
- await waitForResults(15);
- });
-
it("leaves the open results alone while the query is typed", async () => {
vi.spyOn(server, "get").mockResolvedValue(response(15, []));
const { editor } = await mount();
@@ -337,13 +307,6 @@ function pressArrowDown(editor: EditorView, modifiers: KeyboardEventInit = {}) {
});
}
-/** Gives `element` the scroll metrics happy-dom, which lays nothing out, leaves at 0. */
-function setScrollMetrics(element: HTMLElement, metrics: { scrollTop: number, clientHeight: number, scrollHeight: number }) {
- for (const [ name, value ] of Object.entries(metrics)) {
- Object.defineProperty(element, name, { configurable: true, get: () => value });
- }
-}
-
/** The popup is portaled to `<body>`, outside the rendered container. */
function menu() {
return document.querySelector<HTMLElement>(".quick-search-menu");
diff --git a/apps/client/src/widgets/quick_search.tsx b/apps/client/src/widgets/quick_search.tsx
index 53a4df12f3..627b77f280 100644
--- a/apps/client/src/widgets/quick_search.tsx
+++ b/apps/client/src/widgets/quick_search.tsx
@@ -17,11 +17,6 @@ import Popup from "./react/Popup";
import RawHtml, { RawHtmlBlock } from "./react/RawHtml";
import SearchStringEditor from "./ribbon/SearchStringEditor";
-const INITIAL_DISPLAYED_NOTES = 15;
-const LOAD_MORE_BATCH_SIZE = 10;
-/** How close to the end of the list, in pixels, scrolling shows the next batch. */
-const LOAD_MORE_THRESHOLD = 50;
-
type SearchState =
| { status: "searching" }
| { status: "done", results: SearchResultDetails[], viewScope: ViewScope | undefined };
@@ -32,7 +27,6 @@ export default function QuickSearch() {
const [ hasQuery, setHasQuery ] = useState(false);
const [ searchState, setSearchState ] = useState<SearchState>();
const [ open, setOpen ] = useState(false);
- const [ displayedCount, setDisplayedCount ] = useState(INITIAL_DISPLAYED_NOTES);
const editorRef = useRef<FieldEditor>();
const boxRef = useRef<HTMLDivElement>(null);
const popupRef = useRef<HTMLDivElement | null>(null);
@@ -58,7 +52,6 @@ export default function QuickSearch() {
);
if (requestId !== requestIdRef.current) return;
- setDisplayedCount(INITIAL_DISPLAYED_NOTES);
setSearchState({
status: "done",
results: searchResults,
@@ -67,16 +60,6 @@ export default function QuickSearch() {
});
}
- /** Shows the next batch of results once `scroller` is scrolled near its end. */
- function showMoreAtEnd(scroller: HTMLElement) {
- const total = searchState?.status === "done" ? searchState.results.length : 0;
- const { scrollTop, clientHeight, scrollHeight } = scroller;
- const nearEnd = scrollTop + clientHeight >= scrollHeight - LOAD_MORE_THRESHOLD;
- if (displayedCount >= total || !nearEnd) return;
- // From the rendered count, so scroll events before the next render ask for the same one.
- setDisplayedCount(displayedCount + LOAD_MORE_BATCH_SIZE);
- }
-
function close() {
// Focus on a result goes back to the field, rather than to the page's body.
if (popupRef.current?.contains(document.activeElement)) editorRef.current?.focus();
@@ -124,15 +107,8 @@ export default function QuickSearch() {
elementRef={popupRef}
onDismiss={close}
>
- <div
- className="quick-search-results"
- onScroll={(e) => showMoreAtEnd(e.currentTarget)}
- >
- <QuickSearchResults
- searchState={searchState}
- displayedCount={displayedCount}
- onOpenResult={close}
- />
+ <div className="quick-search-results">
+ <QuickSearchResults searchState={searchState} onOpenResult={close} />
</div>
</Popup>
)}
@@ -178,9 +154,8 @@ function firstResult(popup: HTMLElement) {
return popup.querySelector(".dropdown-item:not(.disabled)");
}
-function QuickSearchResults({ searchState, displayedCount, onOpenResult }: {
+function QuickSearchResults({ searchState, onOpenResult }: {
searchState: SearchState | undefined;
- displayedCount: number;
onOpenResult(): void;
}) {
if (!searchState) return null;
@@ -198,7 +173,7 @@ function QuickSearchResults({ searchState, displayedCount, onOpenResult }: {
return <span className="dropdown-item disabled">{t("quick-search.no-results")}</span>;
}
- return searchState.results.slice(0, displayedCount).map((result) => (
+ return searchState.results.map((result) => (
<QuickSearchResult
key={result.notePath}
result={result}