From e1fa188244369851ce5c48ef078f55b2ebef9c0e Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 24 Aug 2025 16:17:10 +0300 Subject: [PATCH] chore(react/ribbon): refresh options --- .../widgets/ribbon/SearchDefinitionTab.tsx | 56 +++++++++++++------ .../ribbon_widgets/search_definition.ts | 6 -- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx b/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx index 45954f1e8..73b3cef3b 100644 --- a/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx +++ b/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx @@ -11,9 +11,9 @@ import { note } from "mermaid/dist/rendering-util/rendering-elements/shapes/note import FNote from "../../entities/fnote"; import toast from "../../services/toast"; import froca from "../../services/froca"; -import { useContext, useRef } from "preact/hooks"; +import { useContext, useEffect, useRef, useState } from "preact/hooks"; import { ParentComponent } from "../react/react_utils"; -import { useSpacedUpdate } from "../react/hooks"; +import { useSpacedUpdate, useTriliumEventBeta } from "../react/hooks"; import appContext from "../../components/app_context"; import server from "../../services/server"; @@ -92,7 +92,25 @@ const SEARCH_OPTIONS: SearchOption[] = [ export default function SearchDefinitionTab({ note, ntxId }: TabContext) { const parentComponent = useContext(ParentComponent); - + const [ searchOptions, setSearchOptions ] = useState<{ availableOptions: SearchOption[], activeOptions: SearchOption[] }>(); + + function refreshOptions() { + if (!note) return; + + const availableOptions: SearchOption[] = []; + const activeOptions: SearchOption[] = []; + + for (const searchOption of SEARCH_OPTIONS) { + const attr = note.getAttribute(searchOption.attributeType, searchOption.attributeName); + if (attr && searchOption.component) { + activeOptions.push(searchOption); + } else { + availableOptions.push(searchOption); + } + } + + setSearchOptions({ availableOptions, activeOptions }); + } async function refreshResults() { const noteId = note?.noteId; @@ -113,6 +131,14 @@ export default function SearchDefinitionTab({ note, ntxId }: TabContext) { parentComponent?.triggerEvent("searchRefreshed", { ntxId }); } + // Refresh the list of available and active options. + useEffect(refreshOptions, [ note ]); + useTriliumEventBeta("entitiesReloaded", ({ loadResults }) => { + if (loadResults.getAttributeRows().find((attrRow) => attributes.isAffecting(attrRow, note))) { + refreshOptions(); + } + }); + return (
@@ -121,7 +147,7 @@ export default function SearchDefinitionTab({ note, ntxId }: TabContext) { {t("search_definition.add_search_option")} - {SEARCH_OPTIONS.map(({ icon, label, tooltip }) => ( + {searchOptions?.availableOptions.map(({ icon, label, tooltip }) => (