dsh-better-chat/src/client/SettingsSection.tsx

155 lines
5.7 KiB
TypeScript

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<ApprovalAction | null>(null);
const [keyError, setKeyError] = useState<string | undefined>();
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 (
<div className={css.section} data-better-chat-settings>
{chat ? (
<section className={css.block} data-better-chat-shortcuts>
<div className={css.title}>{text(props, copy, 'shortcutTitle')}</div>
<p className={css.desc}>{text(props, copy, 'shortcutDescription')}</p>
{([
['allow', 'shortcutAllow', bindings.allow],
['always', 'shortcutAlways', bindings.always],
['reject', 'shortcutReject', bindings.reject],
] as const).map(([action, title, binding]) => (
<div className={css.row} key={action}>
<div className={css.rowText}>
<div className={css.title}>{text(props, copy, title)}</div>
</div>
<div className={css.keyActions}>
<button
type="button"
className={css.keyButton}
data-recording={recording === action || undefined}
onClick={() => { setKeyError(undefined); setRecording(action); }}
>
{recording === action
? text(props, copy, 'shortcutRecording')
: binding ? bindingKeycap(binding, mac) : text(props, copy, 'shortcutDisabled')}
</button>
{action === 'always' && binding ? (
<button
type="button"
className={css.button}
onClick={() => {
chat.actions.setApprovalBinding('always', null);
announceBindings();
setKeyError(undefined);
}}
>
{text(props, copy, 'shortcutDisable')}
</button>
) : null}
</div>
</div>
))}
{keyError ? <p className={css.error} data-better-chat-key-error>{keyError}</p> : null}
<div className={css.actions}>
<button
type="button"
className={css.button}
onClick={() => {
chat.actions.resetApprovalBindings();
announceBindings();
setRecording(null);
setKeyError(undefined);
}}
>
{text(props, copy, 'shortcutReset')}
</button>
</div>
</section>
) : null}
</div>
);
}