"use client"; import { useState, useEffect, useRef } from "react"; import Link from "next/link"; import Image from "next/image"; import { usePathname } from "next/navigation"; import { AnimatePresence, motion } from "framer-motion"; import { useTranslations } from "next-intl"; import { useSaleorCheckoutStore } from "@/stores/saleorCheckoutStore"; import { User, ShoppingBag, Menu, X, Globe } from "lucide-react"; import CartDrawer from "@/components/cart/CartDrawer"; import { SUPPORTED_LOCALES, LOCALE_COOKIE, LOCALE_CONFIG, isValidLocale, getPathWithoutLocale, buildLocalePath } from "@/lib/i18n/locales"; import type { Locale } from "@/lib/i18n/locales"; interface HeaderProps { locale?: string; } export default function Header({ locale = "sr" }: HeaderProps) { const t = useTranslations("Header"); const pathname = usePathname(); const dropdownRef = useRef(null); const [mobileMenuOpen, setMobileMenuOpen] = useState(false); const [scrolled, setScrolled] = useState(false); const [langDropdownOpen, setLangDropdownOpen] = useState(false); const { getLineCount, toggleCart, initCheckout } = useSaleorCheckoutStore(); const itemCount = getLineCount(); const currentLocale = isValidLocale(locale) ? LOCALE_CONFIG[locale] : LOCALE_CONFIG.sr; useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { setLangDropdownOpen(false); } }; document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, []); const switchLocale = (newLocale: string) => { if (newLocale === locale) { setLangDropdownOpen(false); return; } if (!isValidLocale(newLocale)) { setLangDropdownOpen(false); return; } document.cookie = `${LOCALE_COOKIE}=${newLocale}; path=/; max-age=31536000`; const pathWithoutLocale = getPathWithoutLocale(pathname); const newPath = buildLocalePath(newLocale as Locale, pathWithoutLocale); window.location.replace(newPath); setLangDropdownOpen(false); }; useEffect(() => { initCheckout(); }, [initCheckout]); useEffect(() => { const handleScroll = () => { setScrolled(window.scrollY > 50); }; window.addEventListener("scroll", handleScroll); return () => window.removeEventListener("scroll", handleScroll); }, []); useEffect(() => { if (mobileMenuOpen) { document.body.style.overflow = "hidden"; } else { document.body.style.overflow = ""; } return () => { document.body.style.overflow = ""; }; }, [mobileMenuOpen]); const navLinks = [ { href: `/${locale}/products`, label: t("products") }, { href: `/${locale}/about`, label: t("about") }, { href: `/${locale}/contact`, label: t("contact") }, ]; return ( <>
ManoonOils
{langDropdownOpen && ( {SUPPORTED_LOCALES.map((loc) => ( ))} )}
{mobileMenuOpen && (
setMobileMenuOpen(false)}> ManoonOils
)}
); }