Bin
2025-12-17 d616898802dfe7e5dd648bcf53c6d1f86b6d3642
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { useHotkeys } from "react-hotkeys-hook";
import { toStudlyCaps } from "@humansignal/core";
import { keymap } from "./keymap";
 
export type Hotkey = {
  title: string;
  shortcut?: string;
  macos?: string;
  other?: string;
};
 
const readableShortcut = (shortcut: string | null | undefined) => {
  if (!shortcut || typeof shortcut !== "string") {
    return "";
  }
 
  return shortcut
    .split("+")
    .map((str) => toStudlyCaps(str))
    .join(" + ");
};
 
export const useShortcut = (
  actionName: keyof typeof keymap,
  callback: () => void,
  options = { showShortcut: true },
  dependencies = undefined,
) => {
  const action = keymap[actionName] as Hotkey;
  const isMacos = /mac/i.test(navigator.platform);
 
  let shortcut = action.shortcut ?? ((isMacos ? action.macos : action.other) as string);
 
  // Check for custom shortcut in app settings
  const customMapping = window.APP_SETTINGS?.lookupHotkey?.(`data_manager:${actionName}`);
  if (customMapping) {
    // Explicitly use the custom key even if it's null
    shortcut = customMapping.key;
  }
 
  useHotkeys(
    shortcut,
    () => {
      callback();
    },
    {
      keyup: false,
      element: document.body,
    } as any,
    dependencies,
  );
 
  const title = action.title + (options.showShortcut ? `: [ ${readableShortcut(shortcut)} ]` : "");
 
  return title;
};