From 5eaac0870ca437a7883138ae5e60cb39e8cff02e Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sun, 28 Jun 2026 01:23:12 +0800 Subject: [PATCH] feat(webui): scale notification-channel opt-in to large lists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The per-account 上下线通知渠道 picker rendered every global channel as a flat toggle row, so 40+ channels produced an unbounded, messy wall inside 通用设置. Rework it into a collapsible control: - collapsed: one-line summary — selected count badge + selected channels as removable chips (cap 8, then "+N 更多") - expanded: description + name/url search filter + 全选/清空 bulk actions + a height-bounded ScrollArea list (max-h-64) so it never blows up - globally-disabled channels stay locked off; data flow unchanged Reuses Badge / ScrollArea / ToggleSwitch / Input. --- .../components/config/notification-opt-in.tsx | 199 +++++++++++++++--- 1 file changed, 170 insertions(+), 29 deletions(-) diff --git a/packages/webui/src/components/config/notification-opt-in.tsx b/packages/webui/src/components/config/notification-opt-in.tsx index 47431c6c..7ea11410 100644 --- a/packages/webui/src/components/config/notification-opt-in.tsx +++ b/packages/webui/src/components/config/notification-opt-in.tsx @@ -2,11 +2,20 @@ // 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. -import { useEffect, useState } from 'react'; -import { Loader2 } from 'lucide-react'; +// +// 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 { useEffect, useMemo, useState } from 'react'; +import { AnimatePresence, motion } from 'motion/react'; +import { ChevronDown, Loader2, Search, X } from 'lucide-react'; +import { Badge } from '@/components/ui/badge'; +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 { @@ -14,9 +23,14 @@ interface NotificationOptInProps { 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 [expanded, setExpanded] = useState(false); + const [query, setQuery] = useState(''); useEffect(() => { let cancelled = false; @@ -33,6 +47,8 @@ export function NotificationOptIn({ selectedIds, onChange }: NotificationOptInPr }; }, [api]); + const selected = useMemo(() => new Set(selectedIds), [selectedIds]); + const toggle = (id: string, on: boolean) => { const set = new Set(selectedIds); if (on) set.add(id); @@ -44,43 +60,168 @@ export function NotificationOptIn({ selectedIds, onChange }: NotificationOptInPr onChange([...known, ...extra]); }; - const selected = new Set(selectedIds); + 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) => (c.name || c.id).toLowerCase().includes(q) || c.url.toLowerCase().includes(q)); + }, [channels, query]); + + const total = channels?.length ?? 0; + const selectedCount = selectedIds.length; return ( -
-
- -

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

-
+
+ {/* header — toggles the editor open/closed */} + {channels === null ? (
加载渠道…
- ) : channels.length === 0 ? ( + ) : total === 0 ? (

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

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

{ch.url}

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

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

+ ) : ( +
+ {selectedChips.slice(0, CHIP_CAP).map((ch) => ( + + {ch.name} + + + ))} + {selectedCount > CHIP_CAP && ( + + )}
- toggle(ch.id, v)} - ariaLabel={`为该账号启用渠道 ${ch.name || ch.id}`} - disabled={!ch.enabled} - /> -
- ))} -
+ ))} + + {/* 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.url}

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