Skip to content

Build your personal knowledge base with Trilium Notes

refactor(quick_search): remove the old widget, showing search errors in the results

Elian Doran74c228b19fd5
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 6f8b0d1234..48028bcfdf 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2755,14 +2755,14 @@
"placeholder": "Quick search",
"searching": "Searching...",
"no-results": "No results found",
- "more-results": "... and {{number}} more results.",
"show-in-full-search": "Show in full search",
"hints": {
"title": "Quick search results",
"to_results": "Move to the results",
"page": "Move a page through the results",
"close": "Close the results"
- }
+ },
+ "error": "Search error"
},
"note_tree": {
"collapse-title": "Collapse note tree",
diff --git a/apps/client/src/widgets/quick_search.css b/apps/client/src/widgets/quick_search.css
index 48e8175f6d..e605d9fa00 100644
--- a/apps/client/src/widgets/quick_search.css
+++ b/apps/client/src/widgets/quick_search.css
@@ -241,4 +241,9 @@
.bx-loader {
margin-inline-end: 4px;
}
+
+ .quick-search-error {
+ margin: 4px 8px;
+ white-space: normal;
+ }
}
diff --git a/apps/client/src/widgets/quick_search.spec.tsx b/apps/client/src/widgets/quick_search.spec.tsx
index 981610d2ed..cf2a387f0e 100644
--- a/apps/client/src/widgets/quick_search.spec.tsx
+++ b/apps/client/src/widgets/quick_search.spec.tsx
@@ -18,6 +18,12 @@ import { ParentComponent } from "./react/react_utils";
const completions = vi.hoisted(() => ({
source: null as ((context: CompletionContext) => unknown) | null
}));
+// Echoes the key and its interpolation, so a spec can tell which message a row shows.
+vi.mock("../services/i18n", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../services/i18n")>()),
+ t: (key: string, options?: object) => (options ? `${key} ${JSON.stringify(options)}` : key)
+}));
+
vi.mock("./ribbon/search_completions", () => ({
searchCompletionSource: (context: CompletionContext) => completions.source?.(context) ?? null,
searchCompletionIcon: () => undefined,
@@ -100,6 +106,34 @@ describe("QuickSearch", () => {
expect(resultItems()).toHaveLength(0);
});
+ it("shows the error the search ran into, above whatever it still found", async () => {
+ const failed = { ...response(0, []), error: "Note content can be searched only with operators" };
+ const get = vi.spyOn(server, "get").mockResolvedValue(failed);
+ const { editor } = await mount();
+
+ typeQuery(editor, "note.content > 3");
+ pressEnter(editor);
+
+ // In place of "No results found", which would hide why.
+ const error = await vi.waitFor(() => {
+ const row = menu()?.querySelector(".quick-search-error");
+ expect(row).not.toBeNull();
+ return row;
+ });
+ // A caution card titled as an error, with the server's message as its body.
+ expect(error?.classList.contains("extended-admonition")).toBe(true);
+ expect(error?.classList.contains("caution")).toBe(true);
+ expect(error?.querySelector(".admonition-title")?.textContent).toBe("quick-search.error");
+ expect(error?.querySelector(".admonition-body")?.textContent)
+ .toBe("Note content can be searched only with operators");
+ expect(placeholderItems()).toEqual([]);
+
+ get.mockResolvedValue({ ...response(2, []), error: "Note content can be searched only with operators" });
+ pressEnter(editor);
+ await waitForResults(2);
+ expect(menu()?.querySelector(".quick-search-results")?.firstElementChild?.classList.contains("quick-search-error")).toBe(true);
+ });
+
it("keeps the results of the latest search when an earlier one answers last", async () => {
const earlier = deferred<QuickSearchResponse>();
const later = deferred<QuickSearchResponse>();
diff --git a/apps/client/src/widgets/quick_search.tsx b/apps/client/src/widgets/quick_search.tsx
index 47366429a1..c8f53881fa 100644
--- a/apps/client/src/widgets/quick_search.tsx
+++ b/apps/client/src/widgets/quick_search.tsx
@@ -13,6 +13,7 @@ import server from "../services/server";
import type { ShortcutHintDefinition } from "../services/shortcut_hints";
import { isMobile } from "../services/utils";
import ActionButton from "./react/ActionButton";
+import { ExtendedAdmonition } from "./react/Admonition";
import Button from "./react/Button";
import { focusListItem } from "./react/FormList";
import { useTriliumEvent } from "./react/hooks";
@@ -42,7 +43,12 @@ const NAVIGATION_KEYS = new Set([ "ArrowDown", "ArrowUp", "PageDown", "PageUp" ]
type SearchState =
| { status: "searching" }
- | { status: "done", results: SearchResultDetails[], viewScope: ViewScope | undefined };
+ | {
+ status: "done";
+ results: SearchResultDetails[];
+ viewScope: ViewScope | undefined;
+ error: string | null;
+ };
export default function QuickSearch() {
// In a ref rather than state, so typing re-renders nothing; only its emptiness is state.
@@ -76,7 +82,7 @@ export default function QuickSearch() {
setOpen(true);
const requestId = ++requestIdRef.current;
setSearchState({ status: "searching" });
- const { searchResults, highlightedTokens } = await server.get<QuickSearchResponse>(
+ const { searchResults, highlightedTokens, error } = await server.get<QuickSearchResponse>(
`quick-search/${encodeURIComponent(query)}`
);
if (requestId !== requestIdRef.current) return;
@@ -85,7 +91,8 @@ export default function QuickSearch() {
status: "done",
results: searchResults,
// Opens the note at its first match; with nothing highlighted, at the plain path.
- viewScope: highlightedTokens.length ? { searchTerms: highlightedTokens } : undefined
+ viewScope: highlightedTokens.length ? { searchTerms: highlightedTokens } : undefined,
+ error
});
}
@@ -286,18 +293,36 @@ function QuickSearchResults({ searchState, onOpenResult }: {
);
}
+ // What the search ran into while running; the field's linter shows a query that fails to parse
+ // before it is run.
+ const error = searchState.error && (
+ <ExtendedAdmonition
+ type="caution"
+ icon="bx bx-error-circle"
+ title={t("quick-search.error")}
+ className="quick-search-error"
+ >
+ {searchState.error}
+ </ExtendedAdmonition>
+ );
+
if (!searchState.results.length) {
- return <span className="dropdown-item disabled">{t("quick-search.no-results")}</span>;
+ return error || (
+ <span className="dropdown-item disabled">{t("quick-search.no-results")}</span>
+ );
}
- return searchState.results.map((result) => (
- <QuickSearchResult
- key={result.notePath}
- result={result}
- viewScope={searchState.viewScope}
- onOpen={onOpenResult}
- />
- ));
+ return <>
+ {error}
+ {searchState.results.map((result) => (
+ <QuickSearchResult
+ key={result.notePath}
+ result={result}
+ viewScope={searchState.viewScope}
+ onOpen={onOpenResult}
+ />
+ ))}
+ </>;
}
function QuickSearchResult({ result, viewScope, onOpen }: {
diff --git a/apps/client/src/widgets/quick_search_old.ts b/apps/client/src/widgets/quick_search_old.ts
deleted file mode 100644
index 84d176fe7e..0000000000
--- a/apps/client/src/widgets/quick_search_old.ts
+++ /dev/null
@@ -1,61 +0,0 @@
-import type { QuickSearchResponse } from "@triliumnext/commons";
-import { Dropdown, Tooltip } from "bootstrap";
-
-import server from "../services/server.js";
-import { handleRightToLeftPlacement } from "../services/utils.js";
-import BasicWidget from "./basic_widget.js";
-
-const TPL = /*html*/`
-<div class="quick-search input-group input-group-sm">
- <div class="input-group-prepend">
- <button class="btn btn-outline-secondary search-button" type="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
- <span class="bx bx-search"></span>
- </button>
- <div class="dropdown-menu tn-dropdown-list">
- <div class="quick-search-results"></div>
- </div>
- </div>
-</div>`;
-
-export default class QuickSearchWidget extends BasicWidget {
-
- private dropdown!: bootstrap.Dropdown;
- /** The query, set by the field that holds it. */
- searchString = "";
-
- doRender() {
- this.$widget = $(TPL);
-
- this.dropdown = Dropdown.getOrCreateInstance(this.$widget.find("[data-bs-toggle='dropdown']")[0], {
- popperConfig: {
- strategy: "fixed",
- placement: "bottom"
- }
- });
-
- return this.$widget;
- }
-
- async search() {
- const searchString = this.searchString.trim();
-
- if (!searchString) {
- this.dropdown.hide();
- return;
- }
-
- const { error } = await server.get<QuickSearchResponse>(`quick-search/${encodeURIComponent(searchString)}`);
-
- if (error) {
- const tooltip = new Tooltip(this.$widget[0], {
- trigger: "manual",
- title: `Search error: ${error}`,
- placement: handleRightToLeftPlacement("right")
- });
-
- tooltip.show();
-
- setTimeout(() => tooltip.dispose(), 4000);
- }
- }
-}