import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Search, Bell, User, Settings, LogOut } from 'lucide-react'; import { useUser } from '../../context/UserContext'; import { useAuth } from '../../context/AuthContext'; interface Notification { id: string; type: 'timecard' | 'schedule' | 'payroll' | 'general'; title: string; time: string; path: string; } export const TopBar: React.FC = () => { const { user } = useUser(); const { logout } = useAuth(); const navigate = useNavigate(); const [showProfileMenu, setShowProfileMenu] = useState(false); const [showNotifications, setShowNotifications] = useState(false); // Initialize notifications from localStorage or use default const getDefaultNotifications = (): Notification[] => { const timecardPath = ['manager', 'hr', 'admin'].includes(user.role) ? '/team-timecards' : '/timecards'; const schedulePath = ['manager', 'hr', 'admin'].includes(user.role) ? '/team-schedules' : '/schedule'; return [ { id: '1', type: 'timecard', title: 'New timecard submitted', time: '2 hours ago', path: timecardPath, }, { id: '2', type: 'schedule', title: 'Schedule updated', time: '5 hours ago', path: schedulePath, }, { id: '3', type: 'payroll', title: 'Payroll reminder', time: '1 day ago', path: '/payroll-runs', }, ]; }; const [notifications, setNotifications] = useState(() => { const saved = localStorage.getItem('notifications'); if (saved) { try { const parsed = JSON.parse(saved); // If saved notifications exist, use them; otherwise use defaults return parsed.length > 0 ? parsed : getDefaultNotifications(); } catch { return getDefaultNotifications(); } } return getDefaultNotifications(); }); // Save notifications to localStorage whenever they change React.useEffect(() => { localStorage.setItem('notifications', JSON.stringify(notifications)); }, [notifications]); const handleLogout = () => { logout(); navigate('/login'); }; const handleNotificationClick = (path: string) => { navigate(path); setShowNotifications(false); }; const handleDismissNotification = (id: string, e: React.MouseEvent) => { e.stopPropagation(); // Prevent navigation when clicking dismiss setNotifications(prev => prev.filter(n => n.id !== id)); }; const handleClearAllNotifications = () => { setNotifications([]); }; // Close menus when clicking outside React.useEffect(() => { const handleClickOutside = (event: MouseEvent) => { const target = event.target as HTMLElement; if (!target.closest('.notification-menu') && !target.closest('.notification-button')) { setShowNotifications(false); } if (!target.closest('.profile-menu') && !target.closest('.profile-button')) { setShowProfileMenu(false); } }; document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); }, []); return (
A
Axion
{showNotifications && (

Notifications

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

No notifications

) : ( notifications.map((notification) => { const getColor = () => { switch (notification.type) { case 'timecard': return 'bg-blue-500'; case 'schedule': return 'bg-green-500'; case 'payroll': return 'bg-yellow-500'; default: return 'bg-gray-500'; } }; return (
handleNotificationClick(notification.path)} >

{notification.title}

{notification.time}

); }) )}
{notifications.length > 0 && (
)}
)}
{showProfileMenu && (

)}
); };