58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import {Link} from 'react-router';
|
|
import {Image, Money} from '@shopify/hydrogen';
|
|
import type {
|
|
ProductItemFragment,
|
|
CollectionItemFragment,
|
|
} from 'storefrontapi.generated';
|
|
import {useVariantUrl} from '~/lib/variants';
|
|
|
|
function isValidMoney(
|
|
value: unknown,
|
|
): value is {amount: string; currencyCode: string} {
|
|
if (value == null || typeof value !== 'object') return false;
|
|
const v = value as {amount?: unknown; currencyCode?: unknown};
|
|
return (
|
|
typeof v.amount === 'string' &&
|
|
v.amount.length > 0 &&
|
|
typeof v.currencyCode === 'string' &&
|
|
v.currencyCode.length > 0
|
|
);
|
|
}
|
|
|
|
export function ProductItem({
|
|
product,
|
|
loading,
|
|
}: {
|
|
product: CollectionItemFragment | ProductItemFragment;
|
|
loading?: 'eager' | 'lazy';
|
|
}) {
|
|
const variantUrl = useVariantUrl(product.handle);
|
|
const image = product.featuredImage;
|
|
return (
|
|
<Link
|
|
className="product-item"
|
|
key={product.id}
|
|
prefetch="intent"
|
|
to={variantUrl}
|
|
>
|
|
{image && (
|
|
<Image
|
|
alt={image.altText || product.title}
|
|
aspectRatio="1/1"
|
|
data={image}
|
|
loading={loading}
|
|
sizes="(min-width: 45em) 400px, 100vw"
|
|
/>
|
|
)}
|
|
<h4>{product.title}</h4>
|
|
<small>
|
|
{isValidMoney(product.priceRange?.minVariantPrice) ? (
|
|
<Money data={product.priceRange.minVariantPrice} />
|
|
) : (
|
|
'—'
|
|
)}
|
|
</small>
|
|
</Link>
|
|
);
|
|
}
|