import { useState, useEffect, useMemo, useRef, useCallback } from 'react' import { useSettings } from '@/hooks/useSettings' import { useMobile } from '@/hooks/useMobile' import { Loader2, X } from 'lucide-react' import { DEFAULT_KEYBOARD_SHORTCUTS, DEFAULT_LEADER_KEY } from '@/api/types/settings' const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0 const CMD_KEY = isMac ? 'Cmd' : 'Ctrl' const normalizeShortcut = (shortcut: string): string => { return shortcut.replace(/Cmd/g, CMD_KEY) } const DEFAULT_DIRECT_SHORTCUTS = ['submit', 'abort'] export function KeyboardShortcuts() { const { preferences, isLoading, updateSettings } = useSettings() const isMobile = useMobile() const [recordingKey, setRecordingKey] = useState(null) const [recordingLeader, setRecordingLeader] = useState(false) const [tempShortcuts, setTempShortcuts] = useState>({}) const [tempLeaderKey, setTempLeaderKey] = useState(null) const [currentKeys, setCurrentKeys] = useState('') const leaderKey = tempLeaderKey ?? preferences?.leaderKey ?? DEFAULT_LEADER_KEY const directShortcuts = preferences?.directShortcuts ?? DEFAULT_DIRECT_SHORTCUTS const shortcuts = useMemo(() => ({ ...DEFAULT_KEYBOARD_SHORTCUTS, ...preferences?.keyboardShortcuts, ...tempShortcuts }), [preferences?.keyboardShortcuts, tempShortcuts]) const shortcutsRef = useRef(shortcuts) shortcutsRef.current = shortcuts const updateSettingsRef = useRef(updateSettings) updateSettingsRef.current = updateSettings const startRecording = (action: string) => { setRecordingKey(action) setRecordingLeader(false) setCurrentKeys('') } const startRecordingLeader = () => { setRecordingLeader(true) setRecordingKey(null) setCurrentKeys('') } const stopRecording = useCallback(() => { setRecordingKey(null) setRecordingLeader(false) setCurrentKeys('') }, []) const clearShortcut = useCallback((action: string) => { setTempShortcuts(prev => ({ ...prev, [action]: '' })) updateSettingsRef.current({ keyboardShortcuts: { ...shortcutsRef.current, [action]: '' } }) }, []) useEffect(() => { if (!recordingKey && !recordingLeader) return const handleKeyDown = (e: KeyboardEvent) => { e.preventDefault() const keys = [] if (e.ctrlKey) keys.push('Ctrl') if (e.metaKey) keys.push('Cmd') if (e.altKey) keys.push('Alt') if (e.shiftKey) keys.push('Shift') const mainKey = e.key if (!['Control', 'Meta', 'Alt', 'Shift'].includes(mainKey)) { let displayKey = mainKey if (mainKey === ' ') displayKey = 'Space' else if (mainKey === 'ArrowUp') displayKey = 'Up' else if (mainKey === 'ArrowDown') displayKey = 'Down' else if (mainKey === 'ArrowLeft') displayKey = 'Left' else if (mainKey === 'ArrowRight') displayKey = 'Right' else if (mainKey === 'Enter') displayKey = 'Return' else if (mainKey === 'Escape') displayKey = 'Esc' else if (mainKey === 'Tab') displayKey = 'Tab' else if (mainKey === 'Backspace') displayKey = 'Backspace' else if (mainKey === 'Delete') displayKey = 'Delete' else if (mainKey.length === 1) displayKey = mainKey.toUpperCase() keys.push(displayKey) if (keys.length > 0) { const shortcut = keys.join('+') if (recordingLeader) { setTempLeaderKey(shortcut) setRecordingLeader(false) setCurrentKeys('') updateSettingsRef.current({ leaderKey: shortcut }) } else if (recordingKey) { setTempShortcuts(prev => ({ ...prev, [recordingKey]: shortcut })) setRecordingKey(null) setCurrentKeys('') updateSettingsRef.current({ keyboardShortcuts: { ...shortcutsRef.current, [recordingKey]: shortcut } }) } } } else { setCurrentKeys(keys.join('+')) } } const handleKeyUp = (e: KeyboardEvent) => { if (['Control', 'Meta', 'Alt', 'Shift'].includes(e.key)) { setCurrentKeys('') } } document.addEventListener('keydown', handleKeyDown) document.addEventListener('keyup', handleKeyUp) return () => { document.removeEventListener('keydown', handleKeyDown) document.removeEventListener('keyup', handleKeyUp) } }, [recordingKey, recordingLeader]) if (isLoading) { return (
) } const formatShortcutDisplay = (action: string, keys: string) => { if (!keys) return 'Not set' if (directShortcuts.includes(action)) { return normalizeShortcut(keys) } return `${normalizeShortcut(leaderKey)} → ${normalizeShortcut(keys)}` } const toggleDirectShortcut = (action: string) => { const newDirectShortcuts = directShortcuts.includes(action) ? directShortcuts.filter(s => s !== action) : [...directShortcuts, action] updateSettings({ directShortcuts: newDirectShortcuts }) } if (isMobile) { return (

Keyboard Shortcuts

Keyboard shortcuts are not available on mobile devices.

) } return (

Keyboard Shortcuts

Leader Key

Press this first, then the shortcut key

{recordingLeader ? (
) : ( )}
{Object.entries(shortcuts).map(([action, keys]) => (

{action.replace(/([A-Z])/g, ' $1').trim()}

{recordingKey === action ? (
) : (
{keys && ( )}
)}
))}

Click on any shortcut to record a new key combination. Click on the status text below each action to toggle whether it requires the leader key.

) }