"use client"; import Link from "next/link"; import { usePathname, useRouter } from "next/navigation"; import { useCallback, useEffect, useLayoutEffect, useRef, useState, type MouseEvent, } from "react"; import { cn } from "@cohaise/ui"; import { useLanguage } from "../contexts/LanguageContext"; import { scrollToSection, setPendingSection, takePendingSection, } from "../lib/gsap"; import { ThemeToggle } from "./ThemeToggle"; const links = [ { id: "services", path: "/services", key: "nav.services" }, { id: "demos", path: "/demos", key: "nav.demos" }, { id: "stack", path: "/stack", key: "nav.stack" }, { id: "about", path: "/about", key: "nav.about" }, { id: "contact", path: "/contact", key: "nav.contact" }, ] as const; type SectionId = (typeof links)[number]["id"]; type NavLink = (typeof links)[number]; const normalize = (path: string): string => path !== "/" ? path.replace(/\/+$/, "") : path; /* Durée pendant laquelle le suivi du scroll est suspendu après un clic : le temps que GSAP atteigne la région sans que l'indicateur ne revienne en arrière. Une fois le défilement fini, l'onglet cliqué reste donc actif (utile pour « À propos », qui partage la région de « Stack »). */ const NAV_LOCK = 1400; interface Indicator { left: number; width: number; } export const Header = () => { const { t, lang, setLang } = useLanguage(); const pathname = usePathname(); const router = useRouter(); const current = normalize(pathname); const navRef = useRef(null); const tabRefs = useRef(new Map()); const lockUntil = useRef(0); const [active, setActive] = useState(null); const [indicator, setIndicator] = useState(null); /** Région qui occupe la bande centrale du viewport. */ const regionAt = useCallback((): SectionId | null => { const marker = window.innerHeight * 0.45; let found: SectionId | null = null; for (const link of links) { const element = document.getElementById(link.id); if (!element) continue; const rect = element.getBoundingClientRect(); if (rect.top <= marker && rect.bottom > marker) found = link.id; } return found; }, []); /** Aligne la pastille glissante sur l'onglet actif. */ const measure = useCallback(() => { const nav = navRef.current; const tab = active ? tabRefs.current.get(active) : undefined; if (!nav || !tab) { setIndicator(null); return; } const navRect = nav.getBoundingClientRect(); const tabRect = tab.getBoundingClientRect(); setIndicator({ left: tabRect.left - navRect.left, width: tabRect.width }); }, [active]); useLayoutEffect(() => { measure(); }, [measure, lang]); useEffect(() => { const nav = navRef.current; if (!nav) return; const observer = new ResizeObserver(measure); observer.observe(nav); // Les libellés changent de largeur quand la police arrive. document.fonts?.ready.then(measure).catch(() => {}); return () => observer.disconnect(); }, [measure]); /* Sur l'accueil, l'onglet actif suit la région visible. Depuis une page SEO autonome, il suit le chemin courant ; au retour, la région mémorisée est rejointe sans passer par un `#`. */ useEffect(() => { if (current !== "/") { const match = links.find((link) => link.path === current); setActive(match?.id ?? null); return; } let frame = 0; const pending = takePendingSection(); const target = pending ? links.find((link) => link.id === pending) : undefined; if (target) { setActive(target.id); lockUntil.current = Date.now() + NAV_LOCK; // Deux frames : laisser Next monter la page d'accueil et poser les régions. frame = requestAnimationFrame(() => { frame = requestAnimationFrame(() => { const duration = scrollToSection(`#${target.id}`); lockUntil.current = Date.now() + Math.max(NAV_LOCK, duration + 400); }); }); } else { setActive(regionAt()); } const onScroll = () => { if (Date.now() < lockUntil.current) return; setActive(regionAt()); }; window.addEventListener("scroll", onScroll, { passive: true }); window.addEventListener("resize", onScroll); return () => { cancelAnimationFrame(frame); window.removeEventListener("scroll", onScroll); window.removeEventListener("resize", onScroll); }; }, [current, regionAt]); const goTo = (event: MouseEvent, link: NavLink) => { if (current !== "/") { event.preventDefault(); setPendingSection(link.id); router.push("/"); return; } event.preventDefault(); lockUntil.current = Date.now() + NAV_LOCK; setActive(link.id); const duration = scrollToSection(`#${link.id}`); lockUntil.current = Date.now() + Math.max(NAV_LOCK, duration + 400); }; const goHome = (event: MouseEvent) => { if (current !== "/") return; event.preventDefault(); lockUntil.current = Date.now() + NAV_LOCK; setActive(null); const duration = scrollToSection("#top"); lockUntil.current = Date.now() + Math.max(NAV_LOCK, duration + 400); }; return (
{t("brand.name")}
); };