import { useState } from "react"; import { Bell, AlertCircle, UserPlus, Megaphone } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Separator } from "@/components/ui/separator"; import { useAdminNotifications } from "@/contexts/AdminNotificationContext"; import { cn } from "@/lib/utils"; import { useDateFormat } from "@/hooks/useDateFormat"; const TYPE_ICON = { task_overdue: AlertCircle, user_registration: UserPlus, announcement: Megaphone, }; function NotificationIcon({ type, className }) { const Icon = TYPE_ICON[type] ?? Bell; return ; } function timeAgo(dateStr) { const diff = Date.now() - new Date(dateStr).getTime(); const m = Math.floor(diff / 60_000); if (m < 1) return "just now"; if (m < 60) return `${m}m ago`; const h = Math.floor(m / 60); if (h < 24) return `${h}h ago`; return `${Math.floor(h / 24)}d ago`; } export default function NotificationBell() { const { notifications, unseenCount, loading, fetchNotifications, markSeen, markAllSeen } = useAdminNotifications(); const { fmtDateTime } = useDateFormat(); const [selected, setSelected] = useState(null); function handleOpen(open) { if (open) fetchNotifications(); } function handleClickNotification(n) { if (!n.seen) markSeen(n.notification_id); setSelected(n); } return ( <> {unseenCount > 0 && ( {unseenCount > 99 ? "99+" : unseenCount} )} Notifications {unseenCount > 0 && ( Mark all as read )} {loading && notifications.length === 0 ? ( Loading... ) : notifications.length === 0 ? ( No notifications yet. ) : ( {notifications.map((n, i) => ( handleClickNotification(n)} className={cn( "w-full text-left px-4 py-3 hover:bg-muted/50 transition-colors", !n.seen && "bg-blue-50 dark:bg-blue-950/20" )} > {!n.seen && ( )} {n.title} {n.message} {timeAgo(n.createdAt)} {i < notifications.length - 1 && } ))} )} {/* Detail dialog — outside Popover so it isn't clipped */} !open && setSelected(null)}> {selected?.title} {selected?.message} {selected?.type?.replace(/_/g, ' ')} {selected ? fmtDateTime(selected.createdAt) : ""} > ); }
Loading...
No notifications yet.
{n.title}
{n.message}
{timeAgo(n.createdAt)}