Skip to content

Build your personal knowledge base with Trilium Notes

client/shortcut hints: fix some issues

Adorian Doran33ce549d5ef8
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 0cc0c2c336..964372aa8a 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -19,7 +19,7 @@ import { parseNavigationStateFromUrl, ViewScope } from "../../services/link";
import options, { type OptionValue } from "../../services/options";
import protected_session_holder from "../../services/protected_session_holder";
import server from "../../services/server";
-import { ShortcutHintDefinition, ShortcutHintProvider } from "../../services/shortcut_hints";
+import type { ShortcutHintDefinition, ShortcutHintProvider } from "../../services/shortcut_hints";
import shortcuts, { Handler, removeIndividualBinding } from "../../services/shortcuts";
import SpacedUpdate, { type StateCallback } from "../../services/spaced_update";
import { getEffectiveThemeStyle } from "../../services/theme";
@@ -1159,20 +1159,27 @@ export function useLegacyImperativeHandlers(handlers: Record<string, Function>)
/**
* Registers this widget's contextual shortcut hints on its host component. When the user requests
- * contextual shortcut help (Shift+F1 by default), the dispatcher walks up from the focused element
+ * contextual shortcut help (Alt+F1 by default), the dispatcher walks up from the focused element
* and asks each component in the chain to contribute its hints — so these appear only while this
* widget (or one of its descendants) is focused. The registration is removed automatically on unmount.
*
* @param hints the sections to contribute, or a function returning them (use the function form when
- * the hints depend on component state).
+ * the hints depend on component state). Read lazily on each request, so it need not be a
+ * stable reference.
*/
export function useContextualShortcutHints(hints: ShortcutHintDefinition | (() => ShortcutHintDefinition)) {
const parentComponent = useContext(ParentComponent);
+ // Keep the latest hints in a ref so the provider registers once per host rather than
+ // re-registering on every render when the caller passes an inline array/function.
+ const hintsRef = useRef(hints);
+ hintsRef.current = hints;
+
useEffect(() => {
if (!parentComponent) return;
const provider: ShortcutHintProvider = (collector) => {
- collector.add(...(typeof hints === "function" ? hints() : hints));
+ const current = hintsRef.current;
+ collector.add(...(typeof current === "function" ? current() : current));
};
parentComponent.getContextualShortcutHints = provider;
@@ -1182,7 +1189,7 @@ export function useContextualShortcutHints(hints: ShortcutHintDefinition | (() =
delete parentComponent.getContextualShortcutHints;
}
};
- }, [ parentComponent, hints ]);
+ }, [ parentComponent ]);
useDebugValue("contextual-shortcut-hints");
}
diff --git a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsx b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsx
index c690399f33..ecc2618ce2 100644
--- a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsx
+++ b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsx
@@ -126,10 +126,13 @@ function HintRow({ hint }: { hint: ShortcutHint }) {
return;
}
// `action` hints resolve to the user's current, rebindable binding.
+ let cancelled = false;
keyboard_actions.getAction(hint.action).then(action => {
+ if (cancelled) return;
setShortcuts(action?.effectiveShortcuts ?? []);
setFriendlyName(action?.friendlyName);
});
+ return () => { cancelled = true; };
}, [ hint ]);
const description = hint.labelKey ? t(hint.labelKey) : friendlyName ?? "";