Files
monkey-ai-web/src/components/settings/NotifyChannelForm.jsx
T

172 lines
5.7 KiB
React

/*
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 <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React from 'react';
import { useTranslation } from 'react-i18next';
import { Input, InputNumber, Select } from '@/components/ui';
/**
* 通用通知渠道表单(受控组件)。按后端 dto.UserSetting 的通知子集工作,
* 支持 email / webhook / bark / gotify 四渠道。企业「通知预警」等场景复用。
*
* props:
* value: {
* notify_type, notification_email,
* webhook_url, webhook_secret,
* bark_url,
* gotify_url, gotify_token, gotify_priority
* }
* onChange(nextValue): 整对象回传
* disabled?: boolean
* channels?: 可选渠道白名单(如 ['email','webhook']),默认全部四种。
* 成员个人预警场景仅开放邮箱/Webhook。
* emailPlaceholder?: 通知邮箱输入框占位文案。默认"留空则使用企业联系邮箱"
* 是企业级预警的兜底语义;成员场景没有该兜底,须传入"必填"提示。
*/
const ALL_CHANNELS = ['email', 'webhook', 'bark', 'gotify'];
const NotifyChannelForm = ({
value = {},
onChange,
disabled = false,
channels,
emailPlaceholder,
}) => {
const { t } = useTranslation();
const v = value || {};
const allowed = ALL_CHANNELS.filter(
(c) => !Array.isArray(channels) || channels.includes(c),
);
const type =
v.notify_type && allowed.includes(v.notify_type)
? v.notify_type
: allowed[0] || 'email';
const patch = (fields) => onChange?.({ ...v, ...fields });
const Label = ({ children }) => (
<label className='oc-notify-label'>{children}</label>
);
// 原型:渠道下拉(≤360px)+ 浅底详情框承载各渠道字段(竖排标签)
return (
<div className='oc-notify-form'>
<div className='oc-notify-field oc-notify-channel'>
<Label>{t('通知渠道')}</Label>
<Select
value={type}
onChange={(val) => patch({ notify_type: val })}
disabled={disabled}
style={{ width: '100%' }}
optionList={[
{ value: 'email', label: t('邮件') },
{ value: 'webhook', label: 'Webhook' },
{ value: 'bark', label: 'Bark' },
{ value: 'gotify', label: 'Gotify' },
].filter((opt) => allowed.includes(opt.value))}
/>
</div>
<div className='oc-notify-details'>
{type === 'email' && (
<div className='oc-notify-field'>
<Label>{t('通知邮箱')}</Label>
<Input
value={v.notification_email || ''}
onChange={(val) => patch({ notification_email: val })}
placeholder={emailPlaceholder || t('留空则使用企业联系邮箱')}
disabled={disabled}
/>
</div>
)}
{type === 'webhook' && (
<>
<div className='oc-notify-field'>
<Label>{t('Webhook 地址')}</Label>
<Input
value={v.webhook_url || ''}
onChange={(val) => patch({ webhook_url: val })}
placeholder='https://example.com/webhook'
disabled={disabled}
/>
</div>
<div className='oc-notify-field'>
<Label>{t('接口凭证')}</Label>
<Input
value={v.webhook_secret || ''}
onChange={(val) => patch({ webhook_secret: val })}
placeholder={t('可选,将以 Bearer 方式加入请求头')}
disabled={disabled}
/>
</div>
</>
)}
{type === 'bark' && (
<div className='oc-notify-field'>
<Label>{t('Bark 推送地址')}</Label>
<Input
value={v.bark_url || ''}
onChange={(val) => patch({ bark_url: val })}
placeholder='https://api.day.app/xxx'
disabled={disabled}
/>
</div>
)}
{type === 'gotify' && (
<>
<div className='oc-notify-field'>
<Label>{t('Gotify 服务器地址')}</Label>
<Input
value={v.gotify_url || ''}
onChange={(val) => patch({ gotify_url: val })}
placeholder='https://gotify.example.com'
disabled={disabled}
/>
</div>
<div className='oc-notify-field'>
<Label>{t('Gotify 应用密钥')}</Label>
<Input
value={v.gotify_token || ''}
onChange={(val) => patch({ gotify_token: val })}
disabled={disabled}
/>
</div>
<div className='oc-notify-field'>
<Label>{t('消息优先级')}</Label>
<InputNumber
value={v.gotify_priority ?? 0}
onChange={(val) => patch({ gotify_priority: val })}
min={0}
max={10}
disabled={disabled}
style={{ width: '100%' }}
/>
</div>
</>
)}
</div>
</div>
);
};
export default NotifyChannelForm;