'use client' import { useState, useEffect } from 'react' import Image from 'next/image' import Link from 'next/link' import { motion } from 'framer-motion' import { ChevronLeft, ChevronRight } from 'lucide-react' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' interface Product { id: string name: string slug: string price: number description: string | null images: string[] category: { id: string name: string } } interface Category { id: string name: string displayName?: string } interface ProductsSectionProps { products: Product[] categories: Category[] } export default function ProductsSection({ products, categories }: ProductsSectionProps) { const [activeCategory, setActiveCategory] = useState('all') const [currentSlide, setCurrentSlide] = useState(0) const [productsPerSlide, setProductsPerSlide] = useState(4) const [touchStart, setTouchStart] = useState(null) const [touchEnd, setTouchEnd] = useState(null) // Update products per slide based on window size useEffect(() => { const updateProductsPerSlide = () => { if (window.innerWidth < 640) { setProductsPerSlide(1) // mobile } else if (window.innerWidth < 1024) { setProductsPerSlide(2) // tablet } else { setProductsPerSlide(4) // desktop } } updateProductsPerSlide() window.addEventListener('resize', updateProductsPerSlide) return () => window.removeEventListener('resize', updateProductsPerSlide) }, []) // Create category display names mapping const getCategoryDisplayName = (category: Category) => { const displayNames: { [key: string]: string } = { 'basmati': 'Perfectionist', 'premium': 'Quality Seeker', 'organic': 'Taste Champion', 'specialty': 'Smart Shopper', 'kashmina steam': 'Kashmina Steam', 'kashmina': 'Kashmina Steam', 'steam': 'Kashmina Steam', 'rice': 'Premium Rice' } return displayNames[category.name.toLowerCase()] || category.name } // Add "All" category with enhanced categories const allCategories = [ { id: 'all', name: 'All', displayName: 'All' }, ...categories.map(cat => ({ ...cat, displayName: getCategoryDisplayName(cat) })) ] // Filter products based on active category const filteredProducts = activeCategory === 'all' ? products : products.filter(product => product.category.id === activeCategory) // Products per slide (responsive) const getProductsPerSlide = () => { if (typeof window !== 'undefined') { if (window.innerWidth < 640) return 1 // mobile if (window.innerWidth < 1024) return 2 // tablet return 4 // desktop } return 4 } const totalSlides = Math.ceil(filteredProducts.length / productsPerSlide) const nextSlide = () => { setCurrentSlide((prev) => (prev + 1) % totalSlides) } const prevSlide = () => { setCurrentSlide((prev) => (prev - 1 + totalSlides) % totalSlides) } const handleCategoryChange = (categoryId: string) => { setActiveCategory(categoryId) setCurrentSlide(0) // Reset to first slide when category changes } // Touch handlers for mobile swipe const minSwipeDistance = 50 const onTouchStart = (e: React.TouchEvent) => { setTouchEnd(null) // otherwise the swipe is fired even with usual touch events setTouchStart(e.targetTouches[0].clientX) } const onTouchMove = (e: React.TouchEvent) => { setTouchEnd(e.targetTouches[0].clientX) } const onTouchEnd = () => { if (!touchStart || !touchEnd) return const distance = touchStart - touchEnd const isLeftSwipe = distance > minSwipeDistance const isRightSwipe = distance < -minSwipeDistance if (isLeftSwipe && currentSlide < totalSlides - 1) { nextSlide() } else if (isRightSwipe && currentSlide > 0) { prevSlide() } } const getCurrentProducts = () => { const startIndex = currentSlide * productsPerSlide return filteredProducts.slice(startIndex, startIndex + productsPerSlide) } return (
{/* Section Header */}

BASMATI RICE

{/* Category Tabs */}
{allCategories.map((category) => ( ))}
{/* Products Carousel */}
{/* Navigation Arrows */} {totalSlides > 1 && ( <> )} {/* Products Grid */}
{getCurrentProducts().map((product) => ( {/* Product Image */}
{product.images && product.images.length > 0 ? ( {product.name} ) : (
{product.name.charAt(0)}
)}
{/* Product Info */}

{product.name}

))}
{/* Slide Indicators */} {totalSlides > 1 && (
{Array.from({ length: totalSlides }).map((_, index) => (
)}
{/* View All Products Button */}
) }