Skip to content

Build your personal knowledge base with Trilium Notes

chore(client): fit the saved-search run to the line limit

Elian Doran75baf4df1992
diff --git a/apps/client/src/widgets/NoteDetail.tsx b/apps/client/src/widgets/NoteDetail.tsx
index def48ee147..a988cf4375 100644
--- a/apps/client/src/widgets/NoteDetail.tsx
+++ b/apps/client/src/widgets/NoteDetail.tsx
@@ -19,7 +19,13 @@ import toast from "../services/toast.js";
import { isElectron } from "../services/utils";
import { ExtendedNoteType, TYPE_MAPPINGS, TypeWidget } from "./note_types";
import Button from "./react/Button";
-import { useDelayedVisibility, useGetContextDataFrom, useHasTabBeenShown, useNoteContext, useTriliumEvent } from "./react/hooks";
+import {
+ useDelayedVisibility,
+ useGetContextDataFrom,
+ useHasTabBeenShown,
+ useNoteContext,
+ useTriliumEvent
+} from "./react/hooks";
import Icon from "./react/Icon";
import NoItems from "./react/NoItems";
import { NoteListWithLinks } from "./react/NoteList";
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index a71129e8e8..1860cb8e47 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -525,10 +525,10 @@ function findClosestNoteContext(component: Component | null): NoteContext | unde
}
/**
- * Whether the tab holding `noteContext` has been shown since the component mounted. A tab restored or
- * opened in the background reads `false` until the user switches to it. Every split of a tab is shown
- * with it, so the check is keyed on the tab's main context. Special contexts (an ntxId starting with
- * "_", such as the popup editor) always read `true`.
+ * Whether the tab holding `noteContext` has been shown since the component mounted. A tab
+ * restored or opened in the background reads `false` until the user switches to it. Every split
+ * of a tab is shown with it, so the check is keyed on the tab's main context. Special contexts (an
+ * ntxId starting with "_", such as the popup editor) always read `true`.
*/
export function useHasTabBeenShown(noteContext: NoteContext | undefined) {
const isShown = () => (noteContext?.ntxId?.startsWith("_") ?? false)
@@ -550,12 +550,15 @@ export function useHasTabBeenShown(noteContext: NoteContext | undefined) {
}
/**
- * True when the given context belongs to the active tab, identified by `activeMainNtxId` (the ntxId of
- * the active tab's main context). A tab can hold several splits, but only one of them is the "active"
- * context at a time; every split of the active tab is visible and must load eagerly, so the deferral
- * check is keyed on the tab (the main context), not the individual split.
+ * True when the given context belongs to the active tab, identified by `activeMainNtxId` (the
+ * ntxId of the active tab's main context). A tab can hold several splits, but only one of them is
+ * the "active" context at a time; every split of the active tab is visible and must load eagerly,
+ * so the deferral check is keyed on the tab (the main context), not the individual split.
*/
-export function isContextInActiveTab(noteContext: NoteContext | undefined, activeMainNtxId: string | null | undefined): boolean {
+export function isContextInActiveTab(
+ noteContext: NoteContext | undefined,
+ activeMainNtxId: string | null | undefined
+): boolean {
if (!noteContext || activeMainNtxId == null) {
return false;
}
diff --git a/apps/client/src/widgets/search_result.spec.tsx b/apps/client/src/widgets/search_result.spec.tsx
index 1518ea5015..250873d976 100644
--- a/apps/client/src/widgets/search_result.spec.tsx
+++ b/apps/client/src/widgets/search_result.spec.tsx
@@ -50,7 +50,8 @@ beforeEach(() => {
shownTab.activeMainNtxId = "ntx1";
// Client tests run without app start-up, which is what creates the tab manager.
appContext.tabManager = {
- getActiveMainContext: () => (shownTab.activeMainNtxId ? { ntxId: shownTab.activeMainNtxId } : null)
+ getActiveMainContext: () =>
+ (shownTab.activeMainNtxId ? { ntxId: shownTab.activeMainNtxId } : null)
} as never;
window.glob.TRILIUM_SAFE_MODE = false;
parent = new Component();
@@ -149,12 +150,14 @@ describe("SearchResult", () => {
});
describe("running a saved search when it is shown", () => {
- it("runs a saved search shown in the active tab, showing progress until it answers", async () => {
- const savedSearch = buildNote({ id: "autoSearch", title: "Auto", type: "search", "#searchString": "#book" });
+ it("runs a search shown in the active tab and shows progress until it answers", async () => {
+ const savedSearch = searchNote("autoSearch", "#book");
const result = buildNote({ id: "autoResult", title: "Auto result" });
let answer: (value: unknown) => void = () => {};
- const get = vi.spyOn(server, "get").mockReturnValue(new Promise((resolve) => { answer = resolve; }) as never);
- vi.spyOn(appContext, "triggerEvent").mockImplementation(async (name, data) => parent.handleEvent(name, data));
+ const pending = new Promise((resolve) => { answer = resolve; });
+ const get = vi.spyOn(server, "get").mockReturnValue(pending as never);
+ vi.spyOn(appContext, "triggerEvent")
+ .mockImplementation(async (name, data) => parent.handleEvent(name, data));
shownNote.current = savedSearch;
await mount();
@@ -163,14 +166,18 @@ describe("running a saved search when it is shown", () => {
expect(container.querySelector(".no-items .bx-loader-alt")).not.toBeNull();
expect(container.querySelector("button")).toBeNull();
- await settle(() => answer({ searchResultNoteIds: [ result.noteId ], highlightedTokens: [], error: null }));
+ await settle(() => answer({
+ searchResultNoteIds: [ result.noteId ],
+ highlightedTokens: [],
+ error: null
+ }));
expect(container.querySelector(".no-items")).toBeNull();
expect(container.textContent).toContain("Auto result");
});
- it("keeps showing progress for a search when an earlier one in the same tab finishes first", async () => {
- const first = buildNote({ id: "firstSearch", title: "First", type: "search", "#searchString": "#a" });
- const second = buildNote({ id: "secondSearch", title: "Second", type: "search", "#searchString": "#b" });
+ it("keeps a search's progress when an earlier one in the same tab finishes first", async () => {
+ const first = searchNote("firstSearch", "#a");
+ const second = searchNote("secondSearch", "#b");
const answers: Record<string, () => void> = {};
vi.spyOn(froca, "loadSearchNote").mockImplementation((noteId) =>
new Promise((resolve) => { answers[noteId] = () => resolve(undefined); }));
@@ -188,7 +195,7 @@ describe("running a saved search when it is shown", () => {
});
it("waits for a background tab to be shown before running its saved search", async () => {
- const savedSearch = buildNote({ id: "backgroundSearch", title: "Background", type: "search", "#searchString": "#book" });
+ const savedSearch = searchNote("backgroundSearch", "#book");
const loadSearchNote = vi.spyOn(froca, "loadSearchNote").mockResolvedValue(undefined);
shownTab.activeMainNtxId = "otherTab";
@@ -204,8 +211,9 @@ describe("running a saved search when it is shown", () => {
});
it("leaves the search to \"Search now\" in safe mode and after a failed request", async () => {
- const safeSearch = buildNote({ id: "safeSearch", title: "Safe", type: "search", "#searchString": "#book" });
- const loadSearchNote = vi.spyOn(froca, "loadSearchNote").mockRejectedValue(new Error("Network down"));
+ const safeSearch = searchNote("safeSearch", "#book");
+ const loadSearchNote = vi.spyOn(froca, "loadSearchNote")
+ .mockRejectedValue(new Error("Network down"));
vi.spyOn(toast, "showError").mockImplementation(() => {});
window.glob.TRILIUM_SAFE_MODE = true;
@@ -216,7 +224,7 @@ describe("running a saved search when it is shown", () => {
render(null, container);
window.glob.TRILIUM_SAFE_MODE = false;
- const failingSearch = buildNote({ id: "failingSearch", title: "Failing", type: "search", "#searchString": "#book" });
+ const failingSearch = searchNote("failingSearch", "#book");
shownNote.current = failingSearch;
await mount();
@@ -237,6 +245,10 @@ describe("running a saved search when it is shown", () => {
});
});
+function searchNote(id: string, searchString: string) {
+ return buildNote({ id, title: id, type: "search", "#searchString": searchString });
+}
+
async function mount() {
await settle(() => {
render(<ParentComponent.Provider value={parent}><SearchResult /></ParentComponent.Provider>, container);
diff --git a/apps/client/src/widgets/search_result.tsx b/apps/client/src/widgets/search_result.tsx
index ca67ddf3da..881a1019ac 100644
--- a/apps/client/src/widgets/search_result.tsx
+++ b/apps/client/src/widgets/search_result.tsx
@@ -25,11 +25,11 @@ export default function SearchResult() {
const viewType = useNoteViewType(note);
const [ , setRefreshCount ] = useState(0);
const state = getSearchResultState(note);
- const { isSearching, runSearch } = useSavedSearchRun(note, state === SearchResultState.NOT_EXECUTED, {
- noteContext,
- ntxId,
- parentComponent
- });
+ const { isSearching, runSearch } = useSavedSearchRun(
+ note,
+ state === SearchResultState.NOT_EXECUTED,
+ { noteContext, ntxId, parentComponent }
+ );
const highlightedTokens = note?.highlightedTokenInfos ?? note?.highlightedTokens;
// The search note is updated in place, so a re-render picks up the new results.
@@ -89,16 +89,21 @@ export default function SearchResult() {
);
}
+type SearchRunContext = Pick<
+ ReturnType<typeof useNoteContext>,
+ "noteContext" | "ntxId" | "parentComponent"
+>;
+
/**
- * Runs the shown saved search through `search.runSearchNote()`, once each time it is shown in a visible
- * tab, and reports while a run is in progress. A failed request therefore waits for `runSearch()` ("Search
- * now") or the next showing. Safe mode runs nothing on its own, so a hanging search cannot hang every
- * start-up.
+ * Runs a saved search that has no results yet, through `search.runSearchNote()`, when it is
+ * shown in a visible tab, and reports while a run is in progress. A failed request waits for
+ * `runSearch()` ("Search now") or the next showing. Safe mode runs nothing on its own, so a
+ * hanging search cannot hang every start-up.
*/
function useSavedSearchRun(
note: FNote | null | undefined,
hasNotRun: boolean,
- { noteContext, ntxId, parentComponent }: Pick<ReturnType<typeof useNoteContext>, "noteContext" | "ntxId" | "parentComponent">
+ { noteContext, ntxId, parentComponent }: SearchRunContext
) {
const hasTabBeenShown = useHasTabBeenShown(noteContext);
const [ runningNoteId, setRunningNoteId ] = useState<string>();