"use client"; import { useState, useEffect, useRef } from "react"; import { Globe, Moon, Sun, Sparkles, Menu, X, ChevronDown, ShoppingBag, UserCircle, Lock } from "lucide-react"; import { motion, AnimatePresence } from "framer-motion"; import { useLocale, useTranslations } from "next-intl"; import { Link, usePathname, useRouter } from "@/i18n/routing"; import { useUIStore } from "@/lib/store/uiStore"; const NAV_KEYS = [ { key: "applications", href: "/#applications-deep" }, { key: "globalMap", href: "/#global" }, { key: "ourStory", href: "/#our-story" }, { key: "insideFlux", href: "/news" }, { key: "parts", href: "/parts" }, ]; const LOCALES = [ { code: "en", label: "EN" }, { code: "it", label: "IT" }, { code: "vec", label: "VEC" }, { code: "es", label: "ES" }, { code: "de", label: "DE" } ]; export default function NavBar() { const [scrolled, setScrolled] = useState(false); const [isPastHero, setIsPastHero] = useState(false); const [isDark, setIsDark] = useState(false); const [isTranslating, setIsTranslating] = useState(false); const [isLangMenuOpen, setIsLangMenuOpen] = useState(false); const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const [hoveredIndex, setHoveredIndex] = useState(null); const [logoLightError, setLogoLightError] = useState(false); const [logoDarkError, setLogoDarkError] = useState(false); // 🔥 NUEVO ESTADO PARA SABER SI HAY SESIÓN B2B const [hasSession, setHasSession] = useState(false); const dropdownRef = useRef(null); const t = useTranslations("Navigation"); const locale = useLocale(); const router = useRouter(); const pathname = usePathname(); useEffect(() => { const handleScroll = () => { setScrolled(window.scrollY > 20); setIsPastHero(window.scrollY > window.innerHeight * 0.7); }; window.addEventListener("scroll", handleScroll); handleScroll(); const handleClickOutside = (e: MouseEvent) => { if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) { setIsLangMenuOpen(false); } }; document.addEventListener("mousedown", handleClickOutside); // Cookie check is now event-driven (no setInterval polling). // Triggers: // - Initial mount // - "flux:session-changed" CustomEvent dispatched by AuthModal on login/logout // - visibilitychange (catches logout-in-another-tab) // - storage events (multi-tab logout via shared cookie) const checkSession = () => { const cookies = document.cookie.split("; "); const sessionExists = cookies.some((c) => c.startsWith("flux_b2b_session=")); setHasSession(sessionExists); }; checkSession(); const handleVisibility = () => { if (document.visibilityState === "visible") checkSession(); }; window.addEventListener("flux:session-changed", checkSession); document.addEventListener("visibilitychange", handleVisibility); return () => { window.removeEventListener("scroll", handleScroll); document.removeEventListener("mousedown", handleClickOutside); window.removeEventListener("flux:session-changed", checkSession); document.removeEventListener("visibilitychange", handleVisibility); }; }, []); useEffect(() => { const savedTheme = localStorage.getItem("flux-theme"); if (pathname.includes("/heritage")) { setIsDark(true); document.documentElement.classList.add("dark"); } else { if (savedTheme === "dark") { setIsDark(true); document.documentElement.classList.add("dark"); } else { setIsDark(false); document.documentElement.classList.remove("dark"); } } }, [pathname]); if (pathname.startsWith("/hq-command")) return null; const toggleTheme = () => { const newTheme = isDark ? "light" : "dark"; setIsDark(!isDark); if (newTheme === "dark") { document.documentElement.classList.add("dark"); } else { document.documentElement.classList.remove("dark"); } localStorage.setItem("flux-theme", newTheme); }; const switchLanguage = (newLocale: string) => { setIsLangMenuOpen(false); if (newLocale === locale) return; setIsTranslating(true); setTimeout(() => { router.replace(pathname, { locale: newLocale }); setIsTranslating(false); setIsMobileMenuOpen(false); }, 600); }; const isDarkEsthetic = isDark || isMobileMenuOpen || !isPastHero; // 🔥 ESTILO DINÁMICO PARA EL BOTÓN B2B const b2bButtonClass = scrolled ? (isDarkEsthetic ? "bg-white/10 text-white hover:bg-white/20 border border-white/10" : "bg-black/5 text-[#1D1D1F] hover:bg-black/10 border border-black/5") : "bg-black/60 text-white backdrop-blur-md hover:bg-black/80 border border-white/10 shadow-lg"; return (
{/* MENÚ MÓVIL */} {isMobileMenuOpen && (
    {NAV_KEYS.map((item) => (
  • setIsMobileMenuOpen(false)} className="text-lg font-medium text-white block"> {t(item.key)}
  • ))}
{/* 🔥 BOTÓN B2B MÓVIL */}
Language
{LOCALES.map(l => ( ))}
Theme
)}
); } // Helper para renderizar el contador del carrito function DynamicCartBadge() { const [mounted, setMounted] = useState(false); const cartItems = useUIStore((state) => state.cartItems); useEffect(() => { setMounted(true); }, []); if (!mounted || cartItems.length === 0) return null; const totalItems = cartItems.reduce((acc, item) => acc + item.quantity, 0); return ( {totalItems} ); }