import { useCallback, useEffect, useRef, useState } from "react" import { useNavigate } from "react-router-dom" import { Bell, BellOff, CheckCheck, Mail, MailOpen, Trash2 } from "lucide-react" import { toast } from "sonner" import { Badge } from "../ui/badge" import { cn } from "../../lib/utils" import { SpinnerIcon } from "../ui/spinner-icon" import { getNotificationById } from "../../api/notifications.api" import { useNotifications } from "../../hooks/useNotifications" import { NotificationDetailDialog } from "../notifications/NotificationDetailDialog" import { NotificationDeleteDialog } from "../notifications/NotificationDeleteDialog" import { DEFAULT_NOTIFICATION_TYPE_CONFIG, formatNotificationTimestamp, NOTIFICATION_TYPE_CONFIG, } from "../../lib/notificationDisplay" import { getNotificationMessage, getNotificationTitle, type Notification } from "../../types/notification.types" function NotificationItem({ notification, onOpen, onMarkRead, onMarkUnread, onDelete, }: { notification: Notification onOpen: (notification: Notification) => void onMarkRead: (id: string) => void onMarkUnread: (id: string) => void onDelete: (notification: Notification) => void }) { const cfg = NOTIFICATION_TYPE_CONFIG[notification.type] ?? DEFAULT_NOTIFICATION_TYPE_CONFIG const Icon = cfg.icon return ( onOpen(notification)} > {!notification.is_read && ( )} {getNotificationTitle(notification) || "Notification"} {getNotificationMessage(notification) || "No preview text available."} {formatNotificationTimestamp(notification.timestamp)} { e.stopPropagation() if (notification.is_read) { onMarkUnread(notification.id) } else { onMarkRead(notification.id) } }} aria-label={notification.is_read ? "Mark as unread" : "Mark as read"} > {notification.is_read ? ( ) : ( )} { e.stopPropagation() onDelete(notification) }} aria-label="Delete notification" > ) } export function NotificationDropdown() { const [open, setOpen] = useState(false) const [detailOpen, setDetailOpen] = useState(false) const [detailLoading, setDetailLoading] = useState(false) const [detailError, setDetailError] = useState(false) const [selectedNotification, setSelectedNotification] = useState(null) const [selectedNotificationId, setSelectedNotificationId] = useState(null) const [notificationPendingDelete, setNotificationPendingDelete] = useState(null) const containerRef = useRef(null) const navigate = useNavigate() const { notifications, unreadCount, loading, markOneRead, markOneUnread, markAllAsRead, refresh, } = useNotifications() const loadNotificationDetail = useCallback(async (id: string, markReadIfNeeded: boolean) => { setDetailLoading(true) setDetailError(false) setSelectedNotification(null) setSelectedNotificationId(id) setDetailOpen(true) try { const res = await getNotificationById(id) if (!res.data) { setDetailError(true) toast.error("Notification not found") return } setSelectedNotification(res.data) if (markReadIfNeeded && !res.data.is_read) { void markOneRead(id) } } catch { setDetailError(true) toast.error("Failed to load notification details") } finally { setDetailLoading(false) } }, [markOneRead]) const handleOpenNotification = useCallback( (notification: Notification) => { setOpen(false) void loadNotificationDetail(notification.id, !notification.is_read) }, [loadNotificationDetail], ) const handleNotificationDeleted = useCallback((id: string) => { if (selectedNotificationId === id) { setDetailOpen(false) setSelectedNotification(null) setSelectedNotificationId(null) } setNotificationPendingDelete(null) refresh() }, [selectedNotificationId, refresh]) useEffect(() => { function handleMouseDown(e: MouseEvent) { if (containerRef.current && !containerRef.current.contains(e.target as Node)) { setOpen(false) } } if (open) { document.addEventListener("mousedown", handleMouseDown) } return () => document.removeEventListener("mousedown", handleMouseDown) }, [open]) return ( <> setOpen((prev) => !prev)} > {unreadCount > 0 && ( {unreadCount > 99 ? "99+" : unreadCount} )} {open && ( Notifications {unreadCount > 0 && ( {unreadCount} )} {unreadCount > 0 && ( Mark all read )} {loading ? ( ) : notifications.length === 0 ? ( No notifications ) : ( {notifications.map((n) => ( { setOpen(false) setNotificationPendingDelete(notification) }} /> ))} )} { setOpen(false) navigate("/notifications") }} > View all notifications )} void loadNotificationDetail(selectedNotificationId, false) : undefined } onDelete={ selectedNotification ? () => setNotificationPendingDelete(selectedNotification) : undefined } /> { if (!open) setNotificationPendingDelete(null) }} onDeleted={handleNotificationDeleted} /> > ) }
{getNotificationTitle(notification) || "Notification"}
{getNotificationMessage(notification) || "No preview text available."}
{formatNotificationTimestamp(notification.timestamp)}
No notifications