'use client'; import { AddToBoxButton } from '@/components/products/AddToBoxButton'; import { ColorVariant, getProductColorVariants } from '@/components/products/utils/colorUtils'; import { Product } from 'lib/shopify/types'; import Image from "next/image"; import Link from "next/link"; import { usePathname } from 'next/navigation'; import { useEffect, useState } from 'react'; import { AddToCartButton } from './AddToCartButton'; import { ColorSelector } from './ColorSelector'; interface ProductCardProps { title: string; variant: string; price: number; imageSrc: string; slug: string; product?: Product; colors?: string[]; // Array of color hex codes } export function ProductCard({ title, variant, price: defaultPrice, imageSrc, slug, product, colors }: ProductCardProps) { const pathname = usePathname(); const isBuildBoxPage = pathname.includes('/build-box'); const [colorVariants, setColorVariants] = useState(undefined); const [selectedColorHex, setSelectedColorHex] = useState(null); const [selectedVariant, setSelectedVariant] = useState(undefined); const [currentPrice, setCurrentPrice] = useState(defaultPrice); const [isHovered, setIsHovered] = useState(false); // Initialize color variants and selected color useEffect(() => { if (product) { const variants = getProductColorVariants(product); setColorVariants(variants); if (variants && variants.length > 0) { // Only set these values if we have valid variants if (variants[0]?.color) { setSelectedColorHex(variants[0].color); } setSelectedVariant(variants[0]); if (typeof variants[0]?.price === 'number') { setCurrentPrice(variants[0].price); } } } else if (colors && colors.length > 0) { // If we only have color hex codes without variants const firstColor = colors[0]; if (firstColor) { setSelectedColorHex(firstColor); } } }, [product, colors]); // Handle color selection const handleColorSelect = (color: string) => { setSelectedColorHex(color); if (colorVariants) { const variant = colorVariants.find(v => v.color === color); if (variant) { setSelectedVariant(variant); setCurrentPrice(variant.price); } } }; return (
{/* Image container with fixed height and hover effect */}
setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} > {title} {/* Button - always visible on mobile, hover effect on desktop */} {product && ( <> {/* Mobile button - always visible */}
{isBuildBoxPage ? ( ) : ( )}
{/* Desktop button - visible on hover */} )}

{title}

{currentPrice} €
{variant && (

{variant}

)} {colorVariants && colorVariants.length > 0 ? ( ) : colors && colors.length > 0 ? ( ) : null}
); }