import { useState, useRef, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { Bell, Check, X } from 'lucide-react'; import { useNotifications } from '../contexts/NotificationsContext'; import { timeAgo } from '../utils/timeAgo'; // Severity → dot color, mirroring the email/brand palette. const SEVERITY_DOT = { critical: '#fb6f6f', warning: '#f5b945', success: '#3ddc97', info: '#6d7cff', test: '#9aa1b2', }; export default function NotificationBell() { const ctx = useNotifications(); const [open, setOpen] = useState(false); const ref = useRef(null); const navigate = useNavigate(); // The provider is only mounted for authenticated users; render nothing otherwise. const { items = [], unreadCount = 0, markRead, markAllRead, refresh, dismissNotice } = ctx || {}; useEffect(() => { if (!open) return undefined; const onDocClick = (e) => { if (ref.current && !ref.current.contains(e.target)) setOpen(false); }; const onEsc = (e) => { if (e.key === 'Escape') setOpen(false); }; document.addEventListener('mousedown', onDocClick); document.addEventListener('keydown', onEsc); return () => { document.removeEventListener('mousedown', onDocClick); document.removeEventListener('keydown', onEsc); }; }, [open]); if (!ctx) return null; const toggle = () => { const next = !open; setOpen(next); if (next && refresh) refresh(); }; const onItemClick = (item) => { // System notices route to their fix; bus notifications just mark read. if (item.kind === 'notice') { setOpen(false); if (item.action_path) navigate(item.action_path); return; } if (!item.read && markRead) markRead(item.delivery_id); }; const badge = unreadCount > 99 ? '99+' : unreadCount; return (
{open && (
Notifications {unreadCount > 0 && ( )}
{items.length === 0 ? (
You’re all caught up.
) : ( items.map((item) => ( item.kind === 'notice' ? (
) : ( ) )) )}
)}
); }