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

50 lines
2.2 KiB
TypeScript

import type { Context } from '@deepseek-ai/cordis';
import type {} from './settings-slots.js';
import { settingsCopyFor, zh, en } from './settings-copy.js';
import { SettingsSection, type BetterDisplaySettingsInjected, type OpenPrefs } from './SettingsSection.js';
import type { ChatPrefs } from './chat/bindings-store.js';
interface LocaleFace {
register?: (ns: string, dicts: { zh: unknown; en: unknown }) => () => void;
bind?: (ns: string) => (key: string) => string;
getSnapshot?: () => { locale?: string; language?: string };
}
function languageTag(ctx: Context): string | undefined {
const locale = (ctx.get?.('locale') ?? (ctx as unknown as { locale?: LocaleFace }).locale) as LocaleFace | undefined;
const snap = locale?.getSnapshot?.();
return snap?.locale ?? snap?.language
?? (typeof document !== 'undefined' ? document.documentElement.lang : undefined)
?? (typeof navigator !== 'undefined' ? navigator.language : undefined);
}
/**
* Own locale namespace and settings-section id. The merged plugin used to reuse
* dsh-better-display's `better-display` id; while the standalone display plugin
* is still installed, the two registrations collided and the nav label flipped
* between "Better Display" and "Better Chat" depending on load order.
*/
const SETTINGS_LOCALE_NS = 'better-chat'
const SETTINGS_SECTION_ID = 'better-chat'
export function installBetterDisplaySettings(ctx: Context, prefs: OpenPrefs, chat?: ChatPrefs): void {
const locale = (ctx.get?.('locale') ?? (ctx as unknown as { locale?: LocaleFace }).locale) as LocaleFace | undefined;
if (locale?.register) {
ctx.effect(() => locale.register!(SETTINGS_LOCALE_NS, { zh, en }), 'dsh-better-chat: settings copy');
}
const injected = (): BetterDisplaySettingsInjected => ({
prefs,
chat,
copy: settingsCopyFor(languageTag(ctx)),
languageTag: languageTag(ctx),
});
ctx.slots.inject('settings.section', () => ctx.slots.register({
name: 'settings.section',
id: SETTINGS_SECTION_ID,
order: 40,
label: () => locale?.bind?.(SETTINGS_LOCALE_NS)?.('nav') || settingsCopyFor(languageTag(ctx)).nav,
locale: locale?.bind ? SETTINGS_LOCALE_NS : undefined,
inject: injected,
}, SettingsSection));
}