import { useCallback, useEffect, useState, useSyncExternalStore } from 'react'; import { BINDINGS_CHANGED_EVENT, bindingKeycap, normalizeBindings, validateBinding, type ApprovalAction, type ApprovalBinding, } from './chat/bindings.js'; import type { ChatPrefs } from './chat/bindings-store.js'; import { settingsCopyFor, type SettingsCopy, type SettingsCopyKey } from './settings-copy.js'; import css from './SettingsSection.module.css'; export interface BetterChatSettingsInjected { chat?: ChatPrefs; copy?: SettingsCopy; languageTag?: string; } type SettingsProps = BetterChatSettingsInjected & { /** Official settings.section owner share; unused here. */ close?: () => void; t?: (key: SettingsCopyKey) => string; }; function text(props: SettingsProps, copy: SettingsCopy, key: keyof SettingsCopy): string { if (typeof props.t === 'function') { try { const value = props.t(key); if (typeof value === 'string' && value.length > 0 && value !== key) return value; } catch { // Fall through to the bundled dictionaries. } } return copy[key]; } export function SettingsSection(props: SettingsProps) { const copy = props.copy ?? settingsCopyFor(props.languageTag); // 审批快捷键:读插件配置,保存后派发事件让卡片提示立即重建。 const chat = props.chat; const chatSnap = useSyncExternalStore( chat?.subscribe ?? (() => () => {}), () => chat?.getSnapshot() ?? ({ bindings: undefined } as never), () => ({ bindings: undefined } as never), ); const bindings = normalizeBindings(chatSnap); const mac = typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform); const [recording, setRecording] = useState(null); const [keyError, setKeyError] = useState(); const announceBindings = useCallback(() => { if (typeof window === 'undefined') return; window.dispatchEvent(new CustomEvent(BINDINGS_CHANGED_EVENT)); }, []); useEffect(() => { if (!recording || !chat) return; const onKey = (event: KeyboardEvent) => { event.preventDefault(); event.stopPropagation(); if (event.key === 'Escape' && recording !== 'reject' && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) { setRecording(null); setKeyError(undefined); return; } const key = event.key === 'Enter' ? 'Enter' : event.key === 'Escape' ? 'Escape' : undefined; if (!key) { setKeyError(copy.shortcutErrorKey); return; } const binding: ApprovalBinding = { key, primary: event.ctrlKey || event.metaKey, shift: event.shiftKey, alt: event.altKey, }; const verdict = validateBinding(recording, binding, bindings); if (verdict === 'key') { setKeyError(copy.shortcutErrorKey); return; } if (verdict === 'modifier') { setKeyError(copy.shortcutErrorModifier); return; } if (verdict === 'conflict') { setKeyError(copy.shortcutErrorConflict); return; } chat.actions.setApprovalBinding(recording, binding); announceBindings(); setRecording(null); setKeyError(undefined); }; window.addEventListener('keydown', onKey, true); return () => window.removeEventListener('keydown', onKey, true); }, [recording, chat, chatSnap, copy, bindings, announceBindings]); return (
{chat ? (
{text(props, copy, 'shortcutTitle')}

{text(props, copy, 'shortcutDescription')}

{([ ['allow', 'shortcutAllow', bindings.allow], ['always', 'shortcutAlways', bindings.always], ['reject', 'shortcutReject', bindings.reject], ] as const).map(([action, title, binding]) => (
{text(props, copy, title)}
{action === 'always' && binding ? ( ) : null}
))} {keyError ?

{keyError}

: null}
) : null}
); }