'use client' import { useState, useEffect } from 'react' import { useMedusaCart } from '@/hooks/useMedusaCart' import type { MedusaProduct } from '@/hooks/useMedusaProducts' import Link from 'next/link' import { ShoppingBag, TrendingUp } from 'lucide-react' const CURRENCY_SYMBOLS: Record = { zar: 'R', usd: '$', eur: '€', gbp: '£', kes: 'KSh', ngn: '₦', ghs: '₵' } const fmt = (amount: number, code?: string) => { const sym = CURRENCY_SYMBOLS[(code ?? '').toLowerCase()] ?? (code?.toUpperCase() ?? '') return `${sym}${(amount / 100).toFixed(2)}` } export default function MostPopularSection() { const [products, setProducts] = useState([]) const [loading, setLoading] = useState(true) const { addToCart } = useMedusaCart() useEffect(() => { fetch('/api/popular-products?limit=8') .then(r => r.ok ? r.json() : Promise.reject(r.status)) .then(d => { setProducts(d.products ?? []); setLoading(false) }) .catch(() => { fetch('/api/medusa/products?limit=8') .then(r => r.json()) .then(d => { setProducts(d.products ?? []); setLoading(false) }) .catch(() => setLoading(false)) }) }, []) if (loading) { return (
{Array.from({ length: 4 }).map((_, i) => (
))}
) } const featured = products.slice(0, 4) if (!featured.length) return null return (

Best Sellers

Most Popular

View all →
{featured.map((product, idx) => { const variant = product.variants?.[0] const price = variant?.prices?.[0] return (
{idx < 2 && ( #{ idx + 1 } Best Seller )}
{product.thumbnail ? ( {product.title ) : (
)}

{product.title}

{price && (

{fmt(price.amount, price.currency_code)}

)}
) })}
) }