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

240 lines
8.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useCallback, useEffect, useState, useSyncExternalStore } from 'react';
import { frostedGlassOf, type FoldIntensity } from './fold-intensity.js';
import {
BINDINGS_CHANGED_EVENT,
bindingKeycap,
normalizeBindings,
validateBinding,
type ApprovalAction,
type ApprovalBinding,
} from './chat/bindings.js';
import type { ChatPrefs } from './chat/bindings-store.js';
import { deliverableOpenModeOf, type DeliverableOpenMode } from './open-file.js';
import { settingsCopyFor, type SettingsCopy, type SettingsCopyKey } from './settings-copy.js';
import css from './SettingsSection.module.css';
export interface ReaderPrefsSnapshot {
deliverableOpenMode?: DeliverableOpenMode;
frostedGlass?: boolean;
foldIntensity?: FoldIntensity;
autoFold?: boolean;
processOnly?: boolean;
}
export interface OpenPrefs {
getSnapshot: () => ReaderPrefsSnapshot;
subscribe: (fn: () => void) => () => void;
actions: {
setDeliverableOpenMode: (value: DeliverableOpenMode) => void;
setFrostedGlass: (value: boolean) => void;
setFoldIntensity?: (value: FoldIntensity) => void;
setAutoFold?: (value: boolean) => void;
};
}
export interface BetterDisplaySettingsInjected {
prefs: OpenPrefs;
chat?: ChatPrefs;
copy?: SettingsCopy;
languageTag?: string;
}
type SettingsProps = BetterDisplaySettingsInjected & {
/** 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 snap = useSyncExternalStore(
props.prefs.subscribe,
() => props.prefs.getSnapshot() ?? ({} as ReaderPrefsSnapshot),
() => ({} as ReaderPrefsSnapshot),
);
const mode = deliverableOpenModeOf(snap.deliverableOpenMode);
const glass = frostedGlassOf(snap);
const autoFold = snap.autoFold !== false && snap.foldIntensity !== 0;
const setMode = (value: DeliverableOpenMode) => {
props.prefs.actions.setDeliverableOpenMode(value);
};
const on = mode === 'sidebar';
// 审批快捷键:读插件配置(dsh.better-chat.v1),保存后派发事件让卡片提示立即重建。
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-display-settings>
<div className={css.row}>
<div className={css.rowText}>
<div className={css.title}>{text(props, copy, 'openTitle')}</div>
<div className={css.desc}>{text(props, copy, 'openDescription')}</div>
</div>
<button
type="button"
role="switch"
aria-checked={on}
className={css.switch}
data-on={on || undefined}
data-better-display-open-mode={mode}
onClick={() => { setMode(on ? 'external' : 'sidebar'); }}
/>
</div>
<div className={css.row}>
<div className={css.rowText}>
<div className={css.title}>{text(props, copy, 'glassTitle')}</div>
<div className={css.desc}>{text(props, copy, 'glassDescription')}</div>
</div>
<button
type="button"
role="switch"
aria-checked={glass}
className={css.switch}
data-on={glass || undefined}
data-better-display-glass={glass ? 'on' : 'off'}
onClick={() => { props.prefs.actions.setFrostedGlass(!glass); }}
/>
</div>
<div className={css.row}>
<div className={css.rowText}>
<div className={css.title}>{text(props, copy, 'foldTitle')}</div>
<div className={css.desc}>{text(props, copy, 'foldDescription')}</div>
</div>
<button
type="button"
role="switch"
aria-checked={autoFold}
className={css.switch}
data-on={autoFold || undefined}
data-better-display-auto-fold={autoFold ? 'on' : 'off'}
onClick={() => {
props.prefs.actions.setAutoFold?.(!autoFold);
props.prefs.actions.setFoldIntensity?.(!autoFold ? 1 : 0);
}}
/>
</div>
{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>
);
}