"use client"; import React, { useState, useEffect } from 'react'; import { getCollectionProducts } from '@/hooks/use-shopify-collections'; import ProductCard from './ProductCard'; import ProductModal from './ProductModal'; import { Button } from './ui/button'; 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 Collection { id: string; title: string; handle: string; description?: string; image?: ProductImage; } interface CollectionDetailProps { collectionHandle?: string; } const CollectionDetail: React.FC = ({ collectionHandle }) => { const [collection, setCollection] = useState(null); const [products, setProducts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [selectedProduct, setSelectedProduct] = useState(null); const [isModalOpen, setIsModalOpen] = useState(false); useEffect(() => { if (!collectionHandle) return; const fetchCollectionProducts = async () => { try { setLoading(true); setError(null); const data = await getCollectionProducts({ collection: collectionHandle, limit: 20, sortKey: 'COLLECTION_DEFAULT', reverse: false }); setCollection(data.collection); setProducts(data.products); } catch (err) { console.error('Error fetching collection products:', err); setError(err instanceof Error ? err.message : 'Failed to load collection products'); } finally { setLoading(false); } }; fetchCollectionProducts(); }, [collectionHandle]); if (loading) { return (

Collection

{/* Loading Skeleton */}
{Array.from({ length: 8 }).map((_, index) => (
))}
); } if (error) { return (

Collection Not Found

{error || "We couldn't find the collection you're looking for. It may have been removed or the link might be incorrect."}

); } return (
{/* Hero Banner */} {collection?.image && (
{collection.image.altText

{collection.title}

)} {/* Products Section */}
{products.length === 0 ? (

No Products in Collection

This collection doesn't have any products yet.

) : (
{products.map((product) => ( { setSelectedProduct(product); setIsModalOpen(true); }} /> ))}
)}
{/* Product Modal */} {selectedProduct && ( { setIsModalOpen(false); setSelectedProduct(null); }} /> )}
); }; export default CollectionDetail;