// Overview widget: the notification subsystem's in-memory delivery history // (most-recent-first, lost on restart). Opt-in (hidden by default); polls // /api/notifications/recent. No per-widget config. import { useEffect, useState } from 'react'; import { Bell, CheckCircle2, XCircle } from 'lucide-react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { SkeletonSwap } from '@/components/interior/skeleton-swap'; import { cn } from '@/lib/utils'; import { useApi } from '@/lib/api'; import { useTheme } from '@/contexts/ThemeContext'; import type { NotificationDeliveryRecord } from '@/types'; const POLL_MS = 15_000; export function DeliveriesWidget() { const api = useApi(); const { formatClock } = useTheme(); const [records, setRecords] = useState(null); const [loadError, setLoadError] = useState(null); useEffect(() => { let active = true; let inFlight = false; const pull = () => { // Skip while the tab is backgrounded, or if a request is still in flight // (a slow endpoint must not stack overlapping polls). #185. if (inFlight || document.hidden) return; inFlight = true; api.notifications .recent(50) .then((r) => { if (active) { setLoadError(null); setRecords(r); } }) .catch((error) => { console.error('load recent notification deliveries failed', error); if (active) { setLoadError(error instanceof Error ? error.message : '加载最近投递失败'); setRecords((prev) => prev ?? []); } }) .finally(() => { inFlight = false; }); }; void pull(); const id = window.setInterval(() => void pull(), POLL_MS); // The interval keeps firing but no-ops while hidden, so refresh immediately // when the tab becomes visible again to shed any staleness. const onVisible = () => { if (!document.hidden) pull(); }; document.addEventListener('visibilitychange', onVisible); return () => { active = false; window.clearInterval(id); document.removeEventListener('visibilitychange', onVisible); }; }, [api]); return ( 最近投递 {records !== null ? ( loadError && records.length === 0 ? (

{loadError}

) : records.length === 0 ? (

暂无投递记录

) : (
{loadError && (

{loadError};当前显示上次成功读取的投递记录。

)} {records.map((r, i) => (
{r.ok ? ( ) : ( )} {formatClock(r.time)} {r.event === 'online' ? '上线' : '下线'} {r.uin} {r.channelId} {!r.ok && r.error && · {r.error}}
))}
) ) : null}
); }