// 编辑器设置:签名、字体、字号、行高与默认发件账号
import { useEffect, useState } from 'react';
import { api } from '../../api';
import { useI18n } from '../../i18n';
import { Card, Field, Segmented, SettingRow, Slider } from '../../components/ui';
import { PanelHeader, type PanelProps } from './common';
import type { Account, FontFamily } from '../../types';
export function EditorPanel({ settings, updateSettings }: PanelProps) {
const { t } = useI18n();
const editor = settings.editor;
const [accounts, setAccounts] = useState<Account[]>([]);
const patch = (value: Record<string, unknown>) => updateSettings({ editor: value });
useEffect(() => {
api.accounts
.list()
.then(setAccounts)
.catch(() => undefined);
}, []);
const previewFont =
editor.fontFamily === 'serif'
? 'Georgia, "Songti SC", "SimSun", serif'
: '-apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif';
return (
<>
<PanelHeader title={t('editorTitle')} desc={t('editorDesc')} />
<Card title={t('signature')}>
<Field hint={t('signatureHint')}>
<textarea
className="input textarea"
rows={5}
value={editor.signature}
onChange={(event) => patch({ signature: event.target.value })}
/>
</Field>
</Card>
<Card>
<SettingRow label={t('editorFont')}>
<Segmented<FontFamily>
value={editor.fontFamily}
options={[
{ value: 'sans', label: t('fontSans') },
{ value: 'serif', label: t('fontSerif') },
]}
onChange={(value) => patch({ fontFamily: value })}
/>
</SettingRow>
<SettingRow label={t('editorFontSize')}>
<Slider
value={editor.fontSize}
min={12}
max={22}
step={1}
format={(value) => `${value}px`}
onChange={(value) => patch({ fontSize: value })}
/>
</SettingRow>
<SettingRow label={t('editorLineHeight')}>
<Slider
value={editor.lineHeight}
min={1.2}
max={2.2}
step={0.05}
format={(value) => value.toFixed(2)}
onChange={(value) => patch({ lineHeight: value })}
/>
</SettingRow>
</Card>
<Card title={t('defaultAccount')}>
<Field>
<select
className="input"
value={editor.defaultAccountId ?? 0}
onChange={(event) => patch({ defaultAccountId: Number(event.target.value) || null })}
>
<option value={0}>{t('noDefaultAccount')}</option>
{accounts.map((account) => (
<option key={account.id} value={account.id}>
{account.name} · {account.email}
</option>
))}
</select>
</Field>
</Card>
<Card title={t('appearancePreview')}>
<div
className="preview-mail"
style={{ fontFamily: previewFont, fontSize: `${editor.fontSize}px`, lineHeight: editor.lineHeight }}
>
<div className="preview-subject">{t('previewSubject')}</div>
<p className="preview-body">{t('previewBody')}</p>
{editor.signature.trim() && <pre className="preview-signature">{editor.signature}</pre>}
</div>
</Card>
</>
);
}