2025-08-14 19:55:45 +03:00
|
|
|
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
|
2025-08-21 15:50:14 +03:00
|
|
|
import { EventData, EventNames } from "../../components/app_context";
|
2025-08-20 21:50:06 +03:00
|
|
|
import { ParentComponent } from "./react_utils";
|
2025-08-08 23:23:07 +03:00
|
|
|
import SpacedUpdate from "../../services/spaced_update";
|
2025-08-14 17:36:11 +03:00
|
|
|
import { OptionNames } from "@triliumnext/commons";
|
2025-08-14 23:54:32 +03:00
|
|
|
import options, { type OptionValue } from "../../services/options";
|
2025-08-14 18:18:45 +03:00
|
|
|
import utils, { reloadFrontendApp } from "../../services/utils";
|
2025-08-14 21:05:24 +03:00
|
|
|
import Component from "../../components/component";
|
2025-08-20 23:53:13 +03:00
|
|
|
import NoteContext from "../../components/note_context";
|
|
|
|
|
import { ReactWrappedWidget } from "../basic_widget";
|
2025-08-21 10:44:58 +03:00
|
|
|
import FNote from "../../entities/fnote";
|
2025-08-21 15:50:14 +03:00
|
|
|
import attributes from "../../services/attributes";
|
2025-08-14 21:05:24 +03:00
|
|
|
|
|
|
|
|
type TriliumEventHandler<T extends EventNames> = (data: EventData<T>) => void;
|
|
|
|
|
const registeredHandlers: Map<Component, Map<EventNames, TriliumEventHandler<any>[]>> = new Map();
|
2025-08-08 20:08:06 +03:00
|
|
|
|
2025-08-10 15:11:43 +03:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* Under the hood, it works by altering the parent (Trilium) component of the React element to introduce the corresponding event.
|
|
|
|
|
*
|
|
|
|
|
* @param eventName the name of the Trilium event to listen for.
|
|
|
|
|
* @param handler the handler to be invoked when the event is triggered.
|
|
|
|
|
* @param enabled determines whether the event should be listened to or not. Useful to conditionally limit the listener based on a state (e.g. a modal being displayed).
|
|
|
|
|
*/
|
2025-08-14 21:05:24 +03:00
|
|
|
export default function useTriliumEvent<T extends EventNames>(eventName: T, handler: TriliumEventHandler<T>, enabled = true) {
|
2025-08-08 20:08:06 +03:00
|
|
|
const parentWidget = useContext(ParentComponent);
|
2025-08-14 21:05:24 +03:00
|
|
|
if (!parentWidget) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handlerName = `${eventName}Event`;
|
|
|
|
|
const customHandler = useMemo(() => {
|
|
|
|
|
return async (data: EventData<T>) => {
|
|
|
|
|
// Inform the attached event listeners.
|
|
|
|
|
const eventHandlers = registeredHandlers.get(parentWidget)?.get(eventName) ?? [];
|
|
|
|
|
for (const eventHandler of eventHandlers) {
|
|
|
|
|
eventHandler(data);
|
2025-08-08 20:08:06 +03:00
|
|
|
}
|
2025-08-14 21:05:24 +03:00
|
|
|
}
|
|
|
|
|
}, [ eventName, parentWidget ]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// Attach to the list of handlers.
|
|
|
|
|
let handlersByWidget = registeredHandlers.get(parentWidget);
|
|
|
|
|
if (!handlersByWidget) {
|
|
|
|
|
handlersByWidget = new Map();
|
|
|
|
|
registeredHandlers.set(parentWidget, handlersByWidget);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let handlersByWidgetAndEventName = handlersByWidget.get(eventName);
|
|
|
|
|
if (!handlersByWidgetAndEventName) {
|
|
|
|
|
handlersByWidgetAndEventName = [];
|
|
|
|
|
handlersByWidget.set(eventName, handlersByWidgetAndEventName);
|
|
|
|
|
}
|
2025-08-08 20:08:06 +03:00
|
|
|
|
2025-08-14 21:05:24 +03:00
|
|
|
if (!handlersByWidgetAndEventName.includes(handler)) {
|
|
|
|
|
handlersByWidgetAndEventName.push(handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply the custom event handler.
|
|
|
|
|
if (parentWidget[handlerName] && parentWidget[handlerName] !== customHandler) {
|
|
|
|
|
console.warn(`Widget ${parentWidget.componentId} already had an event listener and it was replaced by the React one.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parentWidget[handlerName] = customHandler;
|
|
|
|
|
|
2025-08-08 20:08:06 +03:00
|
|
|
return () => {
|
2025-08-14 21:05:24 +03:00
|
|
|
const eventHandlers = registeredHandlers.get(parentWidget)?.get(eventName);
|
|
|
|
|
if (!eventHandlers || !eventHandlers.includes(handler)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 22:15:47 +03:00
|
|
|
// Remove the event handler from the array.
|
|
|
|
|
const newEventHandlers = eventHandlers.filter(e => e !== handler);
|
|
|
|
|
if (newEventHandlers.length) {
|
|
|
|
|
registeredHandlers.get(parentWidget)?.set(eventName, newEventHandlers);
|
|
|
|
|
} else {
|
|
|
|
|
registeredHandlers.get(parentWidget)?.delete(eventName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!registeredHandlers.get(parentWidget)?.size) {
|
|
|
|
|
registeredHandlers.delete(parentWidget);
|
|
|
|
|
}
|
2025-08-08 20:08:06 +03:00
|
|
|
};
|
2025-08-14 21:05:24 +03:00
|
|
|
}, [ eventName, parentWidget, handler ]);
|
2025-08-08 23:23:07 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-21 13:17:28 +03:00
|
|
|
export function useTriliumEventBeta<T extends EventNames>(eventName: T | T[], handler: TriliumEventHandler<T>) {
|
2025-08-20 23:53:13 +03:00
|
|
|
const parentComponent = useContext(ParentComponent) as ReactWrappedWidget;
|
2025-08-21 10:05:53 +03:00
|
|
|
|
2025-08-21 13:17:28 +03:00
|
|
|
if (Array.isArray(eventName)) {
|
|
|
|
|
for (const eventSingleName of eventName) {
|
|
|
|
|
parentComponent.registerHandler(eventSingleName, handler);
|
|
|
|
|
}
|
|
|
|
|
return (() => {
|
|
|
|
|
for (const eventSingleName of eventName) {
|
|
|
|
|
parentComponent.removeHandler(eventSingleName, handler)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
parentComponent.registerHandler(eventName, handler);
|
|
|
|
|
return (() => parentComponent.removeHandler(eventName, handler));
|
|
|
|
|
}
|
2025-08-20 23:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-08 23:23:07 +03:00
|
|
|
export function useSpacedUpdate(callback: () => Promise<void>, interval = 1000) {
|
2025-08-09 09:15:54 +03:00
|
|
|
const callbackRef = useRef(callback);
|
|
|
|
|
const spacedUpdateRef = useRef<SpacedUpdate>();
|
2025-08-08 23:23:07 +03:00
|
|
|
|
2025-08-09 09:15:54 +03:00
|
|
|
// Update callback ref when it changes
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
callbackRef.current = callback;
|
2025-08-21 13:13:48 +03:00
|
|
|
}, [callback]);
|
2025-08-09 09:15:54 +03:00
|
|
|
|
|
|
|
|
// Create SpacedUpdate instance only once
|
|
|
|
|
if (!spacedUpdateRef.current) {
|
|
|
|
|
spacedUpdateRef.current = new SpacedUpdate(
|
|
|
|
|
() => callbackRef.current(),
|
|
|
|
|
interval
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update interval if it changes
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
spacedUpdateRef.current?.setUpdateInterval(interval);
|
|
|
|
|
}, [interval]);
|
|
|
|
|
|
|
|
|
|
return spacedUpdateRef.current;
|
2025-08-14 17:36:11 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-18 09:34:16 +03:00
|
|
|
/**
|
|
|
|
|
* Allows a React component to read and write a Trilium option, while also watching for external changes.
|
|
|
|
|
*
|
|
|
|
|
* Conceptually, `useTriliumOption` works just like `useState`, but the value is also automatically updated if
|
|
|
|
|
* the option is changed somewhere else in the client.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the option to listen for.
|
|
|
|
|
* @param needsRefresh whether to reload the frontend whenever the value is changed.
|
|
|
|
|
* @returns an array where the first value is the current option value and the second value is the setter.
|
|
|
|
|
*/
|
2025-08-14 23:54:32 +03:00
|
|
|
export function useTriliumOption(name: OptionNames, needsRefresh?: boolean): [string, (newValue: OptionValue) => Promise<void>] {
|
2025-08-14 17:36:11 +03:00
|
|
|
const initialValue = options.get(name);
|
|
|
|
|
const [ value, setValue ] = useState(initialValue);
|
|
|
|
|
|
2025-08-14 19:55:45 +03:00
|
|
|
const wrappedSetValue = useMemo(() => {
|
2025-08-14 23:54:32 +03:00
|
|
|
return async (newValue: OptionValue) => {
|
2025-08-14 19:55:45 +03:00
|
|
|
await options.save(name, newValue);
|
2025-08-14 18:18:45 +03:00
|
|
|
|
2025-08-14 19:55:45 +03:00
|
|
|
if (needsRefresh) {
|
|
|
|
|
reloadFrontendApp(`option change: ${name}`);
|
|
|
|
|
}
|
2025-08-14 18:18:45 +03:00
|
|
|
}
|
2025-08-14 19:55:45 +03:00
|
|
|
}, [ name, needsRefresh ]);
|
2025-08-14 17:47:45 +03:00
|
|
|
|
2025-08-14 19:55:45 +03:00
|
|
|
useTriliumEvent("entitiesReloaded", useCallback(({ loadResults }) => {
|
2025-08-14 17:47:45 +03:00
|
|
|
if (loadResults.getOptionNames().includes(name)) {
|
|
|
|
|
const newValue = options.get(name);
|
|
|
|
|
setValue(newValue);
|
|
|
|
|
}
|
2025-08-14 19:55:45 +03:00
|
|
|
}, [ name ]));
|
2025-08-14 17:47:45 +03:00
|
|
|
|
2025-08-14 17:36:11 +03:00
|
|
|
return [
|
|
|
|
|
value,
|
2025-08-14 17:47:45 +03:00
|
|
|
wrappedSetValue
|
2025-08-14 17:36:11 +03:00
|
|
|
]
|
2025-08-14 17:53:24 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-20 20:34:00 +03:00
|
|
|
/**
|
|
|
|
|
* Similar to {@link useTriliumOption}, but the value is converted to and from a boolean instead of a string.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the option to listen for.
|
|
|
|
|
* @param needsRefresh whether to reload the frontend whenever the value is changed.
|
|
|
|
|
* @returns an array where the first value is the current option value and the second value is the setter.
|
|
|
|
|
*/
|
2025-08-18 09:34:16 +03:00
|
|
|
export function useTriliumOptionBool(name: OptionNames, needsRefresh?: boolean): [boolean, (newValue: boolean) => Promise<void>] {
|
|
|
|
|
const [ value, setValue ] = useTriliumOption(name, needsRefresh);
|
2025-08-14 18:26:22 +03:00
|
|
|
return [
|
|
|
|
|
(value === "true"),
|
|
|
|
|
(newValue) => setValue(newValue ? "true" : "false")
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 20:34:00 +03:00
|
|
|
/**
|
|
|
|
|
* Similar to {@link useTriliumOption}, but the value is converted to and from a int instead of a string.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the option to listen for.
|
|
|
|
|
* @param needsRefresh whether to reload the frontend whenever the value is changed.
|
|
|
|
|
* @returns an array where the first value is the current option value and the second value is the setter.
|
|
|
|
|
*/
|
2025-08-14 23:54:32 +03:00
|
|
|
export function useTriliumOptionInt(name: OptionNames): [number, (newValue: number) => Promise<void>] {
|
|
|
|
|
const [ value, setValue ] = useTriliumOption(name);
|
|
|
|
|
return [
|
|
|
|
|
(parseInt(value, 10)),
|
|
|
|
|
(newValue) => setValue(newValue)
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 20:34:00 +03:00
|
|
|
/**
|
|
|
|
|
* Similar to {@link useTriliumOption}, but the object value is parsed to and from a JSON instead of a string.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the option to listen for.
|
|
|
|
|
* @returns an array where the first value is the current option value and the second value is the setter.
|
|
|
|
|
*/
|
2025-08-15 10:26:25 +03:00
|
|
|
export function useTriliumOptionJson<T>(name: OptionNames): [ T, (newValue: T) => Promise<void> ] {
|
|
|
|
|
const [ value, setValue ] = useTriliumOption(name);
|
|
|
|
|
return [
|
|
|
|
|
(JSON.parse(value) as T),
|
|
|
|
|
(newValue => setValue(JSON.stringify(newValue)))
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 20:34:00 +03:00
|
|
|
/**
|
|
|
|
|
* Similar to {@link useTriliumOption}, but operates with multiple options at once.
|
|
|
|
|
*
|
|
|
|
|
* @param names the name of the option to listen for.
|
|
|
|
|
* @returns an array where the first value is a map where the keys are the option names and the values, and the second value is the setter which takes in the same type of map and saves them all at once.
|
|
|
|
|
*/
|
2025-08-15 11:21:19 +03:00
|
|
|
export function useTriliumOptions<T extends OptionNames>(...names: T[]) {
|
|
|
|
|
const values: Record<string, string> = {};
|
|
|
|
|
for (const name of names) {
|
|
|
|
|
values[name] = options.get(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
values as Record<T, string>,
|
2025-08-18 19:47:40 +03:00
|
|
|
options.saveMany
|
2025-08-15 11:21:19 +03:00
|
|
|
] as const;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 17:53:24 +03:00
|
|
|
/**
|
|
|
|
|
* Generates a unique name via a random alphanumeric string of a fixed length.
|
|
|
|
|
*
|
|
|
|
|
* <p>
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2025-08-19 23:34:25 +03:00
|
|
|
export function useUniqueName(prefix?: string) {
|
|
|
|
|
return useMemo(() => (prefix ? prefix + "-" : "") + utils.randomString(10), [ prefix ]);
|
2025-08-20 23:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useNoteContext() {
|
|
|
|
|
|
|
|
|
|
const [ noteContext, setNoteContext ] = useState<NoteContext>();
|
|
|
|
|
const [ notePath, setNotePath ] = useState<string | null | undefined>();
|
2025-08-21 12:13:30 +03:00
|
|
|
const [ note, setNote ] = useState<FNote | null | undefined>();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setNote(noteContext?.note);
|
|
|
|
|
}, [ notePath ]);
|
2025-08-20 23:53:13 +03:00
|
|
|
|
2025-08-21 09:17:47 +03:00
|
|
|
useTriliumEventBeta("activeContextChanged", ({ noteContext }) => {
|
2025-08-20 23:53:13 +03:00
|
|
|
setNoteContext(noteContext);
|
2025-08-21 12:13:30 +03:00
|
|
|
setNotePath(noteContext.notePath);
|
2025-08-20 23:53:13 +03:00
|
|
|
});
|
|
|
|
|
useTriliumEventBeta("setNoteContext", ({ noteContext }) => {
|
|
|
|
|
setNoteContext(noteContext);
|
|
|
|
|
});
|
2025-08-21 09:17:47 +03:00
|
|
|
useTriliumEventBeta("noteSwitchedAndActivated", ({ noteContext }) => {
|
2025-08-20 23:53:13 +03:00
|
|
|
setNoteContext(noteContext);
|
|
|
|
|
});
|
2025-08-21 09:17:47 +03:00
|
|
|
useTriliumEventBeta("noteSwitched", ({ noteContext, notePath }) => {
|
2025-08-20 23:53:13 +03:00
|
|
|
setNotePath(notePath);
|
|
|
|
|
});
|
2025-08-21 12:13:30 +03:00
|
|
|
useTriliumEventBeta("frocaReloaded", () => {
|
|
|
|
|
setNote(noteContext?.note);
|
|
|
|
|
});
|
2025-08-21 10:05:53 +03:00
|
|
|
|
|
|
|
|
const parentComponent = useContext(ParentComponent) as ReactWrappedWidget;
|
2025-08-20 23:53:13 +03:00
|
|
|
|
|
|
|
|
return {
|
2025-08-21 12:13:30 +03:00
|
|
|
note: note,
|
2025-08-20 23:53:13 +03:00
|
|
|
noteId: noteContext?.note?.noteId,
|
|
|
|
|
notePath: noteContext?.notePath,
|
|
|
|
|
hoistedNoteId: noteContext?.hoistedNoteId,
|
2025-08-21 10:05:53 +03:00
|
|
|
ntxId: noteContext?.ntxId,
|
2025-08-21 11:08:33 +03:00
|
|
|
viewScope: noteContext?.viewScope,
|
|
|
|
|
componentId: parentComponent.componentId,
|
2025-08-21 13:00:05 +03:00
|
|
|
noteContext,
|
|
|
|
|
parentComponent
|
2025-08-20 23:53:13 +03:00
|
|
|
};
|
|
|
|
|
|
2025-08-21 10:44:58 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-21 10:53:59 +03:00
|
|
|
/**
|
|
|
|
|
* Allows a React component to listen to obtain a property of a {@link FNote} while also automatically watching for changes, either via the user changing to a different note or the property being changed externally.
|
|
|
|
|
*
|
|
|
|
|
* @param note the {@link FNote} whose property to obtain.
|
|
|
|
|
* @param property a property of a {@link FNote} to obtain the value from (e.g. `title`, `isProtected`).
|
|
|
|
|
* @param componentId optionally, constricts the refresh of the value if an update occurs externally via the component ID of a legacy widget. This can be used to avoid external data replacing fresher, user-inputted data.
|
|
|
|
|
* @returns the value of the requested property.
|
|
|
|
|
*/
|
|
|
|
|
export function useNoteProperty<T extends keyof FNote>(note: FNote | null | undefined, property: T, componentId?: string) {
|
2025-08-21 10:44:58 +03:00
|
|
|
if (!note) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-08-21 10:53:59 +03:00
|
|
|
|
2025-08-21 10:44:58 +03:00
|
|
|
const [ value, setValue ] = useState<FNote[T]>(note[property]);
|
2025-08-21 12:13:30 +03:00
|
|
|
const refreshValue = () => setValue(note[property]);
|
2025-08-21 10:53:59 +03:00
|
|
|
|
|
|
|
|
// Watch for note changes.
|
2025-08-21 12:13:30 +03:00
|
|
|
useEffect(() => refreshValue(), [ note, note[property] ]);
|
2025-08-21 10:53:59 +03:00
|
|
|
|
|
|
|
|
// Watch for external changes.
|
2025-08-21 10:44:58 +03:00
|
|
|
useTriliumEventBeta("entitiesReloaded", ({ loadResults }) => {
|
2025-08-21 10:53:59 +03:00
|
|
|
if (loadResults.isNoteReloaded(note.noteId, componentId)) {
|
2025-08-21 12:13:30 +03:00
|
|
|
refreshValue();
|
2025-08-21 10:44:58 +03:00
|
|
|
}
|
|
|
|
|
});
|
2025-08-21 11:08:33 +03:00
|
|
|
|
2025-08-21 12:13:30 +03:00
|
|
|
return note[property];
|
2025-08-21 12:55:33 +03:00
|
|
|
}
|
2025-08-21 15:50:14 +03:00
|
|
|
|
2025-08-21 17:16:03 +03:00
|
|
|
export function useNoteLabel(note: FNote | undefined | null, labelName: string): [string | null | undefined, (newValue: string) => void] {
|
2025-08-21 22:19:26 +03:00
|
|
|
const [ labelValue, setLabelValue ] = useState<string | null | undefined>(note?.getLabelValue(labelName));
|
2025-08-21 15:50:14 +03:00
|
|
|
|
2025-08-22 12:14:53 +03:00
|
|
|
useEffect(() => setLabelValue(note?.getLabelValue(labelName) ?? null), [ note ]);
|
2025-08-21 15:50:14 +03:00
|
|
|
useTriliumEventBeta("entitiesReloaded", ({ loadResults }) => {
|
|
|
|
|
for (const attr of loadResults.getAttributeRows()) {
|
|
|
|
|
if (attr.type === "label" && attr.name === labelName && attributes.isAffecting(attr, note)) {
|
2025-08-21 22:19:26 +03:00
|
|
|
setLabelValue(attr.value ?? null);
|
2025-08-21 15:50:14 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const setter = useCallback((value: string | undefined) => {
|
|
|
|
|
if (note) {
|
|
|
|
|
attributes.setLabel(note.noteId, labelName, value)
|
|
|
|
|
}
|
|
|
|
|
}, [note]);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
labelValue,
|
|
|
|
|
setter
|
|
|
|
|
] as const;
|
2025-08-21 22:19:26 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-22 12:14:53 +03:00
|
|
|
export function useNoteLabelBoolean(note: FNote | undefined | null, labelName: string): [ boolean, (newValue: boolean) => void] {
|
|
|
|
|
const [ labelValue, setLabelValue ] = useState<boolean>(!!note?.hasLabel(labelName));
|
|
|
|
|
|
|
|
|
|
useEffect(() => setLabelValue(!!note?.hasLabel(labelName)), [ note ]);
|
2025-08-21 22:19:26 +03:00
|
|
|
|
|
|
|
|
useTriliumEventBeta("entitiesReloaded", ({ loadResults }) => {
|
|
|
|
|
for (const attr of loadResults.getAttributeRows()) {
|
|
|
|
|
if (attr.type === "label" && attr.name === labelName && attributes.isAffecting(attr, note)) {
|
|
|
|
|
setLabelValue(!attr.isDeleted);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const setter = useCallback((value: boolean) => {
|
|
|
|
|
if (note) {
|
|
|
|
|
if (value) {
|
|
|
|
|
attributes.setLabel(note.noteId, labelName, "");
|
|
|
|
|
} else {
|
|
|
|
|
attributes.removeOwnedLabelByName(note, labelName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [note]);
|
|
|
|
|
|
|
|
|
|
return [ labelValue, setter ] as const;
|
2025-08-21 15:50:14 +03:00
|
|
|
}
|