import { useState, useRef, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { AppBar, Toolbar, IconButton, Box, InputBase, Badge, Avatar, Typography, Menu, MenuItem, Divider, ListItemIcon, ListItemText, Tooltip, Button, Stack, Select, alpha } from '@mui/material'; import MenuIcon from '@mui/icons-material/Menu'; import SearchIcon from '@mui/icons-material/Search'; import NotificationsNoneIcon from '@mui/icons-material/NotificationsNone'; import PersonOutlineIcon from '@mui/icons-material/PersonOutline'; import SettingsOutlinedIcon from '@mui/icons-material/SettingsOutlined'; import LogoutIcon from '@mui/icons-material/Logout'; import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown'; import PlaceOutlinedIcon from '@mui/icons-material/PlaceOutlined'; import Inventory2OutlinedIcon from '@mui/icons-material/Inventory2Outlined'; import TwoWheelerOutlinedIcon from '@mui/icons-material/TwoWheelerOutlined'; import PaymentsOutlinedIcon from '@mui/icons-material/PaymentsOutlined'; import AssignmentOutlinedIcon from '@mui/icons-material/AssignmentOutlined'; import DoneAllIcon from '@mui/icons-material/DoneAll'; import Logo from '@/components/Logo'; import { locations } from '@/data/mock'; import { useFilters } from '@/store/Filters'; const RED = '#C01227'; // brand accent (reserved for attention: avatar, unread dots) const INITIAL_NOTIFICATIONS = [ { id: 1, icon: Inventory2OutlinedIcon, title: 'New order #ORD-10482 placed', time: '2 min ago', to: '/orders', read: false }, { id: 2, icon: TwoWheelerOutlinedIcon, title: 'Rider Imran went online', time: '18 min ago', to: '/fleet', read: false }, { id: 3, icon: PaymentsOutlinedIcon, title: 'Invoice INV-2041 marked paid', time: '1 hr ago', to: '/invoice', read: false }, { id: 4, icon: AssignmentOutlinedIcon, title: 'MileTruth AI re-optimized 41 routes', time: '3 hrs ago', to: '/dispatch', read: true } ]; export default function Header({ onToggle }) { const navigate = useNavigate(); const [account, setAccount] = useState(null); const [notifAnchor, setNotifAnchor] = useState(null); const [notifications, setNotifications] = useState(INITIAL_NOTIFICATIONS); const [search, setSearch] = useState(''); const { location, setLocation } = useFilters(); // global location — single source of truth const searchRef = useRef(null); const unread = notifications.filter((n) => !n.read).length; // ⌘K / Ctrl+K focuses global search useEffect(() => { const onKey = (e) => { if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') { e.preventDefault(); searchRef.current?.focus(); } }; window.addEventListener('keydown', onKey); return () => window.removeEventListener('keydown', onKey); }, []); const openNotif = (e) => setNotifAnchor(e.currentTarget); const closeNotif = () => setNotifAnchor(null); const markAllRead = () => setNotifications((prev) => prev.map((n) => ({ ...n, read: true }))); const onNotifClick = (n) => { setNotifications((prev) => prev.map((x) => (x.id === n.id ? { ...x, read: true } : x))); closeNotif(); navigate(n.to); }; const submitSearch = (e) => { e.preventDefault(); const q = search.trim(); if (q) navigate(`/orders?q=${encodeURIComponent(q)}`); }; return ( t.zIndex.drawer + 1, borderBottom: '1px solid', borderColor: 'grey.200' }} > {/* LEFT — hamburger + brand (equal-flex zone) */} navigate('/dashboard')} sx={{ display: 'flex', alignItems: 'center', cursor: 'pointer', flexShrink: 0 }} > {/* CENTER — global search, the primary nav element (fixed width = stays truly centered) */} setSearch(e.target.value)} placeholder="Search orders, shipments, riders, customers…" sx={{ color: 'grey.800', fontSize: '0.875rem', flex: 1 }} inputProps={{ 'aria-label': 'search' }} /> ⌘K {/* RIGHT — location + notifications + profile (equal-flex zone, right-aligned) */} setAccount(e.currentTarget)} sx={{ display: 'flex', alignItems: 'center', gap: 1, cursor: 'pointer', py: 0.5, px: 0.5, borderRadius: 2, '&:hover': { bgcolor: 'grey.100' } }} > AD Aman Deshmukh Operations Admin {/* Notifications dropdown */} Notifications {notifications.length === 0 && ( )} {notifications.map((n) => { const Icon = n.icon; return ( onNotifClick(n)} sx={{ py: 1.25, whiteSpace: 'normal', alignItems: 'flex-start' }}> {!n.read && } ); })} { closeNotif(); navigate('/dashboard'); }} sx={{ justifyContent: 'center', color: 'primary.main', fontWeight: 600 }}> View all activity {/* Account dropdown */} setAccount(null)} transformOrigin={{ horizontal: 'right', vertical: 'top' }} anchorOrigin={{ horizontal: 'right', vertical: 'bottom' }} PaperProps={{ sx: { mt: 1, minWidth: 200 } }} > { setAccount(null); navigate('/profile'); }}> View Profile { setAccount(null); navigate('/settings'); }}> Settings { setAccount(null); navigate('/login'); }} sx={{ color: 'error.main' }}> Logout ); }