summaryrefslogtreecommitdiff
path: root/apps/web/src/actions/use-keyboard-shortcuts-help.ts
diff options
context:
space:
mode:
authorMaze Winther <mazewinther@gmail.com>2026-04-20 11:31:17 +0200
committerMaze Winther <mazewinther@gmail.com>2026-04-20 11:31:17 +0200
commit3e89d2998588f8e35c524e523ba2ee853f0f6475 (patch)
tree57221f96c1259b3b07e3ddf8c1adefc502b9d8eb /apps/web/src/actions/use-keyboard-shortcuts-help.ts
parent729d10592f4e802f91c555c309ff3ba4b119f843 (diff)
downloadopencut-3e89d2998588f8e35c524e523ba2ee853f0f6475.tar.gz
refactor: restructure files to their domains, new preview overlay system, and dep graph
Diffstat (limited to 'apps/web/src/actions/use-keyboard-shortcuts-help.ts')
-rw-r--r--apps/web/src/actions/use-keyboard-shortcuts-help.ts83
1 files changed, 83 insertions, 0 deletions
diff --git a/apps/web/src/actions/use-keyboard-shortcuts-help.ts b/apps/web/src/actions/use-keyboard-shortcuts-help.ts
new file mode 100644
index 00000000..7681e664
--- /dev/null
+++ b/apps/web/src/actions/use-keyboard-shortcuts-help.ts
@@ -0,0 +1,83 @@
+"use client";
+
+import { useMemo } from "react";
+import { useKeybindingsStore } from "@/actions/keybindings-store";
+import { ACTIONS, type TActionWithOptionalArgs } from "@/actions";
+import {
+ getPlatformAlternateKey,
+ getPlatformSpecialKey,
+} from "@/utils/platform";
+
+export interface KeyboardShortcut {
+ id: string;
+ keys: string[];
+ description: string;
+ category: string;
+ action: TActionWithOptionalArgs;
+ icon?: React.ReactNode;
+}
+
+function formatKey({ key }: { key: string }): string {
+ return key
+ .replace("ctrl", getPlatformSpecialKey())
+ .replace("alt", getPlatformAlternateKey())
+ .replace("shift", "Shift")
+ .replace("left", "←")
+ .replace("right", "→")
+ .replace("up", "↑")
+ .replace("down", "↓")
+ .replace("space", "Space")
+ .replace("home", "Home")
+ .replace("enter", "Enter")
+ .replace("end", "End")
+ .replace("delete", "Delete")
+ .replace("backspace", "Backspace")
+ .replace("-", "+");
+}
+
+export function useKeyboardShortcutsHelp() {
+ const { keybindings } = useKeybindingsStore();
+
+ const shortcuts = useMemo(() => {
+ const result: KeyboardShortcut[] = [];
+ const actionToKeys: Partial<Record<TActionWithOptionalArgs, string[]>> = {};
+
+ for (const [key, action] of Object.entries(keybindings) as Array<
+ [string, TActionWithOptionalArgs | undefined]
+ >) {
+ if (action) {
+ if (!actionToKeys[action]) {
+ actionToKeys[action] = [];
+ }
+ actionToKeys[action].push(formatKey({ key }));
+ }
+ }
+
+ for (const [action, keys] of Object.entries(actionToKeys) as Array<
+ [TActionWithOptionalArgs, string[]]
+ >) {
+ const actionDef = ACTIONS[action];
+ if (!actionDef) {
+ continue;
+ }
+ result.push({
+ id: action,
+ keys,
+ description: actionDef.description,
+ category: actionDef.category,
+ action,
+ });
+ }
+
+ return result.sort((a, b) => {
+ if (a.category !== b.category) {
+ return a.category.localeCompare(b.category);
+ }
+ return a.description.localeCompare(b.description);
+ });
+ }, [keybindings]);
+
+ return {
+ shortcuts,
+ };
+}