'use client'; import { useState, useEffect, useRef } from 'react'; import Link from 'next/link'; import Image from 'next/image'; import { useSession } from 'next-auth/react'; import { useHeaderData } from '@/components/providers/header-provider'; const navLinks = [ { label: 'Education', href: '/education' }, { label: 'About Us', href: '/about' }, { label: "FAQ's", href: '/faq' }, ]; export function CommonHeader() { const { data: session } = useSession(); const { profileImage, profileName, avatarLoaded, setAvatarLoaded, notificationCount } = useHeaderData(); const [showProfileMenu, setShowProfileMenu] = useState(false); const [showGuestMenu, setShowGuestMenu] = useState(false); const [showMobileMenu, setShowMobileMenu] = useState(false); const profileMenuRef = useRef(null); const guestMenuRef = useRef(null); const mobileMenuRef = useRef(null); // Close dropdowns when clicking outside useEffect(() => { function handleClickOutside(event: MouseEvent) { if (profileMenuRef.current && !profileMenuRef.current.contains(event.target as Node)) { setShowProfileMenu(false); } if (guestMenuRef.current && !guestMenuRef.current.contains(event.target as Node)) { setShowGuestMenu(false); } if (mobileMenuRef.current && !mobileMenuRef.current.contains(event.target as Node)) { setShowMobileMenu(false); } } if (showProfileMenu || showGuestMenu || showMobileMenu) { document.addEventListener('mousedown', handleClickOutside); } return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [showProfileMenu, showGuestMenu, showMobileMenu]); // Use fetched profile name, fallback to session name const userName = profileName || session?.user?.name; const userEmail = session?.user?.email; const userRole = (session?.user as any)?.role; // Use fetched profile image, fallback to session image const userImage = profileImage || session?.user?.image; // Determine dashboard link based on user role const dashboardLink = userRole === 'AGENT' ? '/agent/dashboard' : '/user/dashboard'; return (
{/* Logo */} RE-QuestN {/* Navigation - Desktop only */} {/* Right Side Icons */}
{session ? ( <> {/* Notification Bell */} Notifications {notificationCount > 0 && ( {notificationCount > 99 ? '99+' : notificationCount} )} {/* Profile Section */}
{/* Profile Dropdown Menu */} {showProfileMenu && (
{(!userImage || !avatarLoaded) && (
)} {userImage && ( { if (el?.complete && el.naturalWidth > 0) setAvatarLoaded(true); }} src={userImage} alt="Profile" className={`w-full h-full object-cover transition-opacity duration-300 ${avatarLoaded ? 'opacity-100' : 'opacity-0'}`} onLoad={() => setAvatarLoaded(true)} /> )}

{userName?.split(' ')[0] || 'User'}

{userEmail}

setShowProfileMenu(false)} > Settings

Account Settings

Profile, Security

setShowProfileMenu(false)} > Notifications

Notification Settings

)}
) : (
{showGuestMenu && (
User

Sign in or create account to continue

setShowGuestMenu(false)} > Settings

Account Settings

Profile, Security

setShowGuestMenu(false)} > Notifications

Notification Settings

setShowGuestMenu(false)} className="flex-1 h-[37px] bg-[#e58625] rounded-[15px] flex items-center justify-center font-fractul text-[14px] text-white hover:bg-[#d47920] transition-colors" > Sign up setShowGuestMenu(false)} className="flex-1 h-[37px] border border-[#e58625] rounded-[15px] flex items-center justify-center font-fractul text-[14px] text-[#e58625] hover:bg-[#e58625]/5 transition-colors" > Login
)}
)} {/* Mobile Menu Button */}
{/* Mobile Navigation Menu */} {showMobileMenu && (
)}
); }