'use client'; import Link from 'next/link'; import Image from 'next/image'; import { useState } from 'react'; import { Check, ShoppingCart } from 'lucide-react'; import BlurHashImage from '@/components/ui/BlurHashImage'; import { ProductResponse } from '@/services/api/catalogService'; import { useCartStore } from '@/store/cartStore'; import { formatCatalogPriceLabel, formatCurrency, getImageUrl } from '@/lib/utils'; interface QuickComparisonTableProps { products: ProductResponse[]; } function priceLabel(product: ProductResponse): string { const label = formatCatalogPriceLabel(product); return label === formatCurrency(0) ? '—' : label; } function productImage(product: ProductResponse): string { return getImageUrl(product.images?.[0]?.image_url || product.variants?.[0]?.images?.[0]?.image_url); } function extraAttributes(product: ProductResponse): { label: string; values: string[] }[] { const buckets: Record }> = {}; (product.variants || []).forEach((variant) => { (variant.attributes || []).forEach((attr) => { const raw = attr.attribute_name || attr.attribute_code || ''; const value = String(attr.attribute_value || '').trim(); if (!raw || !value) return; const key = raw.trim().toLowerCase().replace(/[\s-]+/g, '_'); if (!buckets[key]) { let label = raw.replace(/[_-]+/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase()); if (['qul', 'qual'].includes(key)) label = 'Quality'; else if (['clr'].includes(key)) label = 'Color'; else if (['str'].includes(key)) label = 'Storage'; buckets[key] = { label, values: new Set(), }; } buckets[key].values.add(value); }); }); return Object.values(buckets) .map((item) => ({ label: item.label, values: Array.from(item.values), })) .slice(0, 3); } function ctaKind(product: ProductResponse): 'view' | 'cart' { return (product.variants?.length || 0) > 1 ? 'view' : 'cart'; } export function QuickComparisonTable({ products }: QuickComparisonTableProps) { const addToCart = useCartStore((state) => state.addToCart); const [addedId, setAddedId] = useState(null); if (!products.length) return null; const handleAdd = async (product: ProductResponse) => { const variant = product.variants?.[0]; if (!variant) return; await addToCart(product, variant, 1); setAddedId(product.product_id); setTimeout(() => setAddedId(null), 1800); }; const labelCls = 'sticky left-0 z-10 w-[150px] min-w-[150px] px-4 py-4 text-[13px] font-medium text-gray-500 text-left align-middle bg-[#f3f4f6]'; const cellCls = 'px-5 py-4 min-w-[170px] align-middle text-center'; return (

Quick Comparison

); })} {products.map((prod) => ( ))} {products.map((prod) => { const kind = ctaKind(prod); const justAdded = addedId === prod.product_id; return ( ); })} {products.map((prod) => { const attrs = extraAttributes(prod); return ( ); })}
{products.map((prod) => { const img = productImage(prod); return ( {prod.name}
Price {priceLabel(prod)}
Add to cart {kind === 'view' ? ( View Products ) : ( )}
Additional information {attrs.length === 0 ? ( ) : (
{attrs.map((attr) => (
{attr.label}: {attr.values.map((value, i) => ( {value} {i < attr.values.length - 1 ? , : null} ))}
))}
)}
); }