mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 14:43:24 +00:00
28 lines
684 B
TypeScript
28 lines
684 B
TypeScript
import KEYS from '#core/constants/keys';
|
|
import { useHotkeys } from '@mantine/hooks';
|
|
import { useGlobalHotkeysStore } from '~/stores/global_hotkeys_store';
|
|
|
|
type ShortcutOptions = {
|
|
enabled?: boolean;
|
|
disableGlobalCheck?: boolean;
|
|
};
|
|
|
|
export default function useShortcut(
|
|
key: keyof typeof KEYS,
|
|
cb: () => void,
|
|
{ enabled, disableGlobalCheck }: ShortcutOptions = {
|
|
enabled: true,
|
|
disableGlobalCheck: false,
|
|
}
|
|
) {
|
|
const { globalHotkeysEnabled } = useGlobalHotkeysStore();
|
|
const isEnabled = disableGlobalCheck
|
|
? enabled
|
|
: enabled && globalHotkeysEnabled;
|
|
return useHotkeys(
|
|
[[KEYS[key], () => isEnabled && cb(), { preventDefault: true }]],
|
|
undefined,
|
|
true
|
|
);
|
|
}
|