"use client"; import { useState, useRef, useCallback, useMemo, useEffect } from "react"; import { motion } from "framer-motion"; import Section from "@/components/Section"; import Image from "next/image"; export default function ProductCategory() { const [currentCategory, setCurrentCategory] = useState(0); const categoriesSectionRef = useRef(null); const imageCache = useRef<{ [key: string]: HTMLImageElement }>({}); const categories = [ "Bars", "Barks", "Pralines", "Spreads", "Dragees", "Gelatos", ]; const categoryImages = [ "/categories/c1.svg", "/categories/c2.svg", "/categories/c3.svg", "/categories/c4.svg", "/categories/c5.svg", "/categories/c6.svg", ]; // Preload all images on component mount useEffect(() => { const preloadImages = async () => { const loadPromises = categoryImages.map((src) => { return new Promise((resolve) => { if (imageCache.current[src]) { resolve(); return; } const img = new window.Image(); img.onload = () => { imageCache.current[src] = img; resolve(); }; img.onerror = () => resolve(); // Continue even if one fails img.src = src; }); }); await Promise.all(loadPromises); }; preloadImages(); }, []); // Ultra-fast animation variants (no delays) const fadeInLeft = { initial: { opacity: 0, x: -20 }, animate: { opacity: 1, x: 0 }, transition: { duration: 0.3, ease: [0.23, 1, 0.32, 1] }, }; const fadeInRight = { initial: { opacity: 0, x: 20 }, animate: { opacity: 1, x: 0 }, transition: { duration: 0.3, ease: [0.23, 1, 0.32, 1] }, }; // Instant category change with no debouncing const handleCategoryChange = useCallback((idx: number) => { setCurrentCategory(idx); }, []); // Optimized category buttons const categoryButtons = useMemo(() => { return categories.map((category, idx) => { const isActive = idx === currentCategory; return ( ); }); }, [currentCategory, handleCategoryChange]); // Ultra-optimized image switcher with instant transitions const categoryImageComponent = useMemo( () => (
{/* Render all images but show only active one */} {categoryImages.map((src, idx) => ( {categories[idx]} ))}
), [currentCategory] ); return (
{/* Left Column - Categories List */}
    {categoryButtons}
{/* Right Column - Large Category Image */} {categoryImageComponent}
); }