/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import React, { useRef, useEffect } from 'react'; import { Button, Form, Toast } from '@/components/ui'; import { Check, Copy } from 'lucide-react'; import { FormQuotaInput } from '../../../common/ui/QuotaInput'; import { copy } from '../../../../helpers'; import SectionHeader from '../components/SectionHeader'; // Webhook 请求结构示例(只读展示,与原 CodeViewer 展示的内容相同) const WEBHOOK_EXAMPLE = { type: 'quota_exceed', title: '额度预警通知', content: '您的额度即将用尽,当前剩余额度为 {{value}}', values: ['¥0.99'], timestamp: 1739950503, }; const NotificationSection = ({ t, notificationSettings, handleNotificationSettingChange, saveNotificationSettings, }) => { const formApiRef = useRef(null); useEffect(() => { if (formApiRef.current && notificationSettings) { formApiRef.current.setValues(notificationSettings); } }, [notificationSettings]); const handleFormChange = (field, value) => { handleNotificationSettingChange(field, value); }; const handleSubmit = () => { if (formApiRef.current) { formApiRef.current .validate() .then(() => { saveNotificationSettings(); }) .catch((errors) => { console.log('表单验证失败:', errors); Toast.error(t('请检查表单填写是否正确')); }); } else { saveNotificationSettings(); } }; // 复制示例:沿用原 CodeViewer 的复制方式与提示 const handleCopyExample = async () => { try { const success = await copy(JSON.stringify(WEBHOOK_EXAMPLE, null, 2)); Toast.success(t('已复制到剪贴板')); if (!success) { throw new Error('Copy operation failed'); } } catch (err) { Toast.error(t('复制失败')); console.error('Copy failed:', err); } }; // 通知方式选项(仅保留邮件通知和Webhook) const notifyOptions = [ { value: 'email', label: t('邮件提醒'), desc: t('发送到账户邮箱或指定邮箱'), }, { value: 'webhook', label: 'Webhook', desc: t('将提醒推送到外部系统'), }, ]; return (
{t('保存设置')} } />
(formApiRef.current = api)} initValues={notificationSettings} onSubmit={handleSubmit} > {/* 通知方式选择 */}
{t('通知方式')}
{notifyOptions.map((option) => { const isSelected = notificationSettings.warningType === option.value; return ( ); })}
{/* 预警阈值 */} handleFormChange('warningThreshold', val)} extraText={t('当剩余额度低于此数值时,通过所选方式发送通知')} style={{ width: 260, maxWidth: '100%' }} rules={[ { required: true, message: t('请输入预警阈值') }, { validator: (rule, value) => { const numValue = Number(value); if (isNaN(numValue) || numValue <= 0) { return Promise.reject(t('预警阈值必须为正数')); } return Promise.resolve(); }, }, ]} /> {/* 邮件通知设置 */} {notificationSettings.warningType === 'email' && (
handleFormChange('notificationEmail', val)} extraText={t('不填写则使用账号绑定的邮箱')} showClear />
)} {/* Webhook通知设置 */} {notificationSettings.warningType === 'webhook' && (
handleFormChange('webhookUrl', val)} extraText={t('仅支持 HTTPS,系统以 POST 方式发送通知')} showClear rules={[ { required: notificationSettings.warningType === 'webhook', message: t('请输入Webhook地址'), }, { pattern: /^https:\/\/.+/, message: t('Webhook地址必须以https://开头'), }, ]} /> handleFormChange('webhookSecret', val)} extraText={t('密钥以 Bearer 方式添加到请求头中')} />
{t('Webhook 请求结构说明')}
              {JSON.stringify(WEBHOOK_EXAMPLE, null, 2)}
            
)}
); }; export default NotificationSection;