/*
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 (
);
};
export default NotificationSection;