'use client'; import Link from 'next/link'; import Image from 'next/image'; import { useTranslations } from 'next-intl'; import { usePathname } from 'next/navigation'; import { Button } from './ui'; import { useEffect, useState, useRef } from 'react'; import { cn } from './ui'; import { useAnalytics } from './analytics/useAnalytics'; import { AnalyticsEvents } from './analytics/analytics-events'; export default function Header() { const t = useTranslations('Navigation'); const pathname = usePathname(); const { trackEvent } = useAnalytics(); const [isScrolled, setIsScrolled] = useState(false); const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const mobileMenuRef = useRef(null); // Extract locale from pathname const currentLocale = pathname.split('/')[1] || 'en'; // Check if homepage const isHomePage = pathname === `/${currentLocale}` || pathname === '/'; useEffect(() => { const handleScroll = () => { setIsScrolled(window.scrollY > 50); }; window.addEventListener('scroll', handleScroll); return () => window.removeEventListener('scroll', handleScroll); }, []); // Prevent scroll when mobile menu is open and handle focus trap useEffect(() => { if (isMobileMenuOpen) { document.body.style.overflow = 'hidden'; // Focus trap logic const focusableElements = mobileMenuRef.current?.querySelectorAll( 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])', ); if (focusableElements && focusableElements.length > 0) { const firstElement = focusableElements[0] as HTMLElement; const lastElement = focusableElements[focusableElements.length - 1] as HTMLElement; const handleTabKey = (e: KeyboardEvent) => { if (e.key === 'Tab') { if (e.shiftKey) { if (document.activeElement === firstElement) { e.preventDefault(); lastElement.focus(); } } else { if (document.activeElement === lastElement) { e.preventDefault(); firstElement.focus(); } } } }; const handleEscapeKey = (e: KeyboardEvent) => { if (e.key === 'Escape') { setIsMobileMenuOpen(false); } }; document.addEventListener('keydown', handleTabKey); document.addEventListener('keydown', handleEscapeKey); // Focus the first element when menu opens setTimeout(() => firstElement.focus(), 100); return () => { document.removeEventListener('keydown', handleTabKey); document.removeEventListener('keydown', handleEscapeKey); }; } } else { document.body.style.overflow = 'unset'; } }, [isMobileMenuOpen]); // Function to get path for a different locale with segment translation const getPathForLocale = (newLocale: string) => { const segments = pathname.split('/'); const originLocale = segments[1] || 'en'; // Translation map for localized URL segments const segmentMap: Record> = { de: { produkte: 'products', kontakt: 'contact', impressum: 'legal-notice', datenschutz: 'privacy-policy', agbs: 'terms', niederspannungskabel: 'low-voltage-cables', mittelspannungskabel: 'medium-voltage-cables', hochspannungskabel: 'high-voltage-cables', solarkabel: 'solar-cables', }, en: { products: 'produkte', contact: 'kontakt', 'legal-notice': 'impressum', 'privacy-policy': 'datenschutz', terms: 'agbs', 'low-voltage-cables': 'niederspannungskabel', 'medium-voltage-cables': 'mittelspannungskabel', 'high-voltage-cables': 'hochspannungskabel', 'solar-cables': 'solarkabel', }, }; // Replace the locale segment segments[1] = newLocale; // Translate other segments if they exist in our map const translatedSegments = segments.map((segment, index) => { if (index <= 1) return segment; // Skip empty and locale segments const mapping = segmentMap[originLocale as keyof typeof segmentMap]; return mapping && mapping[segment] ? mapping[segment] : segment; }); return translatedSegments.join('/'); }; const menuItems = [ { label: t('home'), href: '/' }, { label: t('team'), href: '/team' }, { label: t('products'), href: currentLocale === 'de' ? '/produkte' : '/products' }, { label: t('blog'), href: '/blog' }, ]; const headerClass = cn( 'fixed top-0 left-0 right-0 z-50 transition-all duration-500 safe-area-p transform-gpu fill-mode-both', { 'bg-primary/95 backdrop-blur-md md:bg-transparent py-3 md:py-8 shadow-2xl md:shadow-none': isHomePage && !isScrolled && !isMobileMenuOpen, 'bg-primary/90 backdrop-blur-md py-3 md:py-4 shadow-2xl': !isHomePage || isScrolled || isMobileMenuOpen, }, ); const textColorClass = 'text-white'; const logoSrc = '/logo-white.svg'; return ( <>
trackEvent(AnalyticsEvents.BUTTON_CLICK, { target: 'home_logo', location: 'header', }) } > {t('home')}
trackEvent(AnalyticsEvents.TOGGLE_SWITCH, { type: 'language', from: currentLocale, to: 'en', location: 'header', }) } className={`hover:text-accent transition-colors flex items-center gap-2 touch-target ${currentLocale === 'en' ? 'text-accent' : 'opacity-80'}`} > EN
trackEvent(AnalyticsEvents.TOGGLE_SWITCH, { type: 'language', from: currentLocale, to: 'de', location: 'header', }) } className={`hover:text-accent transition-colors flex items-center gap-2 touch-target ${currentLocale === 'de' ? 'text-accent' : 'opacity-80'}`} > DE
{/* Mobile Menu Button */}
{/* Mobile Menu Overlay */} ); }