import { useState, useEffect, useMemo, memo, useCallback } from "react"; import logo from "../../assets/logo/SVG-07.svg"; const HomePageTopBar = memo(() => { const [activeTab, setActiveTab] = useState(0); const tabs = useMemo(() => ["تخفیفها", "برترین فروشگاهها", "برای شما"], []); // Scroll to section when tab is clicked const scrollToSection = useCallback((sectionId: string) => { const section = document.getElementById(sectionId); if (section) { section.scrollIntoView({ behavior: "smooth" }); } }, []); // Update active tab based on scroll position with throttling useEffect(() => { let ticking = false; const handleScroll = () => { if (!ticking) { window.requestAnimationFrame(() => { const section1 = document.getElementById("section1"); const section2 = document.getElementById("section2"); const section3 = document.getElementById("section3"); if (section1 && section2 && section3) { const scrollPosition = window.scrollY; if (scrollPosition >= section3.offsetTop - 100) { setActiveTab(2); } else if (scrollPosition >= section2.offsetTop - 100) { setActiveTab(1); } else { setActiveTab(0); } } ticking = false; }); ticking = true; } }; window.addEventListener("scroll", handleScroll, { passive: true }); return () => window.removeEventListener("scroll", handleScroll); }, []); const scrollToTop = useCallback(() => { window.scrollTo({ top: 0, behavior: "smooth" }); }, []); const handleTabClick = useCallback( (index: number) => { setActiveTab(index); scrollToSection(`section${index + 1}`); }, [scrollToSection] ); return (
{tab}