From 5a15024e59dedc98e1f2f6a193ae072c1724e36f Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 23 Sep 2025 20:24:40 +0300 Subject: [PATCH] refactor(client): use type safety for option names --- apps/client/src/services/froca_updater.ts | 6 +++--- apps/client/src/services/load_results.ts | 8 ++++---- apps/client/src/services/options.ts | 14 +++++++------- apps/client/src/widgets/llm_chat/validation.ts | 4 ++-- .../src/widgets/type_widgets/options/shortcuts.tsx | 10 +++++----- packages/commons/src/lib/options_interface.ts | 2 ++ 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/apps/client/src/services/froca_updater.ts b/apps/client/src/services/froca_updater.ts index 50826b5a5..6d6ef9213 100644 --- a/apps/client/src/services/froca_updater.ts +++ b/apps/client/src/services/froca_updater.ts @@ -8,6 +8,7 @@ import FAttribute, { type FAttributeRow } from "../entities/fattribute.js"; import FAttachment, { type FAttachmentRow } from "../entities/fattachment.js"; import type { default as FNote, FNoteRow } from "../entities/fnote.js"; import type { EntityChange } from "../server_types.js"; +import type { OptionNames } from "@triliumnext/commons"; async function processEntityChanges(entityChanges: EntityChange[]) { const loadResults = new LoadResults(entityChanges); @@ -30,9 +31,8 @@ async function processEntityChanges(entityChanges: EntityChange[]) { continue; // only noise } - options.set(attributeEntity.name, attributeEntity.value); - - loadResults.addOption(attributeEntity.name); + options.set(attributeEntity.name as OptionNames, attributeEntity.value); + loadResults.addOption(attributeEntity.name as OptionNames); } else if (ec.entityName === "attachments") { processAttachment(loadResults, ec); } else if (ec.entityName === "blobs") { diff --git a/apps/client/src/services/load_results.ts b/apps/client/src/services/load_results.ts index ef70b76cd..4a4875725 100644 --- a/apps/client/src/services/load_results.ts +++ b/apps/client/src/services/load_results.ts @@ -1,4 +1,4 @@ -import type { AttachmentRow, EtapiTokenRow } from "@triliumnext/commons"; +import type { AttachmentRow, EtapiTokenRow, OptionNames } from "@triliumnext/commons"; import type { AttributeType } from "../entities/fattribute.js"; import type { EntityChange } from "../server_types.js"; @@ -67,7 +67,7 @@ export default class LoadResults { private revisionRows: RevisionRow[]; private noteReorderings: string[]; private contentNoteIdToComponentId: ContentNoteIdToComponentIdRow[]; - private optionNames: string[]; + private optionNames: OptionNames[]; private attachmentRows: AttachmentRow[]; public hasEtapiTokenChanges: boolean = false; @@ -180,11 +180,11 @@ export default class LoadResults { return this.contentNoteIdToComponentId.find((l) => l.noteId === noteId && l.componentId !== componentId); } - addOption(name: string) { + addOption(name: OptionNames) { this.optionNames.push(name); } - isOptionReloaded(name: string) { + isOptionReloaded(name: OptionNames) { return this.optionNames.includes(name); } diff --git a/apps/client/src/services/options.ts b/apps/client/src/services/options.ts index 39bbccb23..06b7cde47 100644 --- a/apps/client/src/services/options.ts +++ b/apps/client/src/services/options.ts @@ -20,7 +20,7 @@ class Options { this.arr = arr; } - get(key: string) { + get(key: OptionNames) { return this.arr?.[key] as string; } @@ -40,7 +40,7 @@ class Options { } } - getInt(key: string) { + getInt(key: OptionNames) { const value = this.arr?.[key]; if (typeof value === "number") { return value; @@ -52,7 +52,7 @@ class Options { return null; } - getFloat(key: string) { + getFloat(key: OptionNames) { const value = this.arr?.[key]; if (typeof value !== "string") { return null; @@ -60,15 +60,15 @@ class Options { return parseFloat(value); } - is(key: string) { + is(key: OptionNames) { return this.arr[key] === "true"; } - set(key: string, value: OptionValue) { + set(key: OptionNames, value: OptionValue) { this.arr[key] = value; } - async save(key: string, value: OptionValue) { + async save(key: OptionNames, value: OptionValue) { this.set(key, value); const payload: Record = {}; @@ -85,7 +85,7 @@ class Options { await server.put("options", newValues); } - async toggle(key: string) { + async toggle(key: OptionNames) { await this.save(key, (!this.is(key)).toString()); } } diff --git a/apps/client/src/widgets/llm_chat/validation.ts b/apps/client/src/widgets/llm_chat/validation.ts index 67a3bbcdc..2d287c401 100644 --- a/apps/client/src/widgets/llm_chat/validation.ts +++ b/apps/client/src/widgets/llm_chat/validation.ts @@ -34,10 +34,10 @@ export async function validateProviders(validationWarning: HTMLElement): Promise precedenceList = [precedenceStr]; } } - + // Check for configuration issues with providers in the precedence list const configIssues: string[] = []; - + // Always add experimental warning as the first item configIssues.push(t("ai_llm.experimental_warning")); diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx index 9094d0a00..33a74e2df 100644 --- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx +++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx @@ -14,9 +14,9 @@ import dialog from "../../../services/dialog"; import { useTriliumEvent } from "../../react/hooks"; export default function ShortcutSettings() { - const [ keyboardShortcuts, setKeyboardShortcuts ] = useState([]); + const [ keyboardShortcuts, setKeyboardShortcuts ] = useState([]); const [ filter, setFilter ] = useState(); - + useEffect(() => { server.get("keyboard-actions").then(setKeyboardShortcuts); }, []) @@ -82,7 +82,7 @@ export default function ShortcutSettings() { - setFilter(value.toLowerCase())} /> @@ -136,7 +136,7 @@ function KeyboardShortcutTable({ filter, keyboardShortcuts }: { filter?: string, }}> {action.separator} - ) : ( (!filter || filterKeyboardAction(action, filter)) && + ) : ( (!filter || filterKeyboardAction(action, filter)) && <> {action.friendlyName} @@ -181,4 +181,4 @@ function getOptionName(actionName: string) { function getActionNameFromOptionName(optionName: string) { return optionName.at(PREFIX.length)?.toLowerCase() + optionName.substring(PREFIX.length + 1); -} \ No newline at end of file +} diff --git a/packages/commons/src/lib/options_interface.ts b/packages/commons/src/lib/options_interface.ts index 78e0cc609..7671d4315 100644 --- a/packages/commons/src/lib/options_interface.ts +++ b/packages/commons/src/lib/options_interface.ts @@ -100,6 +100,7 @@ export interface OptionDefinitions extends KeyboardShortcutsOptions