mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 19:05:59 +01:00
refactor(react): add debug information for devtools
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
|
import { useCallback, useContext, useDebugValue, useEffect, useMemo, useRef, useState } from "preact/hooks";
|
||||||
import { EventData, EventNames } from "../../components/app_context";
|
import { EventData, EventNames } from "../../components/app_context";
|
||||||
import { ParentComponent } from "./react_utils";
|
import { ParentComponent } from "./react_utils";
|
||||||
import SpacedUpdate from "../../services/spaced_update";
|
import SpacedUpdate from "../../services/spaced_update";
|
||||||
@@ -18,12 +18,14 @@ import { CSSProperties } from "preact/compat";
|
|||||||
export function useTriliumEvent<T extends EventNames>(eventName: T, handler: (data: EventData<T>) => void) {
|
export function useTriliumEvent<T extends EventNames>(eventName: T, handler: (data: EventData<T>) => void) {
|
||||||
const parentComponent = useContext(ParentComponent)!;
|
const parentComponent = useContext(ParentComponent)!;
|
||||||
parentComponent.registerHandler(eventName, handler);
|
parentComponent.registerHandler(eventName, handler);
|
||||||
|
useDebugValue(eventName);
|
||||||
return (() => parentComponent.removeHandler(eventName, handler));
|
return (() => parentComponent.removeHandler(eventName, handler));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useTriliumEvents<T extends EventNames>(eventNames: T[], handler: (data: EventData<T>, eventName: T) => void) {
|
export function useTriliumEvents<T extends EventNames>(eventNames: T[], handler: (data: EventData<T>, eventName: T) => void) {
|
||||||
const parentComponent = useContext(ParentComponent)!;
|
const parentComponent = useContext(ParentComponent)!;
|
||||||
const handlers: ({ eventName: T, callback: (data: EventData<T>) => void })[] = [];
|
const handlers: ({ eventName: T, callback: (data: EventData<T>) => void })[] = [];
|
||||||
|
useDebugValue(() => eventNames.join(", "));
|
||||||
|
|
||||||
for (const eventName of eventNames) {
|
for (const eventName of eventNames) {
|
||||||
handlers.push({ eventName, callback: (data) => {
|
handlers.push({ eventName, callback: (data) => {
|
||||||
@@ -98,6 +100,8 @@ export function useTriliumOption(name: OptionNames, needsRefresh?: boolean): [st
|
|||||||
}
|
}
|
||||||
}, [ name ]));
|
}, [ name ]));
|
||||||
|
|
||||||
|
useDebugValue(name);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
value,
|
value,
|
||||||
wrappedSetValue
|
wrappedSetValue
|
||||||
@@ -113,6 +117,7 @@ export function useTriliumOption(name: OptionNames, needsRefresh?: boolean): [st
|
|||||||
*/
|
*/
|
||||||
export function useTriliumOptionBool(name: OptionNames, needsRefresh?: boolean): [boolean, (newValue: boolean) => Promise<void>] {
|
export function useTriliumOptionBool(name: OptionNames, needsRefresh?: boolean): [boolean, (newValue: boolean) => Promise<void>] {
|
||||||
const [ value, setValue ] = useTriliumOption(name, needsRefresh);
|
const [ value, setValue ] = useTriliumOption(name, needsRefresh);
|
||||||
|
useDebugValue(name);
|
||||||
return [
|
return [
|
||||||
(value === "true"),
|
(value === "true"),
|
||||||
(newValue) => setValue(newValue ? "true" : "false")
|
(newValue) => setValue(newValue ? "true" : "false")
|
||||||
@@ -128,6 +133,7 @@ export function useTriliumOptionBool(name: OptionNames, needsRefresh?: boolean):
|
|||||||
*/
|
*/
|
||||||
export function useTriliumOptionInt(name: OptionNames): [number, (newValue: number) => Promise<void>] {
|
export function useTriliumOptionInt(name: OptionNames): [number, (newValue: number) => Promise<void>] {
|
||||||
const [ value, setValue ] = useTriliumOption(name);
|
const [ value, setValue ] = useTriliumOption(name);
|
||||||
|
useDebugValue(name);
|
||||||
return [
|
return [
|
||||||
(parseInt(value, 10)),
|
(parseInt(value, 10)),
|
||||||
(newValue) => setValue(newValue)
|
(newValue) => setValue(newValue)
|
||||||
@@ -142,6 +148,7 @@ export function useTriliumOptionInt(name: OptionNames): [number, (newValue: numb
|
|||||||
*/
|
*/
|
||||||
export function useTriliumOptionJson<T>(name: OptionNames): [ T, (newValue: T) => Promise<void> ] {
|
export function useTriliumOptionJson<T>(name: OptionNames): [ T, (newValue: T) => Promise<void> ] {
|
||||||
const [ value, setValue ] = useTriliumOption(name);
|
const [ value, setValue ] = useTriliumOption(name);
|
||||||
|
useDebugValue(name);
|
||||||
return [
|
return [
|
||||||
(JSON.parse(value) as T),
|
(JSON.parse(value) as T),
|
||||||
(newValue => setValue(JSON.stringify(newValue)))
|
(newValue => setValue(JSON.stringify(newValue)))
|
||||||
@@ -160,6 +167,8 @@ export function useTriliumOptions<T extends OptionNames>(...names: T[]) {
|
|||||||
values[name] = options.get(name);
|
values[name] = options.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useDebugValue(() => names.join(", "));
|
||||||
|
|
||||||
return [
|
return [
|
||||||
values as Record<T, string>,
|
values as Record<T, string>,
|
||||||
options.saveMany
|
options.saveMany
|
||||||
@@ -206,6 +215,8 @@ export function useNoteContext() {
|
|||||||
setNote(noteContext?.note);
|
setNote(noteContext?.note);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useDebugValue(() => `notePath=${notePath}, ntxId=${noteContext?.ntxId}`);
|
||||||
|
|
||||||
const parentComponent = useContext(ParentComponent) as ReactWrappedWidget;
|
const parentComponent = useContext(ParentComponent) as ReactWrappedWidget;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -248,6 +259,7 @@ export function useNoteProperty<T extends keyof FNote>(note: FNote | null | unde
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useDebugValue(property);
|
||||||
return note[property];
|
return note[property];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,6 +281,8 @@ export function useNoteRelation(note: FNote | undefined | null, relationName: st
|
|||||||
}
|
}
|
||||||
}, [note]);
|
}, [note]);
|
||||||
|
|
||||||
|
useDebugValue(relationName);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
relationValue,
|
relationValue,
|
||||||
setter
|
setter
|
||||||
@@ -304,6 +318,8 @@ export function useNoteLabel(note: FNote | undefined | null, labelName: string):
|
|||||||
}
|
}
|
||||||
}, [note]);
|
}, [note]);
|
||||||
|
|
||||||
|
useDebugValue(labelName);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
labelValue,
|
labelValue,
|
||||||
setter
|
setter
|
||||||
@@ -333,6 +349,8 @@ export function useNoteLabelBoolean(note: FNote | undefined | null, labelName: s
|
|||||||
}
|
}
|
||||||
}, [note]);
|
}, [note]);
|
||||||
|
|
||||||
|
useDebugValue(labelName);
|
||||||
|
|
||||||
return [ labelValue, setter ] as const;
|
return [ labelValue, setter ] as const;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -354,6 +372,8 @@ export function useNoteBlob(note: FNote | null | undefined): [ FBlob | null | un
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useDebugValue(note.noteId);
|
||||||
|
|
||||||
return [ blob ] as const;
|
return [ blob ] as const;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -396,6 +416,8 @@ export function useLegacyWidget<T extends BasicWidget>(widgetFactory: () => T, {
|
|||||||
}
|
}
|
||||||
}, [ noteContext ]);
|
}, [ noteContext ]);
|
||||||
|
|
||||||
|
useDebugValue(widget);
|
||||||
|
|
||||||
return [ <div className={containerClassName} style={containerStyle} ref={ref} />, widget ]
|
return [ <div className={containerClassName} style={containerStyle} ref={ref} />, widget ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -479,6 +501,8 @@ export function useTooltip(elRef: RefObject<HTMLElement>, config: Partial<Toolti
|
|||||||
$el.tooltip("hide");
|
$el.tooltip("hide");
|
||||||
}, [ elRef ]);
|
}, [ elRef ]);
|
||||||
|
|
||||||
|
useDebugValue(config.title);
|
||||||
|
|
||||||
return { showTooltip, hideTooltip };
|
return { showTooltip, hideTooltip };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user