import * as React from "react"; import { Link } from "react-router"; import { Typography } from "@/components/Typography"; type ProductImage = { url: string; altText?: string }; type ProductPrice = { amount: string; currencyCode: string }; export type ProductCardData = { id: string; handle: string; title: string; images?: { edges?: Array<{ node: ProductImage }> }; priceRange?: { minVariantPrice?: ProductPrice }; compareAtPriceRange?: { minVariantPrice?: ProductPrice }; }; function format(price: ProductPrice) { return new Intl.NumberFormat("en-US", { style: "currency", currency: price.currencyCode, }).format(parseFloat(price.amount)); } export function ProductCard({ product, aspect = "portrait", }: { product: ProductCardData; aspect?: "portrait" | "square" | "landscape"; }) { const image = product.images?.edges?.[0]?.node; const price = product.priceRange?.minVariantPrice; const compare = product.compareAtPriceRange?.minVariantPrice; const onSale = price && compare && parseFloat(compare.amount) > parseFloat(price.amount); const aspectClass: Record = { portrait: "aspect-[4/5]", square: "aspect-square", landscape: "aspect-[4/3]", }; return (
{image ? ( {image.altText ) : null}
{product.title} {price ? (
{onSale && compare ? ( {format(compare)} ) : null} {format(price)}
) : null}
); } export default ProductCard;