Skip to content

Build your personal knowledge base with Trilium Notes

refactor(react/settings): use better option mechanism

Elian Doranda92255dd687
diff --git a/apps/client/src/services/options.ts b/apps/client/src/services/options.ts
index c1590721f7..9a97153d0b 100644
--- a/apps/client/src/services/options.ts
+++ b/apps/client/src/services/options.ts
@@ -1,7 +1,7 @@
import server from "./server.js";
import { isShare } from "./utils.js";
-type OptionValue = number | string;
+export type OptionValue = number | string;
class Options {
initializedPromise: Promise<void>;
diff --git a/apps/client/src/widgets/react/FormSelect.tsx b/apps/client/src/widgets/react/FormSelect.tsx
index f068adcda1..ce42c4815d 100644
--- a/apps/client/src/widgets/react/FormSelect.tsx
+++ b/apps/client/src/widgets/react/FormSelect.tsx
@@ -65,7 +65,7 @@ function FormSelectGroup<T>({ values, keyProperty, titleProperty, currentValue }
return (
<option
value={item[keyProperty] as any}
- selected={item[keyProperty] == currentValue} // triple equal is intentionally not used here, for comparisons with numeric values
+ selected={item[keyProperty] === currentValue}
>
{item[titleProperty ?? keyProperty] ?? item[keyProperty] as any}
</option>
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 1c6707a674..04ae86ef2c 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -3,7 +3,7 @@ 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 options, { type OptionValue } from "../../services/options";
import utils, { reloadFrontendApp } from "../../services/utils";
import Component from "../../components/component";
@@ -99,12 +99,12 @@ export function useSpacedUpdate(callback: () => Promise<void>, interval = 1000)
return spacedUpdateRef.current;
}
-export function useTriliumOption(name: OptionNames, needsRefresh?: boolean): [string, (newValue: string) => Promise<void>] {
+export function useTriliumOption(name: OptionNames, needsRefresh?: boolean): [string, (newValue: OptionValue) => Promise<void>] {
const initialValue = options.get(name);
const [ value, setValue ] = useState(initialValue);
const wrappedSetValue = useMemo(() => {
- return async (newValue: string) => {
+ return async (newValue: OptionValue) => {
await options.save(name, newValue);
if (needsRefresh) {
@@ -134,6 +134,14 @@ export function useTriliumOptionBool(name: OptionNames): [boolean, (newValue: bo
]
}
+export function useTriliumOptionInt(name: OptionNames): [number, (newValue: number) => Promise<void>] {
+ const [ value, setValue ] = useTriliumOption(name);
+ return [
+ (parseInt(value, 10)),
+ (newValue) => setValue(newValue)
+ ]
+}
+
/**
* Generates a unique name via a random alphanumeric string of a fixed length.
*
diff --git a/apps/client/src/widgets/type_widgets/options/i18n.tsx b/apps/client/src/widgets/type_widgets/options/i18n.tsx
index 95ea8af75c..c965d8e7ea 100644
--- a/apps/client/src/widgets/type_widgets/options/i18n.tsx
+++ b/apps/client/src/widgets/type_widgets/options/i18n.tsx
@@ -3,7 +3,7 @@ import { getAvailableLocales, t } from "../../../services/i18n";
import FormSelect from "../../react/FormSelect";
import OptionsRow from "./components/OptionsRow";
import OptionsSection from "./components/OptionsSection";
-import { useTriliumOption } from "../../react/hooks";
+import { useTriliumOption, useTriliumOptionInt } from "../../react/hooks";
import type { Locale } from "@triliumnext/commons";
import { isElectron } from "../../../services/utils";
import FormRadioGroup from "../../react/FormRadioGroup";
@@ -54,7 +54,7 @@ function LocaleSelector({ locales, currentValue, onChange }: { locales: Locale[]
function DateSettings() {
const [ firstDayOfWeek, setFirstDayOfWeek ] = useTriliumOption("firstDayOfWeek");
const [ firstWeekOfYear, setFirstWeekOfYear ] = useTriliumOption("firstWeekOfYear");
- const [ minDaysInFirstWeek, setMinDaysInFirstWeek ] = useTriliumOption("minDaysInFirstWeek");
+ const [ minDaysInFirstWeek, setMinDaysInFirstWeek ] = useTriliumOptionInt("minDaysInFirstWeek");
return (
<>
diff --git a/apps/client/src/widgets/type_widgets/options/i18n/i18n.ts b/apps/client/src/widgets/type_widgets/options/i18n/i18n.ts
index 53e509462b..0106ae2273 100644
--- a/apps/client/src/widgets/type_widgets/options/i18n/i18n.ts
+++ b/apps/client/src/widgets/type_widgets/options/i18n/i18n.ts
@@ -67,20 +67,8 @@ const TPL = /*html*/`
export default class LocalizationOptions extends OptionsWidget {
- private $formattingLocaleSelect!: JQuery<HTMLElement>;
- private $minDaysRow!: JQuery<HTMLElement>;
-
doRender() {
this.$widget = $(TPL);
-
- this.$minDaysRow = this.$widget.find(".min-days-row");
-
- this.$widget.find('input[name="first-week-of-year"]').on('change', (e) => {
- const target = e.target as HTMLInputElement;
- const value = parseInt(target.value);
- this.updateOption("firstWeekOfYear", value);
- });
-
this.$widget.find(".restart-app-button").on("click", utils.restartDesktopApp);
}