diff --git a/packages/webui/src/components/config/notification-opt-in.tsx b/packages/webui/src/components/config/notification-opt-in.tsx index 3c472b41..47431c6c 100644 --- a/packages/webui/src/components/config/notification-opt-in.tsx +++ b/packages/webui/src/components/config/notification-opt-in.tsx @@ -71,9 +71,12 @@ export function NotificationOptIn({ selectedIds, onChange }: NotificationOptInPr

{ch.url}

toggle(ch.id, v)} ariaLabel={`为该账号启用渠道 ${ch.name || ch.id}`} + disabled={!ch.enabled} /> ))} diff --git a/packages/webui/src/components/settings/notification-channel-dialog.tsx b/packages/webui/src/components/settings/notification-channel-dialog.tsx new file mode 100644 index 00000000..8f4c498b --- /dev/null +++ b/packages/webui/src/components/settings/notification-channel-dialog.tsx @@ -0,0 +1,176 @@ +// 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 { Button } from '@/components/ui/button'; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from '@/components/ui/dialog'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +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 ( + + + + {isEdit ? '编辑渠道' : '新建渠道'} + 账号上线 / 下线时向该 Webhook POST 一条渲染后的通知。 + + +
+
+ patch({ id: v })} + /> + patch({ name: v })} + /> +
+ + patch({ url: v })} + /> + +
+ +