"use client"; import { useState, useEffect, useRef } from "react"; import { Bell, X, AlertCircle, CheckCircle2, AlertTriangle, Info } 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 { cn } from "@/lib/utils"; import { useNotificationStore, type Notification } from "@/store/notifications"; export function NotificationBell() { const { notifications, unreadCount, markAsRead, markAllAsRead, clearAll, removeNotification } = useNotificationStore(); const [animate, setAnimate] = useState(false); const [selectedNotification, setSelectedNotification] = useState(null); const prevCount = useRef(unreadCount); useEffect(() => { // Only animate if unread count INCREASED if (unreadCount > prevCount.current) { // Defer animation trigger to avoid synchronous state update warning const timer = setTimeout(() => { setAnimate(true); setTimeout(() => setAnimate(false), 1000); }, 0); return () => clearTimeout(timer); } prevCount.current = unreadCount; }, [unreadCount]); return ( <>

Notifications

{notifications.length > 0 && (
)}
{notifications.length === 0 ? (

No notifications yet

) : (
{notifications.map((notification) => (
{ markAsRead(notification.id); setSelectedNotification(notification); }} >

{notification.title}

{notification.message}

{formatTimestamp(notification.timestamp)}

))}
)}
!open && setSelectedNotification(null)}> {selectedNotification?.type === 'error' && } {selectedNotification?.type === 'success' && } {selectedNotification?.type === 'warning' && } {selectedNotification?.type === 'info' && } {selectedNotification?.title} {formatTimestamp(selectedNotification?.timestamp || new Date())}
{selectedNotification?.message}
{selectedNotification?.link && ( )}
); } function formatTimestamp(date: Date): string { const now = new Date(); const diff = now.getTime() - date.getTime(); const seconds = Math.floor(diff / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); if (days > 0) return `${days}d ago`; if (hours > 0) return `${hours}h ago`; if (minutes > 0) return `${minutes}m ago`; return "Just now"; } // Helper function to trigger notifications from anywhere in the app export function sendNotification(notification: { title: string; message: string; type: "info" | "success" | "warning" | "error"; autoClose?: boolean; duration?: number; link?: string; actionLabel?: string; }) { useNotificationStore.getState().addNotification(notification); }