"use client" import { Bell } from "lucide-react" import { Button } from "@/components/ui/button" import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover" import { useEffect, useState } from "react" import { getApiUrl } from "@/lib/api-config" import Link from "next/link" import { cn } from "@/lib/utils" interface Notification { id: number type: string title: string message: string link?: string is_read: boolean created_at: string } export function NotificationBell() { const [notifications, setNotifications] = useState([]) const [unreadCount, setUnreadCount] = useState(0) const [open, setOpen] = useState(false) const fetchNotifications = () => { const token = localStorage.getItem("token") if (!token) return fetch(`${getApiUrl()}/notifications/`, { headers: { Authorization: `Bearer ${token}` } }) .then(res => res.json()) .then(data => { setNotifications(data) setUnreadCount(data.filter((n: Notification) => !n.is_read).length) }) .catch(console.error) } useEffect(() => { fetchNotifications() // Poll every 60 seconds const interval = setInterval(fetchNotifications, 60000) return () => clearInterval(interval) }, []) const markAsRead = (id: number) => { const token = localStorage.getItem("token") if (!token) return fetch(`${getApiUrl()}/notifications/${id}/read`, { method: "POST", headers: { Authorization: `Bearer ${token}` } }).then(() => { setNotifications(prev => prev.map(n => n.id === id ? { ...n, is_read: true } : n)) setUnreadCount(prev => Math.max(0, prev - 1)) }) } const markAllRead = () => { const token = localStorage.getItem("token") if (!token) return fetch(`${getApiUrl()}/notifications/mark-all-read`, { method: "POST", headers: { Authorization: `Bearer ${token}` } }).then(() => { setNotifications(prev => prev.map(n => ({ ...n, is_read: true }))) setUnreadCount(0) }) } return (

Notifications

{unreadCount > 0 && ( )}
{notifications.length === 0 ? (
No notifications
) : ( notifications.map((notification) => (
{ if (!notification.is_read) markAsRead(notification.id) }} >

{notification.title}

{notification.message}

{new Date(notification.created_at).toLocaleDateString()}

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