import { ChevronLeft, ChevronRight } from 'lucide-react'; import type { ReactNode } from 'react'; import { useRef } from 'react'; import { cn } from '@/lib/utils'; import { useStorefront } from '../context'; import { ProductCard } from '../product-card'; import { storefrontText } from '../text'; import { formatPrice } from '../theme'; import type { ProductGridSection } from '../types'; import { Container, ImagePlaceholder, SectionHeading, SfButton } from '../ui'; /** How many real products the home page shows before "View all". */ const maxShown = 8; /** The large layout shows fewer, bigger products. */ const maxLarge = 4; /** * Shows the merchant's real products when there are any. * Until then it shows the example items the AI wrote, so a new store never looks empty. * * Layouts: grid (4 across), minimal (centred), carousel (one row that scrolls sideways) * and large (2 big cards across, like a magazine). */ export function ProductGrid({ section }: { section: ProductGridSection }) { const { heading, subheading } = section.content; const variant = section.variant; const centered = variant === 'minimal'; const { products, language } = useStorefront(); const t = storefrontText(language); const hasProducts = products.length > 0; const cards = useCards(section, centered); const shown = variant === 'large' ? cards.slice(0, maxLarge) : cards; const viewAll = hasProducts && ( {t.viewAll} ); if (variant === 'carousel') { return ; } return (
{!centered && viewAll}
    {shown.map((card) => (
  • {card.node}
  • ))}
{centered && viewAll && (
{viewAll}
)}
); } /** One row of products that scrolls sideways, with arrows on wide screens. */ function Carousel({ section, cards, viewAll, }: { section: ProductGridSection; cards: { key: string; node: ReactNode }[]; viewAll: ReactNode; }) { const { language } = useStorefront(); const t = storefrontText(language); const row = useRef(null); function scroll(direction: 1 | -1) { const list = row.current; if (list) { list.scrollBy({ left: direction * list.clientWidth * 0.8, behavior: window.matchMedia('(prefers-reduced-motion: reduce)') .matches ? 'auto' : 'smooth', }); } } const arrow = 'hidden size-10 items-center justify-center rounded-full border border-(--sf-border) text-(--sf-fg) transition-colors hover:bg-(--sf-surface) md:inline-flex'; return (
{viewAll}
    {cards.map((card) => (
  • {card.node}
  • ))}
); } /** The real products as cards, or the AI's example items until the merchant adds products. */ function useCards( section: ProductGridSection, centered: boolean, ): { key: string; node: ReactNode }[] { const { products } = useStorefront(); if (products.length > 0) { return products.slice(0, maxShown).map((product, index) => ({ key: `p${product.id}`, node: ( ), })); } return section.content.items.map((item, index) => ({ key: `i${index}${item.name}`, node: ( <>
{item.badge && ( {item.badge} )}

{item.name}

{formatPrice(item.price)}

), })); }