Skip to content

Build your personal knowledge base with Trilium Notes

refactor(client): run a saved search through one `runSearchNote()`

Elian Doran217a1b80c9aa
diff --git a/apps/client/src/services/search.spec.ts b/apps/client/src/services/search.spec.ts
index 5031154f75..9088cab008 100644
--- a/apps/client/src/services/search.spec.ts
+++ b/apps/client/src/services/search.spec.ts
@@ -1,8 +1,11 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
+import Component from "../components/component.js";
import { buildNote } from "../test/easy-froca";
+import froca from "./froca.js";
import searchService from "./search.js";
import server from "./server.js";
+import toast from "./toast.js";
describe("search service", () => {
beforeEach(() => {
@@ -51,4 +54,23 @@ describe("search service", () => {
expect(notes).toEqual([]);
});
+
+ it("runSearchNote returns the query's error, toasts a failed request and refreshes the tab", async () => {
+ const component = new Component();
+ const triggerEvent = vi.spyOn(component, "triggerEvent");
+ const showError = vi.spyOn(toast, "showError").mockImplementation(() => {});
+ const loadSearchNote = vi.spyOn(froca, "loadSearchNote");
+
+ loadSearchNote.mockResolvedValueOnce({ error: "Bad query" });
+ expect(await searchService.runSearchNote(component, "search1", "ntx1")).toBe("Bad query");
+ expect(loadSearchNote).toHaveBeenCalledWith("search1");
+ expect(showError).not.toHaveBeenCalled();
+
+ loadSearchNote.mockRejectedValueOnce(new Error("Network down"));
+ expect(await searchService.runSearchNote(component, "search1", "ntx1")).toBeUndefined();
+ expect(showError).toHaveBeenCalledWith("Network down");
+
+ expect(triggerEvent).toHaveBeenCalledTimes(2);
+ expect(triggerEvent).toHaveBeenCalledWith("searchRefreshed", { ntxId: "ntx1" });
+ });
});
diff --git a/apps/client/src/services/search.ts b/apps/client/src/services/search.ts
index b4cbf1c0e4..b3af2298a4 100644
--- a/apps/client/src/services/search.ts
+++ b/apps/client/src/services/search.ts
@@ -1,7 +1,10 @@
import type { SearchWithTokensResponse } from "@triliumnext/commons";
-import server from "./server.js";
+import type Component from "../components/component.js";
import froca from "./froca.js";
+import server from "./server.js";
+import toast from "./toast.js";
+import { getErrorMessage } from "./utils.js";
async function searchForNoteIds(searchString: string) {
return await server.get<string[]>(`search/${encodeURIComponent(searchString)}`);
@@ -23,8 +26,25 @@ async function searchInSubtree(searchString: string, ancestorNoteId: string) {
+ `?ancestorNoteId=${encodeURIComponent(ancestorNoteId)}&includeTokens=true`);
}
+/**
+ * Runs a saved search and triggers `searchRefreshed` so the tab shows its results. A failed request
+ * is shown as a toast; an error in the query itself is returned for the caller to show.
+ */
+async function runSearchNote(component: Component | null | undefined, noteId: string, ntxId: string | null | undefined) {
+ let error: string | undefined;
+ try {
+ error = (await froca.loadSearchNote(noteId))?.error ?? undefined;
+ } catch (e: unknown) {
+ toast.showError(getErrorMessage(e));
+ }
+
+ component?.triggerEvent("searchRefreshed", { ntxId });
+ return error;
+}
+
export default {
searchForNoteIds,
searchForNotes,
- searchInSubtree
+ searchInSubtree,
+ runSearchNote
};
diff --git a/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx b/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx
index 5ab5526f44..82e890e9b1 100644
--- a/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx
+++ b/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx
@@ -8,12 +8,11 @@ import appContext from "../../components/app_context";
import FNote from "../../entities/fnote";
import attributes from "../../services/attributes";
import bulk_action, { ACTION_GROUPS } from "../../services/bulk_action";
-import froca from "../../services/froca";
import { t } from "../../services/i18n";
+import search from "../../services/search";
import server from "../../services/server";
import toast from "../../services/toast";
import tree from "../../services/tree";
-import { getErrorMessage } from "../../services/utils";
import ws from "../../services/ws";
import RenameNoteBulkAction from "../bulk_actions/note/rename_note";
import Button, { SplitButton } from "../react/Button";
@@ -55,18 +54,8 @@ export default function SearchDefinitionTab({ note, ntxId, hidden }: Pick<TabCon
return;
}
- try {
- const result = await froca.loadSearchNote(noteId);
- if (result?.error) {
- setError({ message: result?.error});
- } else {
- setError(undefined);
- }
- } catch (e: unknown) {
- toast.showError(getErrorMessage(e));
- }
-
- parentComponent?.triggerEvent("searchRefreshed", { ntxId });
+ const error = await search.runSearchNote(parentComponent, noteId, ntxId);
+ setError(error ? { message: error } : undefined);
}
// Refresh the list of available and active options.
diff --git a/apps/client/src/widgets/search_result.tsx b/apps/client/src/widgets/search_result.tsx
index 440318679a..1b9e88987d 100644
--- a/apps/client/src/widgets/search_result.tsx
+++ b/apps/client/src/widgets/search_result.tsx
@@ -5,10 +5,9 @@ import clsx from "clsx";
import { useState } from "preact/hooks";
import type FNote from "../entities/fnote";
-import froca from "../services/froca";
import { t } from "../services/i18n";
+import search from "../services/search";
import toast from "../services/toast";
-import { getErrorMessage } from "../services/utils";
import { SearchNoteList, useNoteViewType } from "./collections/NoteList";
import SearchResultsList from "./collections/search/SearchResultsList";
import Button from "./react/Button";
@@ -37,15 +36,11 @@ export default function SearchResult() {
if (!note?.noteId) {
return;
}
- try {
- const result = await froca.loadSearchNote(note.noteId);
- if (result?.error) {
- toast.showError(result.error);
- }
- } catch (e: unknown) {
- toast.showError(getErrorMessage(e));
+
+ const error = await search.runSearchNote(parentComponent, note.noteId, ntxId);
+ if (error) {
+ toast.showError(error);
}
- parentComponent?.triggerEvent("searchRefreshed", { ntxId });
}
useTriliumEvent("searchRefreshed", ({ ntxId: eventNtxId }) => {