// Modal editor for a notification channel — used for both create and edit, // mirroring NodeEditDialog. Holds a local draft until 保存 (cancel discards). // The channel id is the stable key per-account opt-ins reference, so it's // editable only on create and locked on edit. import { useState } from 'react'; import { Modal } from '@/components/interior/modal'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { ToggleSwitch } from '@/components/ui/toggle-switch'; import { cn } from '@/lib/utils'; import type { NotificationChannel } from '@/types'; const CHANNEL_ID_RE = /^[\w.-]+$/; function isHttpUrl(u: string): boolean { try { const x = new URL(u.trim()); return x.protocol === 'http:' || x.protocol === 'https:'; } catch { return false; } } interface NotificationChannelDialogProps { open: boolean; onOpenChange: (open: boolean) => void; isEdit: boolean; /** Seed draft — a blank channel for create, the existing one for edit. */ initial: NotificationChannel; /** Ids of every other channel, for the duplicate check. */ otherIds: string[]; onSubmit: (channel: NotificationChannel) => void; } export function NotificationChannelDialog(props: NotificationChannelDialogProps) { const { open, onOpenChange, isEdit, initial, otherIds, onSubmit } = props; // Parent unmounts on close, so each open re-seeds via lazy init. const [draft, setDraft] = useState(initial); const patch = (p: Partial) => setDraft({ ...draft, ...p }); const id = draft.id.trim(); const idBlank = id.length === 0; const idBad = !idBlank && (!CHANNEL_ID_RE.test(id) || id.length > 64); const idDup = !idBlank && otherIds.includes(id); const urlBlank = draft.url.trim().length === 0; const urlBad = !urlBlank && !isHttpUrl(draft.url); const canSave = !idBlank && !idBad && !idDup && !urlBlank && !urlBad; const idError = idBlank ? '请填写渠道 ID' : idBad ? '只能用字母 / 数字 / . _ - ,≤64 字符' : idDup ? 'ID 与其它渠道重复' : undefined; const urlError = urlBlank ? '请填写 Webhook URL' : urlBad ? '必须是 http(s) 地址' : undefined; return ( onOpenChange(false)} title={isEdit ? '编辑渠道' : '新建渠道'} description="账号上线 / 下线时向该 Webhook POST 一条渲染后的通知。" closeLabel="关闭渠道编辑弹窗" maxWidth={576} footer={( <> )} >
patch({ id: v })} /> patch({ name: v })} />
patch({ url: v })} />