commerce/components/grid/three-items.tsx
2024-12-27 03:24:35 +01:00

58 lines
1.6 KiB
TypeScript

import { GridTileImage } from 'components/grid/tile';
import { Product } from 'lib/woocomerce/models/product';
import { woocommerce } from 'lib/woocomerce/woocommerce';
import Link from 'next/link';
export function ThreeItemGridItem({
item,
size,
priority
}: {
item: Product;
size: 'full' | 'half';
priority?: boolean;
}) {
return (
<div
className={size === 'full' ? 'md:col-span-4 md:row-span-2' : 'md:col-span-2 md:row-span-1'}
>
<Link
className="relative block aspect-square h-full w-full"
href={`/product/${item.slug}`}
prefetch={true}
>
<GridTileImage
src={item.images?.[0]?.src || ''}
fill
sizes={
size === 'full' ? '(min-width: 768px) 66vw, 100vw' : '(min-width: 768px) 33vw, 100vw'
}
priority={priority}
alt={item.name}
label={{
position: size === 'full' ? 'center' : 'bottom',
title: item.name as string,
amount: item.price,
currencyCode: 'EUR'
}}
/>
</Link>
</div>
);
}
export async function ThreeItemGrid() {
// Collections that start with `hidden-*` are hidden from the search page.
const products: Product[] = await woocommerce.get('products');
const [firstProduct, secondProduct, thirdProduct] = products;
return (
<section className="mx-auto grid max-w-screen-2xl gap-4 px-4 pb-4 md:grid-cols-6 md:grid-rows-2 lg:max-h-[calc(100vh-200px)]">
{products.map((product, index) => (
<ThreeItemGridItem key={product.id} size={index === 0 ? 'full' : 'half'} item={product} />
))}
</section>
);
}