"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: "Professional", href: "/user/profiles" }, { label: "Education", href: "/education" }, { label: "About Us", href: "/about" }, { label: "FAQ's", href: "/faq" }, ]; export function CommonHeader() { const { data: session, status } = useSession(); const { profileImage, profileName, avatarLoaded, setAvatarLoaded, notificationCount, messageCount, } = useHeaderData(); const [showProfileMenu, setShowProfileMenu] = useState(false); const [showGuestMenu, setShowGuestMenu] = useState(false); const [showMobileMenu, setShowMobileMenu] = useState(false); const [avatarError, setAvatarError] = 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); } // const showProfessional = userRole === "AGENT" || userRole === "LENDER"; 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; // const showProfessional = userRole === "AGENT" || userRole === "LENDER"; // Use fetched profile image, fallback to session image const userImage = profileImage || session?.user?.image; // Logo destination — always lands on the user dashboard regardless of role. // const dashboardLink = "/user/dashboard"; const dashboardLink = "/home"; return (
{/* Logo */} RE-QuestN {/* Navigation - Desktop only */} {/* */} {/* Right Side Icons */}
{status === "loading" ? (
) : session ? ( <> {/* Message Icon - User role only */} {userRole === "USER" && ( Messages {messageCount > 0 && ( {messageCount > 99 ? "99+" : messageCount} )} )} {/* Notification Bell */} Notifications {notificationCount > 0 && ( {notificationCount > 99 ? "99+" : notificationCount} )} {/* Profile Section */}
{/* Profile Dropdown Menu */} {showProfileMenu && (
{/* Initials base layer */}
{userName?.[0]?.toUpperCase() || "?"}
{userImage && !avatarError && ( { if (el?.complete && el.naturalWidth > 0) setAvatarLoaded(true); }} src={userImage} alt="Profile" className={`absolute inset-0 w-full h-full object-cover transition-opacity duration-300 ${avatarLoaded ? "opacity-100" : "opacity-0"}`} onLoad={() => setAvatarLoaded(true)} onError={() => setAvatarError(true)} /> )}

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

{userEmail}

setShowProfileMenu(false)} > Settings

Account Settings

Profile, Security

setShowProfileMenu(false)} > Notifications

Notification Settings

{userRole === "AGENT" && ( setShowProfileMenu(false)} > Edit Page

Edit Page

)}
)}
) : (
{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 && (
{/* */}
)}
); }