feat(notifications): P4 WebUI — channel manager, per-account opt-in, deliveries widget

- API client: notifications surface (getConfig/saveConfig/recent/test) + types.
- Settings → 通知 tab: global channel CRUD (id/name/url/body-template/enabled) +
  global debounce window, per-channel test-send, dirty-tracked save; invalid
  channels (bad slug / non-http URL) are dropped server-side and surfaced.
- Per-account 通用设置: opt-in toggles bound to notifications.channelIds, riding
  the ConfigPage's existing debounced autosave; stale opt-ins preserved.
- Overview: opt-in 最近投递 widget (polls /api/notifications/recent, read-only).
  Hidden-by-default set is now DERIVED from the catalogue (defaultVisible) and
  shared by desktop + mobile reconcile — replaces the hardcoded STATIC_WIDGET_IDS
  so a new opt-in widget can't reappear unbidden on existing mobile dashboards.

Typecheck + build green. Adversarial review: 1 should-fix (mobile hidden-set) +
2 nits found, fixed/triaged, and re-verified clean.
This commit is contained in:
DrSmoothl committed 2026-06-16 19:53:31 +08:00
1 parent c0af18e1c2
commit 0b7fb0c69d
12 files changed
+509 -7

No files matched your search

@@ -6,6 +6,7 @@
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { ToggleSwitch } from '@/components/ui/toggle-switch';
import { NotificationOptIn } from '@/components/config/notification-opt-in';
import type { OneBotConfig, StatusCommandConfig } from '@/types';
interface GeneralSettingsTabProps {
@@ -83,6 +84,11 @@ export function GeneralSettingsTab({ config, onChange }: GeneralSettingsTabProps
</p>
</div>
</div>
<NotificationOptIn
selectedIds={config.notifications?.channelIds ?? []}
onChange={(channelIds) => onChange({ ...config, notifications: { channelIds } })}
/>
</div>
);
}
@@ -0,0 +1,84 @@
// 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.
import { useEffect, useState } from 'react';
import { Loader2 } from 'lucide-react';
import { Label } from '@/components/ui/label';
import { ToggleSwitch } from '@/components/ui/toggle-switch';
import { useApi } from '@/lib/api';
import type { NotificationChannel } from '@/types';
interface NotificationOptInProps {
selectedIds: string[];
onChange: (channelIds: string[]) => void;
}
export function NotificationOptIn({ selectedIds, onChange }: NotificationOptInProps) {
const api = useApi();
const [channels, setChannels] = useState<NotificationChannel[] | null>(null);
useEffect(() => {
let cancelled = false;
void (async () => {
try {
const cfg = await api.notifications.getConfig();
if (!cancelled) setChannels(cfg.channels);
} catch {
if (!cancelled) setChannels([]);
}
})();
return () => {
cancelled = true;
};
}, [api]);
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 selected = new Set(selectedIds);
return (
<div className="flex flex-col gap-3 rounded-lg border bg-card/40 p-4">
<div>
<Label>上下线通知渠道</Label>
<p className="mt-1 text-[11px] leading-relaxed text-muted-foreground">
该账号上线 / 下线时向选中的渠道推送通知。渠道在「设置 → 通知」中全局定义。
</p>
</div>
{channels === null ? (
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<Loader2 className="size-3.5 animate-spin" /> 加载渠道…
</div>
) : channels.length === 0 ? (
<p className="text-[11px] text-muted-foreground">还没有全局渠道——先到「设置 → 通知」添加。</p>
) : (
<div className="flex flex-col divide-y">
{channels.map((ch) => (
<div key={ch.id} className="flex items-center justify-between gap-3 py-2 first:pt-0 last:pb-0">
<div className="min-w-0">
<span className="text-sm">{ch.name || ch.id}</span>
{!ch.enabled && <span className="ml-2 text-[10px] text-muted-foreground">(已全局禁用)</span>}
<p className="truncate text-[11px] text-muted-foreground">{ch.url}</p>
</div>
<ToggleSwitch
value={selected.has(ch.id)}
onChange={(v) => toggle(ch.id, v)}
ariaLabel={`为该账号启用渠道 ${ch.name || ch.id}`}
/>
</div>
))}
</div>
)}
</div>
);
}