import { useNavigate } from 'react-router-dom'; import { useState } from 'react'; import type { Product, TranslatableString } from '../types'; import { getCategoryImage, formatPrice } from '../types'; import { Heart, ShoppingBag, Layers } from 'lucide-react'; import { useWishlistStore } from '../store/wishlistStore'; import { useCartStore } from '../store/cartStore'; import { useTranslation } from 'react-i18next'; function resolveStr(v: TranslatableString | null | undefined, lang: string): string { if (!v) return '' if (typeof v === 'string') return v return v[lang] ?? v[lang.slice(0, 2)] ?? v['th'] ?? v['en'] ?? Object.values(v)[0] ?? '' } function resolveOptionValue(val: string, options: any[] | null | undefined, lang: string): string { if (!val) return ''; if (!options || !Array.isArray(options)) return val; for (const option of options) { if (!option.values || !Array.isArray(option.values)) continue; for (const v of option.values) { const baseStr = typeof v === 'string' ? v : (v['th'] || v['en'] || Object.values(v)[0] || ''); if (baseStr === val) { if (typeof v === 'string') return v; return v[lang] ?? v[lang.slice(0, 2)] ?? v['th'] ?? v['en'] ?? Object.values(v)[0] ?? baseStr; } } } return val; } interface ProductCardProps { product: Product; enableQuickShop?: boolean; categoryNameOverride?: string; } export default function ProductCard({ product, enableQuickShop = false, categoryNameOverride }: ProductCardProps) { const navigate = useNavigate(); const { addItem, removeItem, isInWishlist } = useWishlistStore(); const { addToCart } = useCartStore(); const { i18n, t } = useTranslation(); const isWished = isInWishlist(product.id); const [selectedVariant, _setSelectedVariant] = useState(product.variants?.[0] || null); const displayName = resolveStr(product.name, i18n.language) || (product.brand ? product.brand.name : ''); const displayCategoryName = categoryNameOverride || resolveStr(product.category.name, i18n.language); const handleWishlistClick = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); if (isWished) { removeItem(product.id); } else { addItem(product); } }; const handleCardClick = () => { navigate(`/products/${product.id}`); }; return (
{displayCategoryName}