diff --git a/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx b/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx index 21d7dcdc7..72c07dfdc 100644 --- a/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx +++ b/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx @@ -6,7 +6,7 @@ import attributes from "../../services/attributes"; import FNote from "../../entities/fnote"; import toast from "../../services/toast"; import froca from "../../services/froca"; -import { useContext, useEffect, useState } from "preact/hooks"; +import { useContext, useEffect, useMemo, useState } from "preact/hooks"; import { ParentComponent } from "../react/react_utils"; import { useTriliumEvent } from "../react/hooks"; import appContext from "../../components/app_context"; @@ -73,24 +73,29 @@ export default function SearchDefinitionTab({ note, ntxId, hidden }: TabContext) } }); + const executionState = useMemo(() => { + const AUTO_EXEC_KEY = "lastAutoExecutedSearchNote"; + return { + load: () => sessionStorage.getItem(AUTO_EXEC_KEY), + save: (noteId: string) => sessionStorage.setItem(AUTO_EXEC_KEY, noteId), + }; + }, []); + useEffect(() => { async function autoExecute() { - if (note?.type !== 'search' || !note?.hasLabel('autoExecuteSearch')) { + if (!note || note?.type !== "search" || !note?.hasLabel("autoExecuteSearch")) { + executionState.save(""); return; } - console.log('Executing search'); + const lastExecutedNoteId = executionState.load(); + if (lastExecutedNoteId !== note.noteId) { + executionState.save(note.noteId); - // Only execute if no results exist yet - await refreshResults(); + await refreshResults(); - console.log('Executed search'); - - const hasResults = note.children && note.children.length > 0; - - if (hasResults) { - parentComponent?.triggerCommand('toggleRibbonTabBookProperties', { ntxId }); - } + parentComponent?.triggerCommand("toggleRibbonTabBookProperties", {}); + } } autoExecute(); }, [note]);