()
| 7 | * Mount once inside App, within Router + AppProvider. |
| 8 | */ |
| 9 | export const GlobalShortcuts: FC = () => { |
| 10 | const { shortcuts } = useShortcutActions(); |
| 11 | |
| 12 | useEffect(() => { |
| 13 | const keyToName = new Map<string, keyof typeof shortcuts>( |
| 14 | Object.entries(shortcuts).map(([name, cfg]) => [ |
| 15 | cfg.key, |
| 16 | name as keyof typeof shortcuts, |
| 17 | ]), |
| 18 | ); |
| 19 | |
| 20 | const handler = (event: KeyboardEvent) => { |
| 21 | // Ignore if user is typing in an input, textarea, or with modifiers |
| 22 | if ( |
| 23 | event.target instanceof HTMLInputElement || |
| 24 | event.target instanceof HTMLTextAreaElement || |
| 25 | event.metaKey || |
| 26 | event.ctrlKey || |
| 27 | event.altKey |
| 28 | ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | const key = event.key.toLowerCase(); |
| 33 | const name = keyToName.get(key); |
| 34 | if (!name) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if (shortcuts[name].isAllowed) { |
| 39 | event.preventDefault(); |
| 40 | shortcuts[name].action(); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | document.addEventListener('keydown', handler); |
| 45 | return () => { |
| 46 | document.removeEventListener('keydown', handler); |
| 47 | }; |
| 48 | }, [shortcuts]); |
| 49 | |
| 50 | return null; |
| 51 | }; |
nothing calls this directly
no test coverage detected