// Per-account opt-in to the GLOBAL notification channels. Renders the channels // defined in 设置 → 通知 as toggles; the selection maps to // OneBotConfig.notifications.channelIds. Lives inside the account's 通用设置 tab, // so toggling marks the config dirty and rides the parent's debounced autosave. // // Scales to many channels (40+): collapsed by default to a one-line summary of // selected channels as removable chips; expanded into a searchable, bulk- // actionable, height-bounded scroll list so it never blows up the page. import { useCallback, useEffect, useMemo, useState } from 'react'; import { AnimatePresence, motion } from 'motion/react'; import { ChevronDown, Search, X } from 'lucide-react'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { SkeletonSwap } from '@/components/interior/skeleton-swap'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { ScrollArea } from '@/components/ui/scroll-area'; import { ToggleSwitch } from '@/components/ui/toggle-switch'; import { useApi } from '@/lib/api'; import { cn } from '@/lib/utils'; import type { NotificationChannel } from '@/types'; interface NotificationOptInProps { selectedIds: string[]; onChange: (channelIds: string[]) => void; } /** How many summary chips to show before collapsing the rest into "+N". */ const CHIP_CAP = 8; export function NotificationOptIn({ selectedIds, onChange }: NotificationOptInProps) { const api = useApi(); const [channels, setChannels] = useState(null); const [loadError, setLoadError] = useState(null); const [expanded, setExpanded] = useState(false); const [query, setQuery] = useState(''); const load = useCallback(async () => { setChannels(null); setLoadError(null); try { const cfg = await api.notifications.getConfig(); setChannels(cfg.channels); } catch (error) { console.error('load notification opt-ins failed', error); setLoadError(error instanceof Error ? error.message : '加载通知渠道失败'); setChannels([]); } }, [api]); useEffect(() => { void load(); }, [load]); const selected = useMemo(() => new Set(selectedIds), [selectedIds]); const toggle = (id: string, on: boolean) => { const set = new Set(selectedIds); if (on) { set.add(id); } else { set.delete(id); } // Order known channels by the global list; keep any stale ids (channel // deleted globally but still opted-in) at the end rather than dropping them. const known = (channels ?? []).map((c) => c.id).filter((cid) => set.has(cid)); const extra = selectedIds.filter((sid) => set.has(sid) && !known.includes(sid)); onChange([...known, ...extra]); }; const selectAll = () => { // Only enabled channels can ever fire, so "全选" maps to those. onChange((channels ?? []).filter((c) => c.enabled).map((c) => c.id)); }; const clearAll = () => onChange([]); // Resolve the selected ids back to channels for the collapsed chip summary. // Stale ids (channel deleted globally) fall back to showing the raw id. const selectedChips = useMemo(() => { const byId = new Map((channels ?? []).map((c) => [c.id, c])); return selectedIds.map((id) => ({ id, name: byId.get(id)?.name || id })); }, [channels, selectedIds]); const filtered = useMemo(() => { const list = channels ?? []; const q = query.trim().toLowerCase(); if (!q) { return list; } return list.filter((c) => { const hay = [c.name, c.id, c.url, c.smtpHost, c.to, c.from] .filter(Boolean) .join(' ') .toLowerCase(); return hay.includes(q); }); }, [channels, query]); const total = channels?.length ?? 0; const selectedCount = selectedIds.length; return (
{/* header — toggles the editor open/closed */} {channels !== null ? ( loadError ? (
{loadError}
) : total === 0 ? (

还没有全局渠道——先到「设置 → 通知」添加。

) : ( <> {/* collapsed: selected channels as removable chips */} {!expanded && (selectedCount === 0 ? (

未选择任何渠道。展开以选择。

) : (
{selectedChips.slice(0, CHIP_CAP).map((ch) => ( {ch.name} ))} {selectedCount > CHIP_CAP && ( )}
))} {/* expanded: search + bulk + bounded scroll list */} {expanded && (

该账号上线 / 下线时向选中的渠道推送通知。渠道在「设置 → 通知」中全局定义。

setQuery(e.target.value)} placeholder="搜索渠道…" className="h-8 pl-8 text-xs" />
{filtered.length === 0 ? (

无匹配渠道

) : (
{filtered.map((ch) => (
{ch.name || ch.id} {!ch.enabled && ( (已全局禁用) )}

{ch.type === 'email' ? ch.to || ch.smtpHost || '邮件' : ch.url}

toggle(ch.id, v) } ariaLabel={`为该账号启用渠道 ${ch.name || ch.id}`} disabled={!ch.enabled} />
))}
)}
)}
) ) : null}
); }