'use client'; import { useState, useEffect, useCallback, useRef } from 'react'; import { Menu, X } from 'lucide-react'; import { motion, AnimatePresence } from 'framer-motion'; import { Button } from '@/components/ui/button'; import { COMPANY_INFO, NAVIGATION } from '@/lib/constants'; export function Header() { const [isOpen, setIsOpen] = useState(false); const [activeSection, setActiveSection] = useState('home'); const [isScrolled, setIsScrolled] = useState(false); const isScrollingRef = useRef(false); const scrollTimeoutRef = useRef(null); const handleNavClick = useCallback((e: React.MouseEvent, href: string) => { e.preventDefault(); const targetId = href.replace('#', ''); const element = document.getElementById(targetId); if (element) { isScrollingRef.current = true; if (scrollTimeoutRef.current) { clearTimeout(scrollTimeoutRef.current); } const headerOffset = 64; const elementPosition = element.getBoundingClientRect().top; const offsetPosition = elementPosition + window.pageYOffset - headerOffset; window.scrollTo({ top: offsetPosition, behavior: 'smooth' }); setActiveSection(targetId); setIsOpen(false); scrollTimeoutRef.current = setTimeout(() => { isScrollingRef.current = false; }, 1000); } }, []); useEffect(() => { const handleScroll = () => { setIsScrolled(window.scrollY > 20); if (isScrollingRef.current) { return; } const sections = NAVIGATION.map(item => item.href.replace('#', '')); const scrollPosition = window.scrollY + 100; for (let i = sections.length - 1; i >= 0; i--) { const section = document.getElementById(sections[i]); if (section) { const sectionTop = section.offsetTop; const sectionBottom = sectionTop + section.offsetHeight; if (scrollPosition >= sectionTop && scrollPosition < sectionBottom) { setActiveSection(sections[i]); break; } } } }; window.addEventListener('scroll', handleScroll, { passive: true }); handleScroll(); return () => { window.removeEventListener('scroll', handleScroll); if (scrollTimeoutRef.current) { clearTimeout(scrollTimeoutRef.current); } }; }, []); return ( <>
handleNavClick(e, '#home')} className="flex items-center group" > {COMPANY_INFO.name}
{isOpen && (
setIsOpen(false)} /> )} ); }