'use client'; import { LanguageSwitcher } from '@/components/i18n/LanguageSwitcher'; import { Button } from '@/components/ui/Button'; import { useTranslation } from '@/lib/hooks/useTranslation'; import { container } from '@/lib/utils'; import { useCart } from 'components/cart/cart-context'; import { Menu, X } from 'lucide-react'; import Image from 'next/image'; import Link from 'next/link'; import { usePathname } from 'next/navigation'; import { useEffect, useState } from 'react'; export default function Navbar() { const { t, locale } = useTranslation(); const [isMenuOpen, setIsMenuOpen] = useState(false); const { cart } = useCart(); const pathname = usePathname(); // Create links with translations and proper prefix const getNavLinks = () => { const links = [ { name: t('navbar.build_box'), path: '/build-box' }, { name: t('navbar.products'), path: '/products' }, { name: t('navbar.about'), path: '/about' } ]; // Add locale prefix for English paths if (locale === 'en') { return links.map(link => ({ ...link, href: `/en${link.path}` })); } // Return paths as is for Croatian return links.map(link => ({ ...link, href: link.path })); }; const navLinks = getNavLinks(); // Close mobile menu on resize to desktop useEffect(() => { const handleResize = () => { if (window.innerWidth >= 768) { setIsMenuOpen(false); } }; window.addEventListener('resize', handleResize); return () => window.removeEventListener('resize', handleResize); }, []); // Prevent scrolling when mobile menu is open useEffect(() => { if (isMenuOpen) { document.body.style.overflow = 'hidden'; } else { document.body.style.overflow = ''; } return () => { document.body.style.overflow = ''; }; }, [isMenuOpen]); // Create home link with proper prefix const homeLink = locale === 'en' ? '/en' : '/'; // Create cart link with proper prefix const cartLink = locale === 'en' ? '/en/cart' : '/cart'; return ( <>
{/* Logo */} Logo {/* Desktop Nav and Cart */}
{navLinks.map((link) => { const isActive = pathname === link.href || (link.href !== homeLink && pathname.startsWith(link.href)); return ( {link.name} ); })} {t('navbar.cart')} Cart {cart?.totalQuantity ? ( {cart.totalQuantity} ) : null}
{/* Mobile menu button and cart */}
Cart {cart?.totalQuantity ? ( {cart.totalQuantity} ) : null}
{/* Mobile menu */} {isMenuOpen && (
{/* Mobile menu header - use same container and height as main navbar */}
setIsMenuOpen(false)} > Logo
{/* Mobile menu links */}
{navLinks.map((link) => { const isActive = pathname === link.href || (link.href !== homeLink && pathname.startsWith(link.href)); return ( setIsMenuOpen(false)} > {link.name} ); })}
setIsMenuOpen(false)} >
)}
); }