import { tr } from '../lib/i18n'; import { useState } from 'react'; import { Dialog, Btn, IconBtn } from '../components/ui'; import { setShortcuts, useStore } from '../lib/store'; import { getCommands, MOD } from '../lib/commands'; import { bindingFor, bindingEqual, bindingKeys, eventBinding, resetBindings, setBinding, useBindings } from '../lib/shortcut-bindings'; export function ShortcutsDialog() { const open = useStore(s => s.shortcutsOpen); useBindings(); const [recording, setRecording] = useState(null), [error, setError] = useState(''); const rows = [...getCommands().filter(c => c.key).map(c => ({ id: c.id, label: c.label, binding: c.binding ?? null, group: c.group })), { id: 'chat:send', label: 'Send message', binding: bindingFor('chat:send', { key: 'enter', mod: false, alt: false, shift: false }), group: 'Chat' }]; function record(id: string, e: React.KeyboardEvent) { e.preventDefault(); e.stopPropagation(); if (['Meta', 'Control', 'Shift', 'Alt'].includes(e.key)) return; if (e.key === 'Escape') { setRecording(null); setError(''); return; } const b = eventBinding(e); if (id === 'chat:send' ? b.key !== 'enter' || b.alt || b.shift : !b.mod || ['tab', 'escape', 'enter'].includes(b.key)) { setError(id === 'chat:send' ? 'Use Enter or Command/Ctrl + Enter. Shift + Enter stays available for a new line.' : 'Use Command/Ctrl with a letter, number or punctuation key.'); return; } const duplicate = rows.find(r => r.id !== id && bindingEqual(r.binding, b)); if (duplicate) { setError(`Already assigned to ${duplicate.label}. Clear or change that binding first.`); return; } setBinding(id, b); setRecording(null); setError(''); } return { setRecording(null); setShortcuts(false); }} actions={<> { resetBindings(); setRecording(null); setError(''); }}>{tr("Restore defaults")} { setRecording(null); setError(''); setShortcuts(false); }}>{tr("Done")}}>

{tr("Click a shortcut, then press your preferred keys. Changes are saved in this browser. Escape cancels recording.")}

{tr(error ?

{tr(error)}

: null)}
{tr([...new Set(rows.map(r => r.group))].map(group =>

{tr(group)}

{tr(rows.filter(r => r.group === group).map(r =>
{tr(r.label)}{tr(r.id !== 'chat:send' ? setBinding(r.id, null)}/> : null)}
))}
))}

{tr("Shift + Enter adds a new line. Type / for chat commands. Escape closes panels.")}

; } export { MOD };