"use client" import { useEffect, useState, useRef } from "react" import { Bell, Loader2, CheckCircle2, Clock, XCircle, Mail, Package, ShoppingCart, X, AlertTriangle } from "lucide-react" import { formatDistanceToNow } from "date-fns" import { motion, AnimatePresence } from "framer-motion" import { useTheme } from "@/components/ThemeProvider" interface Notification { id: string type: string title: string message: string read: boolean readAt: string | null createdAt: string metadata?: any } export default function NotificationBell() { const [notifications, setNotifications] = useState([]) const [unreadCount, setUnreadCount] = useState(0) const [loading, setLoading] = useState(true) const [isOpen, setIsOpen] = useState(false) const dropdownRef = useRef(null) const { theme } = useTheme() const isLight = theme === "light" useEffect(() => { fetchNotifications() // Poll for new notifications every 5 seconds const interval = setInterval(() => { fetchNotifications() }, 5000) return () => clearInterval(interval) }, []) // Close dropdown when clicking outside useEffect(() => { function handleClickOutside(event: MouseEvent) { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { setIsOpen(false) } } if (isOpen) { document.addEventListener("mousedown", handleClickOutside) } return () => { document.removeEventListener("mousedown", handleClickOutside) } }, [isOpen]) const fetchNotifications = async () => { try { const res = await fetch("/api/notifications?limit=10") if (!res.ok) throw new Error("Failed to fetch notifications") const data = await res.json() setNotifications(data.notifications || []) setUnreadCount(data.unreadCount || 0) } catch (error) { console.error("Error fetching notifications:", error) } finally { setLoading(false) } } const markAsRead = async (notificationId: string) => { try { await fetch(`/api/notifications/${notificationId}`, { method: "PUT" }) fetchNotifications() // Refresh } catch (error) { console.error("Error marking notification as read:", error) } } const markAllAsRead = async () => { try { await fetch("/api/notifications", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ markAllAsRead: true }) }) fetchNotifications() // Refresh } catch (error) { console.error("Error marking all as read:", error) } } const deleteNotification = async (notificationId: string) => { try { await fetch(`/api/notifications/${notificationId}`, { method: "DELETE" }) fetchNotifications() // Refresh } catch (error) { console.error("Error deleting notification:", error) } } const getNotificationIcon = (type: string) => { switch (type) { case "INVITATION_ACCEPTED": return case "INVITATION_RECEIVED": return case "CONNECTION_STATUS_CHANGED": return case "ORDER_RECEIVED": return case "PRODUCT_UPDATED": return case "SHOPIFY_LOW_STOCK": return default: return } } return (
{isOpen && (

Notifications

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

No notifications yet

) : (
{notifications.map((notification) => (
{getNotificationIcon(notification.type)}

{notification.title}

{notification.message}

{formatDistanceToNow(new Date(notification.createdAt), { addSuffix: true })}

{!notification.read && ( )}
))}
)}
)}
) }