import React from 'react'; import { createPortal } from 'react-dom'; import { Bell, Check, Eye, EyeOff, Loader2, X } from 'lucide-react'; import { enableCustomSMTP } from '../../../../notificationEmailConfig'; import { notificationChannelTypes } from '../state'; import { NotificationEventSelector } from './NotificationEventSelector'; import type { NotificationChannel, NotificationChannelType, NotificationEventType, SystemSettings } from '../../../../types'; import type { NotificationForm } from '../types'; // NotificationChannelModalProps 描述渠道编辑弹窗所需的状态和回调。 export interface NotificationChannelModalProps { // showModal 表示弹窗是否打开。 showModal: boolean; // editing 表示当前编辑的渠道,新增时为空。 editing: NotificationChannel | null; // form 是当前渠道表单值。 form: NotificationForm; // setForm 更新渠道表单值。 setForm: React.Dispatch>; // smtp 是系统 SMTP 配置,用于填充独立 SMTP 初始值。 smtp: SystemSettings; // showChannelSmtpPassword 表示是否显示独立 SMTP 密码。 showChannelSmtpPassword: boolean; // setShowChannelSmtpPassword 切换独立 SMTP 密码显示状态。 setShowChannelSmtpPassword: React.Dispatch>; // saving 表示渠道保存请求是否正在执行。 saving: boolean; // onClose 关闭弹窗。 onClose: () => void; // onSave 提交渠道表单。 onSave: () => void | Promise; } // NotificationChannelModal 渲染渠道字段、SMTP 覆盖项和事件绑定表单。 export const NotificationChannelModal: React.FC = ({ showModal, editing, form, setForm, smtp, showChannelSmtpPassword, setShowChannelSmtpPassword, saving, onClose, onSave }) => { // meta 是当前渠道类型对应的字段和指南。 const meta = notificationChannelTypes[form.type]; // handleNameChange 更新渠道名称。 const handleNameChange = (event: React.ChangeEvent) => setForm( // nextFormUpdater 使用函数式更新名称字段。 current => ({ ...current, name: event.target.value }), ); // handleTypeChange 切换渠道类型并清空旧类型配置。 const handleTypeChange = (event: React.MouseEvent) => { // type 是按钮数据属性中的目标渠道类型。 const type = event.currentTarget.dataset.type as NotificationChannelType | undefined; if (!type) return; setForm( // nextFormUpdater 使用函数式更新渠道类型。 current => ({ ...current, type, config: {} }), ); }; // handleConfigFieldChange 更新普通或独立 SMTP 配置字段。 const handleConfigFieldChange = (event: React.ChangeEvent) => { // key 是输入框数据属性中的配置字段名。 const key = event.currentTarget.dataset.field; if (!key) return; setForm( // nextFormUpdater 使用函数式更新配置字段。 current => ({ ...current, config: { ...current.config, [key]: event.target.value } }), ); }; // handleToggleCustomSMTP 切换邮件渠道是否使用独立 SMTP。 const handleToggleCustomSMTP = () => setForm( // nextFormUpdater 使用函数式更新 SMTP 来源模式。 current => ({ ...current, config: current.config.use_custom_smtp === true ? { ...current.config, use_custom_smtp: false } : enableCustomSMTP(current.config, smtp) }), ); // handleChannelTlsChange 切换独立 SMTP 的 STARTTLS 并保持模式互斥。 const handleChannelTlsChange = (event: React.ChangeEvent) => setForm( // nextFormUpdater 使用函数式更新独立 SMTP 的 TLS/SSL 互斥字段。 current => ({ ...current, config: { ...current.config, smtp_use_tls: event.target.checked, smtp_use_ssl: event.target.checked ? false : current.config.smtp_use_ssl } }), ); // handleChannelSslChange 切换独立 SMTP 的 SSL 并保持模式互斥。 const handleChannelSslChange = (event: React.ChangeEvent) => setForm( // nextFormUpdater 使用函数式更新独立 SMTP 的 SSL/TLS 互斥字段。 current => ({ ...current, config: { ...current.config, smtp_use_ssl: event.target.checked, smtp_use_tls: event.target.checked ? false : current.config.smtp_use_tls } }), ); // handleToggleEvent 使用函数式更新切换事件绑定。 const handleToggleEvent = (event: NotificationEventType) => setForm( // nextFormUpdater 使用函数式更新事件绑定集合。 current => ({ ...current, event_types: current.event_types.includes(event) ? current.event_types.filter( // item 是待保留的已有事件值。 item => item !== event, ) : [...current.event_types, event] }), ); // handleToggleEnabled 使用函数式更新切换渠道启用状态。 const handleToggleEnabled = () => setForm( // nextFormUpdater 使用函数式更新启用状态。 current => ({ ...current, enabled: !current.enabled }), ); // handleToggleChannelPassword 切换独立 SMTP 密码明文显示。 const handleToggleChannelPassword = () => setShowChannelSmtpPassword( // nextPasswordState 使用函数式更新密码显示状态。 value => !value, ); // renderGuideStep 渲染当前渠道指南中的单个步骤。 const renderGuideStep = (step: string, index: number) =>
  • {step}
  • ; if (!showModal) return null; return createPortal(

    {editing ? '编辑通知渠道' : '新建通知渠道'}

    {(Object.keys(notificationChannelTypes) as NotificationChannelType[]).map( // type 是当前渠道类型选择项。 type => { // channelMeta 是当前类型的展示元数据。 const channelMeta = notificationChannelTypes[type]; // TypeIcon 是当前类型的图标组件。 const TypeIcon = channelMeta.icon; // selected 表示当前类型是否已选中。 const selected = form.type === type; return ; }, )}
    如何获取 {meta.label} 配置?
      {meta.guide.steps.map(renderGuideStep)}
    {meta.guide.urlFormat &&
    格式示例:{meta.guide.urlFormat}
    }{meta.guide.note &&

    💡 {meta.guide.note}

    }
    {meta.fields.map( // field 是当前渠道配置字段。 field =>
    {field.help &&

    {field.help}

    }
    , )}
    {form.type === 'email' && (
    SMTP 来源

    {form.config.use_custom_smtp === true ? '当前渠道使用一套完整、独立的发件配置。' : '当前渠道完整继承系统 SMTP,只单独设置收件邮箱。'}

    {form.config.use_custom_smtp === true && (
    )}
    )}
    , document.body, ); };