'use client'; import { Suspense, useState, useEffect, useCallback, useRef } from 'react'; import Link from 'next/link'; import Image from 'next/image'; import { usePathname, useSearchParams, useRouter } from 'next/navigation'; import { Menu, X } from 'lucide-react'; import { AnimatePresence, motion } from 'framer-motion'; import { Button } from '@/components/ui/button'; import { COMPANY_INFO, NAVIGATION, type NavigationItem } from '@/lib/constants'; import { useFocusTrap } from '@/hooks/use-focus-trap'; function HeaderContent() { const [isOpen, setIsOpen] = useState(false); const [isScrolled, setIsScrolled] = useState(false); const pathname = usePathname(); const searchParams = useSearchParams(); const router = useRouter(); const focusTrapRef = useFocusTrap(isOpen); const isScrollingRef = useRef(false); const scrollTimeoutRef = useRef(null); const getActiveSection = useCallback(() => { if (pathname === '/contact') return 'contact'; if (pathname === '/') { const section = searchParams.get('section'); return section || 'home'; } return ''; }, [pathname, searchParams]); const activeSection = getActiveSection(); useEffect(() => { const handleScroll = () => { setIsScrolled(window.scrollY > 20); if (pathname === '/' && !isScrollingRef.current) { const scrollPosition = window.scrollY + 100; const sections = ['home', 'services', 'products', 'cases', 'about', 'news']; for (const sectionId of sections) { const element = document.getElementById(sectionId); if (element) { const offsetTop = element.offsetTop; const offsetHeight = element.offsetHeight; if (scrollPosition >= offsetTop && scrollPosition < offsetTop + offsetHeight) { const currentSection = searchParams.get('section') || 'home'; if (currentSection !== sectionId) { const url = sectionId === 'home' ? '/' : `/?section=${sectionId}`; window.history.replaceState(null, '', url); } break; } } } } }; const handleGlobalKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape' && isOpen) { setIsOpen(false); } }; window.addEventListener('scroll', handleScroll, { passive: true }); window.addEventListener('keydown', handleGlobalKeyDown); return () => { window.removeEventListener('scroll', handleScroll); window.removeEventListener('keydown', handleGlobalKeyDown); if (scrollTimeoutRef.current) { clearTimeout(scrollTimeoutRef.current); } }; }, [pathname, isOpen, searchParams]); const handleKeyDown = useCallback((e: React.KeyboardEvent) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); setIsOpen(!isOpen); } if (e.key === 'Escape' && isOpen) { setIsOpen(false); } }, [isOpen]); const handleNavClick = useCallback((e: React.MouseEvent, item: NavigationItem) => { e.preventDefault(); if (item.id === 'contact') { router.push('/contact'); } else if (item.id === 'home') { if (pathname === '/') { isScrollingRef.current = true; window.scrollTo({ top: 0, behavior: 'smooth' }); window.history.pushState(null, '', '/'); scrollTimeoutRef.current = setTimeout(() => { isScrollingRef.current = false; }, 1000); } else { router.push('/'); } } else { if (pathname === '/') { const scrollToSection = (retryCount = 0) => { const element = document.getElementById(item.id); if (element) { isScrollingRef.current = true; element.scrollIntoView({ behavior: 'smooth', block: 'start' }); window.history.pushState(null, '', `/?section=${item.id}`); scrollTimeoutRef.current = setTimeout(() => { isScrollingRef.current = false; }, 1000); } else if (retryCount < 10) { setTimeout(() => scrollToSection(retryCount + 1), 100); } }; scrollToSection(); } else { router.push(`/?section=${item.id}`); } } setIsOpen(false); }, [pathname, router]); const isActive = useCallback((item: NavigationItem) => { if (item.id === 'contact') { return pathname === '/contact'; } if (pathname === '/') { return activeSection === item.id; } return false; }, [pathname, activeSection]); const navigationItems = NAVIGATION; return ( <>
{COMPANY_INFO.name}
{isOpen && (
setIsOpen(false)} aria-hidden="true" /> )} ); } function HeaderFallback() { return (
); } export function Header() { return ( }> ); }