From 25527ecc21938f1bdd0a9864144f3696b5f803e2 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 14 Aug 2025 17:53:24 +0300 Subject: [PATCH] fix(react/settings): not working properly when side-by-side in split --- .../client/src/widgets/react/FormRadioGroup.tsx | 3 ++- apps/client/src/widgets/react/hooks.tsx | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/apps/client/src/widgets/react/FormRadioGroup.tsx b/apps/client/src/widgets/react/FormRadioGroup.tsx index 600ed8d5e..faa5279e8 100644 --- a/apps/client/src/widgets/react/FormRadioGroup.tsx +++ b/apps/client/src/widgets/react/FormRadioGroup.tsx @@ -1,4 +1,5 @@ import type { ComponentChildren } from "preact"; +import { useUniqueName } from "./hooks"; interface FormRadioProps { name: string; @@ -19,7 +20,7 @@ export default function FormRadioGroup({ name, values, currentValue, onChange }: onChange((e.target as HTMLInputElement).value)} /> diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx index 885049b9c..263ea313f 100644 --- a/apps/client/src/widgets/react/hooks.tsx +++ b/apps/client/src/widgets/react/hooks.tsx @@ -1,9 +1,10 @@ -import { type Dispatch, type StateUpdater, useContext, useEffect, useRef, useState } from "preact/hooks"; +import { type Dispatch, type StateUpdater, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks"; import { EventData, EventNames } from "../../components/app_context"; import { ParentComponent } from "./ReactBasicWidget"; import SpacedUpdate from "../../services/spaced_update"; import { OptionNames } from "@triliumnext/commons"; import options from "../../services/options"; +import utils from "../../services/utils"; /** * Allows a React component to react to Trilium events (e.g. `entitiesReloaded`). When the desired event is triggered, the handler is invoked with the event parameters. @@ -78,7 +79,6 @@ export function useTriliumOption(name: OptionNames): [string, (newValue: string) useTriliumEvent("entitiesReloaded", ({ loadResults }) => { if (loadResults.getOptionNames().includes(name)) { const newValue = options.get(name); - console.log("Got entities reloaded for option ", name, "value", newValue); setValue(newValue); } }); @@ -87,4 +87,17 @@ export function useTriliumOption(name: OptionNames): [string, (newValue: string) value, wrappedSetValue ] +} + +/** + * Generates a unique name via a random alphanumeric string of a fixed length. + * + *

+ * Generally used to assign names to inputs that are unique, especially useful for widgets inside tabs. + * + * @param prefix a prefix to add to the unique name. + * @returns a name with the given prefix and a random alpanumeric string appended to it. + */ +export function useUniqueName(prefix: string) { + return useMemo(() => prefix + utils.randomString(10), [ prefix]); } \ No newline at end of file