'use client'; import { useState, useRef, useEffect } from 'react'; import Link from 'next/link'; import { usePathname } from 'next/navigation'; import { useAuthStore } from '@/stores/authStore'; /** * Navigation items for the header */ const navItems = [ { href: '/dashboard', label: 'Dashboard' }, { href: '/beneficiaries', label: 'Loved Ones' }, ]; /** * Header Component * * Responsive header with navigation and profile menu. * - Desktop: Full navigation with profile dropdown * - Mobile: Hamburger menu with slide-in navigation * * @example * ```tsx *
* ``` */ export function Header() { const pathname = usePathname(); const { user, logout } = useAuthStore(); const [isProfileMenuOpen, setIsProfileMenuOpen] = useState(false); const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const profileMenuRef = useRef(null); // Close profile menu when clicking outside useEffect(() => { function handleClickOutside(event: MouseEvent) { if (profileMenuRef.current && !profileMenuRef.current.contains(event.target as Node)) { setIsProfileMenuOpen(false); } } if (isProfileMenuOpen) { document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); } }, [isProfileMenuOpen]); // Close mobile menu when route changes useEffect(() => { setIsMobileMenuOpen(false); }, [pathname]); const handleLogout = async () => { setIsProfileMenuOpen(false); await logout(); }; const userInitials = user?.firstName && user?.lastName ? `${user.firstName[0]}${user.lastName[0]}`.toUpperCase() : user?.email?.[0]?.toUpperCase() || '?'; const userName = user?.firstName && user?.lastName ? `${user.firstName} ${user.lastName}` : user?.email || 'User'; return (
{/* Logo and brand */}
W
WellNuo {/* Desktop navigation */}
{/* Right side: Profile menu */}
{/* Desktop profile dropdown */}
{/* Profile dropdown menu */} {isProfileMenuOpen && (
{userName}
{user?.email && (
{user.email}
)}
setIsProfileMenuOpen(false)} > Profile Settings
)}
{/* Mobile menu button */}
{/* Mobile menu */} {isMobileMenuOpen && (
{/* User info */}
{userInitials}
{userName}
{user?.email && (
{user.email}
)}
{/* Navigation links */} {navItems.map((item) => { const isActive = pathname === item.href || pathname.startsWith(`${item.href}/`); return ( {item.label} ); })} {/* Profile link */} Profile Settings {/* Sign out */}
)}
); }