'use client'; import React, { useState, useEffect } from 'react'; import ProductCard from './product-card'; import { getProducts } from '@/hooks/use-shopify-products'; import { Button } from '@/components/ui/button'; import { Loader } from '@/app/components/ui/loader'; interface ProductImage { url: string; altText?: string; } interface ProductPrice { amount: string; currencyCode: string; } interface ProductVariant { id: string; title: string; price: ProductPrice; availableForSale: boolean; } interface Product { id: string; title: string; description?: string; handle: string; images: { edges: Array<{ node: ProductImage; }>; }; priceRange: { minVariantPrice: ProductPrice; }; compareAtPriceRange?: { minVariantPrice: ProductPrice; }; variants: { edges: Array<{ node: ProductVariant; }>; }; } interface ProductsProps { title?: string; limit?: number; showLoadMore?: boolean; } const Products: React.FC = ({ title = 'Our Products', limit = 12, showLoadMore = true, }) => { const [products, setProducts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [loadingMore, setLoadingMore] = useState(false); const [hasMoreProducts, setHasMoreProducts] = useState(true); const fetchProducts = async ( currentProducts: Product[] = [], loadMore = false ) => { try { if (loadMore) { setLoadingMore(true); } else { setLoading(true); setError(null); } const newProducts = await getProducts({ first: limit, sortKey: 'CREATED_AT', reverse: true, }); if (loadMore) { const existingIds = new Set(currentProducts.map((p) => p.id)); const uniqueNewProducts = newProducts.filter( (p) => !existingIds.has(p.id) ); if (uniqueNewProducts.length === 0) { setHasMoreProducts(false); } else { setProducts((prev) => [...prev, ...uniqueNewProducts]); } } else { setProducts(newProducts); setHasMoreProducts(newProducts.length === limit); } } catch (err) { console.error('Error fetching products:', err); setError(err instanceof Error ? err.message : 'Failed to load products'); } finally { setLoading(false); setLoadingMore(false); } }; useEffect(() => { fetchProducts(); }, [limit]); const handleAddToCart = async (product: Product) => { console.log('Adding to cart:', product); }; const handleLoadMore = () => { if (!loadingMore && hasMoreProducts) { fetchProducts(products, true); } }; if (loading) { return (
DISCOVER

{title}

Loading our finest selection...

{Array.from({ length: 8 }).map((_, index) => (
))}
); } if (error || products.length === 0) { return (

{title}

{error ? 'Connection Error' : 'Coming Soon'}

{error || 'Our curated collection is being prepared. Please check back shortly.'}

{error && ( )}
); } return (
CURATED SELECTION

{title}

Beautifully designed objects for everyday life

{products.map((product) => ( ))}
{showLoadMore && hasMoreProducts && (
)}
); }; export default Products;