summaryrefslogtreecommitdiff
path: root/apps/web/src/hooks/use-keybinding-conflicts.ts
diff options
context:
space:
mode:
authorMaze Winther <mazewinther@gmail.com>2025-07-18 11:37:48 +0200
committerMaze Winther <mazewinther@gmail.com>2025-07-18 11:37:48 +0200
commit74e7d427aaed3517ce578c2192f783dacffc02fc (patch)
treec6ec27070940778e64ca3ad50cd937227f87f3ac /apps/web/src/hooks/use-keybinding-conflicts.ts
parentc4e6a9a45ab0f0afdde4f0c9991e39cd5d6205d0 (diff)
parent23001f8442e60999e34c0d6c348fecf0867b6cad (diff)
downloadopencut-74e7d427aaed3517ce578c2192f783dacffc02fc.tar.gz
Merge branch 'feat/shortcut-actions' into staging
Diffstat (limited to 'apps/web/src/hooks/use-keybinding-conflicts.ts')
-rw-r--r--apps/web/src/hooks/use-keybinding-conflicts.ts59
1 files changed, 59 insertions, 0 deletions
diff --git a/apps/web/src/hooks/use-keybinding-conflicts.ts b/apps/web/src/hooks/use-keybinding-conflicts.ts
new file mode 100644
index 00000000..cc5dd340
--- /dev/null
+++ b/apps/web/src/hooks/use-keybinding-conflicts.ts
@@ -0,0 +1,59 @@
+"use client";
+
+import { useMemo } from "react";
+import { useKeybindingsStore } from "@/stores/keybindings-store";
+import { ActionWithOptionalArgs } from "@/constants/actions";
+
+export interface KeybindingConflictInfo {
+ key: string;
+ actions: ActionWithOptionalArgs[];
+ isConflict: boolean;
+}
+
+export const useKeybindingConflicts = () => {
+ const { keybindings } = useKeybindingsStore();
+
+ const conflicts = useMemo(() => {
+ const keyToActions: Record<string, ActionWithOptionalArgs[]> = {};
+ const conflictList: KeybindingConflictInfo[] = [];
+
+ // Group actions by key
+ Object.entries(keybindings).forEach(([key, action]) => {
+ if (!keyToActions[key]) {
+ keyToActions[key] = [];
+ }
+ keyToActions[key].push(action);
+ });
+
+ // Find conflicts
+ Object.entries(keyToActions).forEach(([key, actions]) => {
+ const uniqueActions = [...new Set(actions)];
+ conflictList.push({
+ key,
+ actions: uniqueActions,
+ isConflict: uniqueActions.length > 1,
+ });
+ });
+
+ return conflictList.filter((item) => item.isConflict);
+ }, [keybindings]);
+
+ const hasConflicts = conflicts.length > 0;
+
+ const getConflictsForKey = (key: string): KeybindingConflictInfo | null => {
+ return conflicts.find((conflict) => conflict.key === key) || null;
+ };
+
+ const getConflictsForAction = (
+ action: ActionWithOptionalArgs
+ ): KeybindingConflictInfo[] => {
+ return conflicts.filter((conflict) => conflict.actions.includes(action));
+ };
+
+ return {
+ conflicts,
+ hasConflicts,
+ getConflictsForKey,
+ getConflictsForAction,
+ };
+};