// 通知设置:Telegram 机器人配置、开关与测试消息
import { useEffect, useState } from 'react';
import { api, ApiError } from '../../api';
import { useI18n } from '../../i18n';
import { Icon } from '../../components/Icon';
import {
Card,
Field,
InlineError,
SettingRow,
Spinner,
Toggle,
} from '../../components/ui';
import { PanelHeader, type PanelProps } from './common';
import { cx } from '../../utils';
export function NotificationsPanel({
settings,
updateSettings,
flushSettings,
showToast,
}: PanelProps) {
const { t } = useI18n();
const notifications = settings.notifications;
const [botToken, setBotToken] = useState('');
const [chatId, setChatId] = useState('');
const [hasToken, setHasToken] = useState(Boolean(notifications.hasBotToken));
const [hasChat, setHasChat] = useState(Boolean(notifications.hasChatId));
const [saving, setSaving] = useState(false);
const [testing, setTesting] = useState(false);
const [error, setError] = useState('');
useEffect(() => {
setHasToken(Boolean(notifications.hasBotToken));
setHasChat(Boolean(notifications.hasChatId));
}, [notifications.hasBotToken, notifications.hasChatId]);
const configured = hasToken && hasChat;
const toggleEnabled = (value: boolean) => {
if (value && !configured) {
showToast(t('telegramNotConfigured'), 'error');
return;
}
updateSettings({ notifications: { enabled: value } });
};
/** 保存 Bot Token / Chat ID(服务端加密存储) */
const save = async () => {
setError('');
if ((!botToken.trim() && !hasToken) || (!chatId.trim() && !hasChat)) {
setError(t('telegramNotConfigured'));
return;
}
setSaving(true);
try {
await flushSettings();
const payload: Record<string, string> = {};
if (botToken.trim()) payload.botToken = botToken.trim();
if (chatId.trim()) payload.chatId = chatId.trim();
const saved = await api.settings.save({ notifications: payload });
updateSettings(saved);
if (payload.botToken) setHasToken(true);
if (payload.chatId) setHasChat(true);
setBotToken('');
setChatId('');
showToast(t('telegramSaved'));
} catch (err) {
setError(err instanceof ApiError ? err.message : t('networkError'));
} finally {
setSaving(false);
}
};
const test = async () => {
setError('');
setTesting(true);
try {
await flushSettings();
await api.notifications.test({
botToken: botToken.trim() || undefined,
chatId: chatId.trim() || undefined,
});
showToast(t('telegramTestSent'));
} catch (err) {
setError(err instanceof ApiError ? err.message : t('networkError'));
} finally {
setTesting(false);
}
};
return (
<>
<PanelHeader title={t('notificationsTitle')} desc={t('notificationsDesc')} />
<Card
title={t('telegramTitle')}
actions={
<span className={cx('tag', configured && 'tag-accent')}>
{configured ? t('telegramConfigured') : t('telegramNotSet')}
</span>
}
>
<SettingRow label={t('telegramEnable')} hint={t('telegramEnableHint')}>
<Toggle checked={notifications.enabled} onChange={toggleEnabled} />
</SettingRow>
<div className="grid-2">
<Field label={t('botToken')} hint={t('botTokenHint')}>
<input
className="input"
type="password"
autoComplete="off"
value={botToken}
placeholder={hasToken ? '••••••••••••' : ''}
onChange={(event) => setBotToken(event.target.value)}
/>
</Field>
<Field label={t('chatId')} hint={t('chatIdHint')}>
<input
className="input"
autoComplete="off"
value={chatId}
placeholder={hasChat ? '••••••••' : ''}
onChange={(event) => setChatId(event.target.value)}
/>
</Field>
</div>
<div className="field-hint">{t('telegramCredentialsHint')}</div>
<InlineError message={error} />
<div className="form-actions">
<button
type="button"
className="btn btn-primary"
onClick={() => void save()}
disabled={saving || testing}
>
{saving ? <Spinner size={14} /> : <Icon name="lock" size={15} />}
{t('saveTelegram')}
</button>
<button
type="button"
className="btn"
onClick={() => void test()}
disabled={saving || testing}
>
{testing ? <Spinner size={14} /> : <Icon name="send" size={15} />}
{testing ? t('sendingTest') : t('sendTestMessage')}
</button>
</div>
</Card>
<Card title={t('telegramStepsTitle')}>
<div className="about-text">{t('telegramSteps')}</div>
</Card>
</>
);
}