Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(react): add type safety for note relations
Elian Doran3ac0dfb2ad00
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex 76ea6077eb..a1f06eeac1 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -2,7 +2,7 @@ import { Inputs, MutableRef, useCallback, useContext, useDebugValue, useEffect,import { CommandListenerData, EventData, EventNames } from "../../components/app_context";import { ParentComponent } from "./react_utils";import SpacedUpdate from "../../services/spaced_update";-import { FilterLabelsByType, KeyboardActionNames, OptionNames } from "@triliumnext/commons";+import { FilterLabelsByType, KeyboardActionNames, OptionNames, RelationNames } from "@triliumnext/commons";import options, { type OptionValue } from "../../services/options";import utils, { escapeRegExp, reloadFrontendApp } from "../../services/utils";import NoteContext from "../../components/note_context";@@ -258,7 +258,7 @@ export function useNoteProperty<T extends keyof FNote>(note: FNote | null | undereturn note?.[property];}-export function useNoteRelation(note: FNote | undefined | null, relationName: string): [string | null | undefined, (newValue: string) => void] {+export function useNoteRelation(note: FNote | undefined | null, relationName: RelationNames): [string | null | undefined, (newValue: string) => void] {const [ relationValue, setRelationValue ] = useState<string | null | undefined>(note?.getRelationValue(relationName));useEffect(() => setRelationValue(note?.getRelationValue(relationName) ?? null), [ note ]);diff --git a/packages/commons/src/lib/attribute_names.ts b/packages/commons/src/lib/attribute_names.tsindex 9494d7b925..8b8de89c10 100644--- a/packages/commons/src/lib/attribute_names.ts+++ b/packages/commons/src/lib/attribute_names.ts@@ -1,3 +1,6 @@+/**+ * A listing of all the labels used by the system (i.e. not user-defined). Labels defined here have a data type which is not enforced, but offers type safety.+ */type Labels = {color: string;iconClass: string;@@ -38,7 +41,17 @@ type Labels = {includeArchived: boolean;}+/**+ * A listing of all relations used by the system (i.e. not user-defined). Unlike labels, relations+ * always point to a note ID, so no specific data type is necessary.+ */+type Relations = [+ "searchScript",+ "ancestor"+];+export type LabelNames = keyof Labels;+export type RelationNames = Relations[number];export type FilterLabelsByType<U> = {[K in keyof Labels]: Labels[K] extends U ? K : never;