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
fix(search): start a new search on the first page of results (#11787)
Elian Doran60fe6ca4bafe
diff --git a/apps/client/src/widgets/collections/search/SearchResultsList.spec.tsx b/apps/client/src/widgets/collections/search/SearchResultsList.spec.tsxindex 9d9ba254cb..c0421e9d6d 100644--- a/apps/client/src/widgets/collections/search/SearchResultsList.spec.tsx+++ b/apps/client/src/widgets/collections/search/SearchResultsList.spec.tsx@@ -20,10 +20,11 @@ vi.mock("../../../services/i18n", () => ({}));import Component from "../../../components/component";-import { buildNote } from "../../../test/easy-froca";+import server from "../../../services/server";+import { buildNote, buildNotes } from "../../../test/easy-froca";import { ParentComponent } from "../../react/react_utils";import SearchResultCard, { getBreadcrumbTitle, toPlainSearchTerms } from "./SearchResultCard";-import { SearchResultsToolbar } from "./SearchResultsList";+import SearchResultsList, { SearchResultsToolbar } from "./SearchResultsList";function makeDetails(overrides: Partial<SearchResultDetails> = {}): SearchResultDetails {return {@@ -55,6 +56,15 @@ afterEach(() => {container.remove();});+/** Lets the pending loads (`useNoteIds`, `froca.getNotes()`, the details fetch) commit. */+async function settle() {+ for (let pass = 0; pass < 3; pass++) {+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 0));+ });+ }+}+async function mount(element: preact.VNode) {await act(async () => {render(<ParentComponent.Provider value={parent}>{element}</ParentComponent.Provider>, container);@@ -119,6 +129,46 @@ describe("SearchResultCard", () => {});});+describe("SearchResultsList", () => {+ it("goes back to the first page when the search runs again", async () => {+ vi.spyOn(server, "post").mockResolvedValue({ results: [] });+ // 25 results make two pages at the default page size of 20.+ const firstResults = buildNotes(Array.from({ length: 25 }, (_, i) => ({ id: `first-${i}`, title: `First ${i}` })));+ const secondResults = buildNotes(Array.from({ length: 25 }, (_, i) => ({ id: `second-${i}`, title: `Second ${i}` })));+ const note = buildNote({ id: "search", title: "Search", type: "search" });+ note.children = firstResults;++ await mount(<SearchResultsList note={note} notePath="search" ntxId="ntx1" media="screen" />);+ await settle();+ const secondPageButton = [ ...container.querySelectorAll(".note-list-pager-page-button") ]+ .find((button) => button.textContent?.trim() === "2");+ expect(secondPageButton).toBeDefined();+ await act(async () => {+ secondPageButton?.dispatchEvent(new MouseEvent("click", { bubbles: true }));+ });+ await settle();+ expect(currentPage()).toBe("2");+ expect(cardHrefs()[0]).toBe("#first-20");++ // What `froca.loadSearchNote()` does when the search runs with a new query.+ note.children = secondResults;+ await act(async () => {+ await parent.handleEvent("searchRefreshed", { ntxId: "ntx1" });+ });+ await settle();+ expect(currentPage()).toBe("1");+ expect(cardHrefs()[0]).toBe("#second-0");+ });++ function currentPage() {+ return container.querySelector(".note-list-pager-page-button-current")?.textContent?.trim();+ }++ function cardHrefs() {+ return [ ...container.querySelectorAll("a.search-result-card") ].map((card) => card.getAttribute("href"));+ }+});+describe("SearchResultsToolbar", () => {it("always renders the result count, including for a single page", async () => {await mount(<SearchResultsToolbar count={1} pageSize={20} setPageSize={() => {}} />);diff --git a/apps/client/src/widgets/collections/search/SearchResultsList.tsx b/apps/client/src/widgets/collections/search/SearchResultsList.tsxindex f4276ea1d7..1827fafc16 100644--- a/apps/client/src/widgets/collections/search/SearchResultsList.tsx+++ b/apps/client/src/widgets/collections/search/SearchResultsList.tsx@@ -7,7 +7,7 @@ import type FNote from "../../../entities/fnote";import { t } from "../../../services/i18n";import CollectionProperties from "../../note_bars/CollectionProperties";import FormSelect from "../../react/FormSelect";-import { useTriliumOptionInt } from "../../react/hooks";+import { useTriliumEvent, useTriliumOptionInt } from "../../react/hooks";import { ViewModeMedia } from "../interface";import { useNoteIds } from "../NoteList";import { Pager, usePagination } from "../Pagination";@@ -56,6 +56,13 @@ function SearchResultsListInner({ note, ntxId, highlightedTokens }: SearchResult}, [ noteIds, pagination.page, pagination.pageSize ]);const { detailsByNoteId, loading } = useSearchResultDetails(note, pageNoteIds, ntxId);+ // A new search starts on the first page of its results.+ useTriliumEvent("searchRefreshed", ({ ntxId: eventNtxId }) => {+ if (eventNtxId === ntxId) {+ pagination.setPage(1);+ }+ });+return (<div className="search-results-list"><CollectionProperties