import { useState, useEffect } from 'react'; import { Bell, RefreshCw, HelpCircle, User as UserIcon } from 'lucide-react'; import { Button } from '../ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '../ui/dropdown-menu'; import { Badge } from '../ui/badge'; import { toast } from 'sonner'; import { useSelector } from 'react-redux'; import { RootState } from '../../store'; import { notificationService, Notification } from '../../services/notification.service'; import { useSocket } from '../../context/SocketContext'; import { formatDistanceToNow } from 'date-fns'; interface HeaderProps { title: string; onRefresh?: () => void; } export function Header({ title, onRefresh }: HeaderProps) { const { user: currentUser } = useSelector((state: RootState) => state.auth); const { socket } = useSocket(); const [notifications, setNotifications] = useState([]); const [unreadCount, setUnreadCount] = useState(0); useEffect(() => { const fetchNotifications = async () => { try { const res: any = await notificationService.getNotifications(1, 15); if (res.success) { setNotifications(res.data); if (res.pagination && res.pagination.unreadCount !== undefined) { setUnreadCount(res.pagination.unreadCount); } else { setUnreadCount(res.data.filter((n: any) => !n.isRead).length); } } } catch (error) { console.error('Fetch notifications error:', error); } }; fetchNotifications(); }, []); useEffect(() => { if (socket) { socket.on('notification', (newNotification: Notification) => { setNotifications(prev => [newNotification, ...prev].slice(0, 15)); setUnreadCount(prev => prev + 1); toast(newNotification.title, { description: newNotification.message, action: newNotification.link ? { label: 'View', onClick: () => window.location.href = newNotification.link! } : undefined }); }); return () => { socket.off('notification'); }; } }, [socket]); const handleNotificationClick = async (notif: Notification) => { try { if (!notif.isRead) { const res: any = await notificationService.markAsRead(notif.id); if (res.success) { setNotifications(prev => prev.map(n => n.id === notif.id ? { ...n, isRead: true } : n)); setUnreadCount(prev => Math.max(0, prev - 1)); } } if (notif.link) { window.location.href = notif.link; } } catch (error) { console.error('Notification click error:', error); } }; const handleMarkAllAsRead = async () => { try { const res: any = await notificationService.markAllAsRead(); if (res.success) { setNotifications(prev => prev.map(n => ({ ...n, isRead: true }))); setUnreadCount(0); } } catch (error) { console.error('Mark all as read error:', error); } }; return (

{title}

Manage and track dealership applications

{/* Current User Info */} {currentUser && (

{currentUser.name}

{currentUser.role}

)} {/* Refresh Button */} {onRefresh && ( )} {/* Help */} {/* Notifications */}

Notifications

{unreadCount > 0 && ( )}
{notifications.length === 0 ? (
No notifications yet
) : ( notifications.map((notification) => ( handleNotificationClick(notification)} >

{notification.title}

{notification.message}

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

{!notification.isRead && (
)}
)) )}
); }