"use client"; import { useEffect, useRef, useState } from "react"; import { createPortal } from "react-dom"; import Link from "next/link"; import Logo from "@/components/Logo"; import SearchDialog from "@/components/SearchDialog"; import LanguageSelector from "@/components/LanguageSelector"; import { LOCALES, getLocalizedHref } from "@/i18n/links"; import { usePathname } from "next/navigation"; import { useTheme } from "next-themes"; import { ArrowRight, BookA, BookOpen, Box, ChevronDown, Cpu, Library, Menu, Moon, Scale, Search, SlidersHorizontal, Sparkles, Sun, X, AlertTriangle, } from "lucide-react"; const MENUS_PT = [ { id: "hardware", label: "Hardware", items: [ { href: "/series", label: "Séries ESP32", desc: "12 chips, pinagem e especificações", icon: Cpu }, { href: "/comparacao", label: "Comparador", desc: "Até quatro séries lado a lado", icon: Scale }, { href: "/catalogo", label: "Catálogo de placas", desc: "DevKits, pinos e onde comprar", icon: Box }, ], }, { id: "tools", label: "Ferramentas", items: [ { href: "/seletor", label: "Seletor inteligente", desc: "O chip certo para o seu projeto", icon: SlidersHorizontal }, { href: "/diagnostico", label: "Diagnóstico", desc: "Brownout, bootloop, WDT e mais", icon: AlertTriangle }, { href: "/componentes", label: "Component Registry", desc: "Drivers e manifests idf_component.yml", icon: Search }, ], }, { id: "library", label: "Biblioteca", items: [ { href: "/frameworks", label: "Frameworks e SDKs", desc: "ESP-IDF, Arduino, Rust e Matter", icon: BookOpen }, { href: "/glossario", label: "Glossário técnico", desc: "Os termos que aparecem nos datasheets", icon: BookA }, { href: "/sobre", label: "Sobre o projeto", desc: "Autoria, método e abertura", icon: Sparkles }, ], }, ]; const MENUS_EN = [ { id: "hardware", label: "Hardware", items: [ { href: "/series", label: "ESP32 Series", desc: "12 chips, pinouts and specifications", icon: Cpu }, { href: "/comparacao", label: "Comparator", desc: "Up to four series side by side", icon: Scale }, { href: "/catalogo", label: "Board Catalog", desc: "DevKits, pinouts and where to buy", icon: Box }, ], }, { id: "tools", label: "Tools", items: [ { href: "/seletor", label: "Smart Selector", desc: "The right chip for your project", icon: SlidersHorizontal }, { href: "/diagnostico", label: "Diagnostics", desc: "Brownout, bootloop, WDT and more", icon: AlertTriangle }, { href: "/componentes", label: "Component Registry", desc: "Drivers and idf_component.yml manifests", icon: Search }, ], }, { id: "library", label: "Library", items: [ { href: "/frameworks", label: "Frameworks & SDKs", desc: "ESP-IDF, Arduino, Rust and Matter", icon: BookOpen }, { href: "/glossario", label: "Technical Glossary", desc: "Terms appearing in datasheets", icon: BookA }, { href: "/sobre", label: "About the Project", desc: "Authorship, method and open source", icon: Sparkles }, ], }, ]; const MENUS_ES = [ { id: "hardware", label: "Hardware", items: [ { href: "/series", label: "Series ESP32", desc: "12 chips, patillaje y especificaciones", icon: Cpu }, { href: "/comparacao", label: "Comparador", desc: "Hasta cuatro series frente a frente", icon: Scale }, { href: "/catalogo", label: "Catálogo de placas", desc: "DevKits, pines y dónde comprar", icon: Box }, ], }, { id: "tools", label: "Herramientas", items: [ { href: "/seletor", label: "Selector inteligente", desc: "El chip adecuado para su proyecto", icon: SlidersHorizontal }, { href: "/diagnostico", label: "Diagnóstico", desc: "Brownout, bootloop, WDT y más", icon: AlertTriangle }, { href: "/componentes", label: "Component Registry", desc: "Drivers y manifiestos idf_component.yml", icon: Search }, ], }, { id: "library", label: "Biblioteca", items: [ { href: "/frameworks", label: "Frameworks y SDKs", desc: "ESP-IDF, Arduino, Rust y Matter", icon: BookOpen }, { href: "/glossario", label: "Glosario técnico", desc: "Términos que aparecen en las hojas de datos", icon: BookA }, { href: "/sobre", label: "Acerca del proyecto", desc: "Autoría, método y código abierto", icon: Sparkles }, ], }, ]; const MENUS_FR = [ { id: "hardware", label: "Matériel", items: [ { href: "/series", label: "Séries ESP32", desc: "12 puces, brochages et spécifications", icon: Cpu }, { href: "/comparacao", label: "Comparateur", desc: "Jusqu'à quatre séries côte à côte", icon: Scale }, { href: "/catalogo", label: "Catalogue de cartes", desc: "DevKits, broches et où acheter", icon: Box }, ], }, { id: "tools", label: "Outils", items: [ { href: "/seletor", label: "Sélecteur intelligent", desc: "La puce idéale pour votre projet", icon: SlidersHorizontal }, { href: "/diagnostico", label: "Diagnostic", desc: "Brownout, bootloop, WDT et plus", icon: AlertTriangle }, { href: "/componentes", label: "Component Registry", desc: "Pilotes et manifestes idf_component.yml", icon: Search }, ], }, { id: "library", label: "Bibliothèque", items: [ { href: "/frameworks", label: "Frameworks et SDKs", desc: "ESP-IDF, Arduino, Rust et Matter", icon: BookOpen }, { href: "/glossario", label: "Glossaire technique", desc: "Les termes des fiches techniques", icon: BookA }, { href: "/sobre", label: "À propos du projet", desc: "Auteur, méthode et open source", icon: Sparkles }, ], }, ]; const MENUS_DE = [ { id: "hardware", label: "Hardware", items: [ { href: "/series", label: "ESP32-Serien", desc: "12 Chips, Pinbelegungen und Spezifikationen", icon: Cpu }, { href: "/comparacao", label: "Vergleich", desc: "Bis zu vier Serien nebeneinander", icon: Scale }, { href: "/catalogo", label: "Board-Katalog", desc: "DevKits, Pins und Bezugsquellen", icon: Box }, ], }, { id: "tools", label: "Werkzeuge", items: [ { href: "/seletor", label: "Intelligente Auswahl", desc: "Der richtige Chip für Ihr Projekt", icon: SlidersHorizontal }, { href: "/diagnostico", label: "Diagnose", desc: "Brownout, Bootloops, WDT und mehr", icon: AlertTriangle }, { href: "/componentes", label: "Component Registry", desc: "Treiber und idf_component.yml-Manifeste", icon: Search }, ], }, { id: "library", label: "Bibliothek", items: [ { href: "/frameworks", label: "Frameworks & SDKs", desc: "ESP-IDF, Arduino, Rust und Matter", icon: BookOpen }, { href: "/glossario", label: "Technisches Glossar", desc: "Begriffe aus den Datenblättern", icon: BookA }, { href: "/sobre", label: "Über das Projekt", desc: "Autorschaft, Methode und Open Source", icon: Sparkles }, ], }, ]; const MOBILE_LINKS_PT = [ { href: "/series", label: "Séries ESP32" }, { href: "/comparacao", label: "Comparador" }, { href: "/seletor", label: "Seletor inteligente" }, { href: "/diagnostico", label: "Diagnóstico" }, { href: "/frameworks", label: "Frameworks e SDKs" }, { href: "/componentes", label: "Component Registry" }, { href: "/glossario", label: "Glossário" }, { href: "/catalogo", label: "Catálogo de placas" }, { href: "/livro", label: "O livro" }, { href: "/sobre", label: "Sobre" }, ]; const MOBILE_LINKS_EN = [ { href: "/series", label: "ESP32 Series" }, { href: "/comparacao", label: "Comparator" }, { href: "/seletor", label: "Smart Selector" }, { href: "/diagnostico", label: "Diagnostics" }, { href: "/frameworks", label: "Frameworks & SDKs" }, { href: "/componentes", label: "Component Registry" }, { href: "/glossario", label: "Glossary" }, { href: "/catalogo", label: "Board Catalog" }, { href: "/sobre", label: "About" }, ]; const MOBILE_LINKS_ES = [ { href: "/series", label: "Series ESP32" }, { href: "/comparacao", label: "Comparador" }, { href: "/seletor", label: "Selector inteligente" }, { href: "/diagnostico", label: "Diagnóstico" }, { href: "/frameworks", label: "Frameworks y SDKs" }, { href: "/componentes", label: "Component Registry" }, { href: "/glossario", label: "Glosario" }, { href: "/catalogo", label: "Catálogo de placas" }, { href: "/sobre", label: "Acerca de" }, ]; const MOBILE_LINKS_FR = [ { href: "/series", label: "Séries ESP32" }, { href: "/comparacao", label: "Comparateur" }, { href: "/seletor", label: "Sélecteur intelligent" }, { href: "/diagnostico", label: "Diagnostic" }, { href: "/frameworks", label: "Frameworks et SDKs" }, { href: "/componentes", label: "Component Registry" }, { href: "/glossario", label: "Glossaire" }, { href: "/catalogo", label: "Catalogue de cartes" }, { href: "/sobre", label: "À propos" }, ]; const MOBILE_LINKS_DE = [ { href: "/series", label: "ESP32-Serien" }, { href: "/comparacao", label: "Vergleich" }, { href: "/seletor", label: "Intelligente Auswahl" }, { href: "/diagnostico", label: "Diagnose" }, { href: "/frameworks", label: "Frameworks & SDKs" }, { href: "/componentes", label: "Component Registry" }, { href: "/glossario", label: "Glossar" }, { href: "/catalogo", label: "Board-Katalog" }, { href: "/sobre", label: "Über uns" }, ]; function isActivePath(pathname, href, currentLocale) { const localized = getLocalizedHref(`/${currentLocale}${href}`, currentLocale); if (href === "/" || localized === `/${currentLocale}/` || localized === `/${currentLocale}`) { return pathname === `/${currentLocale}` || pathname === `/${currentLocale}/`; } return pathname === localized || String(pathname).startsWith(`${localized}/`); } export default function Header() { const pathname = usePathname() || ""; const [mounted, setMounted] = useState(false); const [openMenu, setOpenMenu] = useState(null); const [drawerOpen, setDrawerOpen] = useState(false); const navRef = useRef(null); const closeRef = useRef(null); const menuButtonRef = useRef(null); const currentLocale = LOCALES.find((l) => pathname === `/${l.code}` || pathname.startsWith(`/${l.code}/`))?.code || "pt"; const isPt = currentLocale === "pt"; const isEn = currentLocale === "en"; const isEs = currentLocale === "es"; const isFr = currentLocale === "fr"; const isDe = currentLocale === "de"; const getLinkHref = (href) => getLocalizedHref(`/${currentLocale}${href}`, currentLocale); useEffect(() => setMounted(true), []); useEffect(() => { setOpenMenu(null); setDrawerOpen(false); }, [pathname]); useEffect(() => { function onPointerDown(event) { if (navRef.current && !navRef.current.contains(event.target)) setOpenMenu(null); } function onKeyDown(event) { if (event.key === "Escape") { setOpenMenu(null); setDrawerOpen(false); } } document.addEventListener("mousedown", onPointerDown); document.addEventListener("keydown", onKeyDown); return () => { document.removeEventListener("mousedown", onPointerDown); document.removeEventListener("keydown", onKeyDown); }; }, []); useEffect(() => { if (!drawerOpen) return; const previous = document.body.style.overflow; document.body.style.overflow = "hidden"; closeRef.current?.focus(); return () => { document.body.style.overflow = previous; menuButtonRef.current?.focus(); }; }, [drawerOpen]); const visibleMobileLinks = isPt ? MOBILE_LINKS_PT : isEs ? MOBILE_LINKS_ES : isFr ? MOBILE_LINKS_FR : isDe ? MOBILE_LINKS_DE : MOBILE_LINKS_EN; const MENUS = isPt ? MENUS_PT : isEs ? MENUS_ES : isFr ? MENUS_FR : isDe ? MENUS_DE : MENUS_EN; return (
ESPDocs
{mounted && createPortal( <>
setDrawerOpen(false)} aria-hidden="true" />

{isPt ? "Navegação" : isEs ? "Navegación" : isFr ? "Navigation" : isDe ? "Navigation" : "Navigation"}

{isPt ? "Para onde ir" : isEs ? "A dónde ir" : isFr ? "Où aller" : isDe ? "Wohin möchten Sie" : "Where to go"}

{isPt && (
setDrawerOpen(false)} className="group grid grid-cols-[3.25rem_1fr_auto] items-center gap-3 rounded-2xl bg-[#0b0d10] p-3 text-[#f4f6f8] ring-1 ring-white/10" > {/* eslint-disable-next-line @next/next/no-img-element */} Livro novo Microcontroladores ESP32 Conectividade, eficiência e desempenho
)}
setDrawerOpen(false)} className="btn btn-primary w-full">
, document.body )}
); } function ThemeToggle({ isPt = true, isEs = false, isFr = false, isDe = false }) { const { resolvedTheme, setTheme } = useTheme(); const [mounted, setMounted] = useState(false); useEffect(() => setMounted(true), []); if (!mounted) { return