// 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 { 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); 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) setRecords(r); }) .catch(() => { if (active) 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 ? (

加载中…

) : records.length === 0 ? (

暂无投递记录

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