import { useState, useEffect, useMemo } from "react"; import { motion, AnimatePresence } from "framer-motion"; import { useNavigate, useLocation } from "react-router-dom"; import { ExternalPortal } from "@/external-portal/components/External-Portal-Navigation/PortalHeader"; import { useTranslation } from "react-i18next"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/shared/common/ui/select"; import { toast } from "sonner"; import { ChevronDown, FileText, Search, Info, Menu, X, Moon, Sun, } from "lucide-react"; import { useUser } from "@/shared/context/UserContext"; import { useDarkMode } from "@/shared/hooks/useDarkMode"; import { useTenantConfig, resolveModuleConfig } from "@/layout/components/TenantConfig"; import { UI_LANGUAGE_OPTIONS, resolveUiLanguage, } from "@/shared/i18n/uiLanguages"; import { hasComplaintVerification, isComplaintAuthContext, } from "@/complaints/utils/complaintVerificationStorage"; import { COMPLAINT_RECORDS_PATH } from "@/complaints/utils/complaintRoutes"; import { useExternalPortalSession } from "@/shared/hooks/useExternalPortalSession"; interface NavItem { id: string; label: string; path?: string; requiresCompleteRegistration?: true; children?: NavItem[]; } const languageOptions = UI_LANGUAGE_OPTIONS; const Header = () => { const [activeTab, setActiveTab] = useState("overview"); const [scrolled, setScrolled] = useState(false); const [mobileMenuOpen, setMobileMenuOpen] = useState(false); const [servicesOpen, setServicesOpen] = useState(false); const [navigateTo, setNaviageTo] = useState("/"); const navigate = useNavigate(); const location = useLocation(); const isComplaintContext = isComplaintAuthContext(location.pathname); const { isAuthenticated, showExternalPortalChrome } = useExternalPortalSession(); const { isDarkMode, toggleDarkMode } = useDarkMode(); const { config: tenantConfig } = useTenantConfig(); const moduleConfig = resolveModuleConfig(tenantConfig); const userDetails = useUser(); const { t, i18n } = useTranslation(); const currentLanguage = resolveUiLanguage(i18n.language); const hasCompletedRegistration = userDetails?.hasFinishedRegistration; const changeLanguage = (lng: string) => i18n.changeLanguage(lng); useEffect(() => { if (!localStorage.getItem("i18nextLng")) { i18n.changeLanguage("am"); localStorage.setItem("i18nextLng", "am"); } }, [i18n]); useEffect(() => { const handleScroll = () => { setScrolled(window.scrollY > 10); }; window.addEventListener("scroll", handleScroll); return () => window.removeEventListener("scroll", handleScroll); }, []); const handleSignIn = () => { navigate("/login"); }; useEffect(() => { if (location.pathname.includes("/external-portal/portal-outgoing")) { setActiveTab("records"); return; } if (location.pathname.startsWith("/complaints")) { setActiveTab("complaint"); } }, [location.pathname]); const navItems: NavItem[] = useMemo(() => { if (showExternalPortalChrome) { return []; } if (isAuthenticated) { return [ { id: "overview", label: t("landingPage.overview") }, { id: "workflow", label: t("landingPage.workflow") }, ]; } const complaintServices: NavItem[] = moduleConfig.complaint ? [ { id: "complaint", label: t("landingPage.fileComplaint"), path: "/complaints", }, { id: "follow-complaint", label: t("landingPage.followComplaint"), path: "/follow-complaint", }, ] : []; const items: NavItem[] = [ { id: "overview", label: t("landingPage.overview") }, { id: "features", label: t("landingPage.features") }, ]; if (complaintServices.length > 0) { items.push({ id: "services", label: t("landingPage.services"), children: complaintServices, }); } items.push( { id: "workflow", label: t("landingPage.workflow") }, { id: "howto", label: t("landingPage.manual") }, ); return items; }, [isAuthenticated, moduleConfig.complaint, showExternalPortalChrome, t]); const handleNavClick = (item: NavItem) => { if ( item.requiresCompleteRegistration && !hasCompletedRegistration && !hasComplaintVerification() ) { toast.error(t("registration.registrationRequired")); return; } if (item.path) { navigate(item.path); setMobileMenuOpen(false); } }; // Animation variants const mobileMenuVariants = { hidden: { opacity: 0, height: 0, transition: { duration: 0.3, when: "afterChildren", }, }, visible: { opacity: 1, height: "auto", transition: { duration: 0.3, when: "beforeChildren", staggerChildren: 0.1, }, }, }; const mobileItemVariants = { hidden: { x: -20, opacity: 0 }, visible: { x: 0, opacity: 1, transition: { x: { stiffness: 1000, velocity: -100 }, }, }, }; const servicesVariants = { hidden: { opacity: 0, y: -10, scale: 0.95, transition: { duration: 0.2, }, }, visible: { opacity: 1, y: 0, scale: 1, transition: { duration: 0.2, staggerChildren: 0.05, }, }, }; const serviceItemVariants = { hidden: { opacity: 0, y: -5 }, visible: { opacity: 1, y: 0 }, }; const getServiceIcon = (id: string) => { switch (id) { case "complaint": return ; case "follow-complaint": return ; case "about-us": return ; default: return ; } }; // Get current language display name const getCurrentLanguageDisplay = () => { const currentLang = languageOptions.find( (lang) => lang.value === currentLanguage, ); return currentLang ? currentLang.label : "English"; }; useMemo(() => { if (showExternalPortalChrome) { setNaviageTo(COMPLAINT_RECORDS_PATH); } }, [showExternalPortalChrome]); return ( <> {/* Registration Alert - Fixed for mobile */} ); }; export default Header;