'use client'; import React from 'react'; import { useParams } from 'next/navigation'; import { useProduct, useProductRecommendations, } from '@/hooks/use-shopify-products'; import ProductCard from './product-card'; interface ProductRecommendationsProps { productId?: string; /** * Seeds recommendations from a specific product. Left empty, it reads the * `[handle]` segment, which is what the `/products/[handle]` template does. */ handle?: string; title?: string; limit?: number; } const ProductRecommendations: React.FC = ({ productId: productIdProp, handle: handleProp, title = 'You May Also Like', limit = 4, }) => { const params = useParams(); const handle = handleProp || (params?.handle as string | undefined); const { product } = useProduct(productIdProp ? null : (handle ?? null)); const resolvedProductId = productIdProp || product?.id || ''; const { recommendations, loading, error } = useProductRecommendations( resolvedProductId || null ); if (!loading && (!recommendations || recommendations.length === 0)) { return null; } return (

{title}

{error ? (

Recommendations could not be loaded

) : (
{loading ? Array.from({ length: limit }).map((_, index) => (
)) : recommendations .slice(0, limit) .map((recommendedProduct) => ( ))}
)}
); }; export default ProductRecommendations;