"use client"; import Link from "next/link"; import { usePathname, useRouter } from "next/navigation"; import { FormEvent, useEffect, useMemo, useRef, useState } from "react"; import AccountMenu from "@/components/AccountMenu"; import { products, site } from "@/data/site"; import { localeOptions, showsCatalog } from "@/i18n/config"; import { useLocale, useMessages, useSetLocale } from "@/i18n/locale-context"; function SearchIcon() { return ( ); } function ChevronIcon() { return ( ); } export default function Header() { const pathname = usePathname(); const router = useRouter(); const locale = useLocale(); const t = useMessages(); const setLocale = useSetLocale(); const catalog = showsCatalog(locale); const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); const [searchOpen, setSearchOpen] = useState(false); const [productsOpen, setProductsOpen] = useState(false); const [hash, setHash] = useState(""); const searchRef = useRef(null); const inputRef = useRef(null); useEffect(() => { function onPointer(event: MouseEvent) { if (!searchRef.current?.contains(event.target as Node)) setSearchOpen(false); } function onKey(event: KeyboardEvent) { if (event.key === "Escape") setSearchOpen(false); } document.addEventListener("mousedown", onPointer); document.addEventListener("keydown", onKey); return () => { document.removeEventListener("mousedown", onPointer); document.removeEventListener("keydown", onKey); }; }, []); useEffect(() => { if (searchOpen) inputRef.current?.focus(); }, [searchOpen]); useEffect(() => { function syncHash() { setHash(window.location.hash); } syncHash(); window.addEventListener("hashchange", syncHash); return () => window.removeEventListener("hashchange", syncHash); }, [pathname]); const results = useMemo(() => { const q = query.trim().toLowerCase(); if (q.length < 2) return []; const pages = [ { href: "/", title: t.nav.home }, ...(catalog ? [ { href: "/products", title: t.nav.products }, { href: "/blog", title: t.nav.blog }, ] : []), { href: "/about", title: t.nav.about }, { href: "/contact", title: t.nav.contact }, ]; const catalogItems = catalog ? t.categories.map((product) => ({ href: `/#product-${product.id}`, title: product.title, })) : []; return [...pages, ...catalogItems].filter((item) => item.title.toLowerCase().includes(q)).slice(0, 8); }, [catalog, query, t]); function onSearch(event: FormEvent) { event.preventDefault(); const first = results[0]; if (!first) return; router.push(first.href); setSearchOpen(false); setQuery(""); } return (
setOpen(false)}> {/* eslint-disable-next-line @next/next/no-img-element */} {t.brand.shortName} {t.brand.shortName} {locale !== "en" ? {t.brand.nameEnShort} : null}
{ setQuery(event.target.value); setSearchOpen(true); }} onFocus={() => setSearchOpen(true)} placeholder={t.nav.search} />
{searchOpen && results.length > 0 ? (
    {results.map((item) => (
  • { setSearchOpen(false); setQuery(""); }} > {item.title}
  • ))}
) : null}
); }