| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import { Check } from 'lucide-react';
- import { cn } from '@/lib/utils';
- import { useStorefront } from '../context';
- import { ProductImage } from '../product-card';
- import { storefrontText } from '../text';
- import { formatPrice } from '../theme';
- import type { FeaturedProductSection } from '../types';
- import { Container, Eyebrow, SfButton } from '../ui';
-
- /**
- * One product, shown large.
- *
- * - Linked to a real product: its real name, price, photo and page are used. The AI's words stay as the description.
- * - Not linked, store has real products: no price is shown, because the AI's price is only an example.
- * - Not linked, store has no products yet: the AI's example name and price are shown, so a new store never looks empty.
- */
- export function FeaturedProduct({
- section,
- }: {
- section: FeaturedProductSection;
- }) {
- const { eyebrow, description, bullets, cta, productSlug } = section.content;
- const { featuredProducts, products, language } = useStorefront();
- const t = storefrontText(language);
- const product = productSlug ? featuredProducts[productSlug] : undefined;
- const showExamplePrice = !product && products.length === 0;
-
- const name = product?.name ?? section.content.name;
- const href = product ? `/products/${product.slug}` : cta.href;
-
- return (
- <section id={section.id} className="scroll-mt-20 bg-(--sf-surface)">
- <Container className="grid items-center gap-10 py-16 md:grid-cols-2 md:py-20">
- <div
- className={cn(
- 'relative',
- section.variant === 'image-right' && 'md:order-last',
- )}
- >
- <ProductImage
- src={product?.image ?? null}
- alt={name}
- seed={2}
- className="aspect-square w-full"
- />
- {product && !product.in_stock && (
- <span className="absolute top-3 left-3 rounded-(--sf-radius) bg-(--sf-fg) px-2.5 py-1 text-xs font-semibold text-(--sf-bg)">
- {t.soldOut}
- </span>
- )}
- </div>
-
- <div>
- {eyebrow && <Eyebrow>{eyebrow}</Eyebrow>}
- <h2 className="sf-heading mt-3 text-3xl leading-tight font-semibold sm:text-4xl">
- {name}
- </h2>
-
- {product && (
- <p
- className="mt-3 text-2xl font-semibold tabular-nums"
- data-test="featured-price"
- >
- {product.price_varies
- ? t.from(formatPrice(product.price))
- : formatPrice(product.price)}
- {!product.price_varies &&
- product.compare_at_price !== null && (
- <s className="ml-3 text-base font-normal text-(--sf-muted)">
- {formatPrice(product.compare_at_price)}
- </s>
- )}
- </p>
- )}
- {showExamplePrice && (
- <p className="mt-3 text-2xl font-semibold tabular-nums">
- {formatPrice(section.content.price)}
- </p>
- )}
-
- <p className="mt-4 max-w-md text-(--sf-muted)">
- {description}
- </p>
-
- {bullets.length > 0 && (
- <ul className="mt-6 space-y-2.5 text-sm">
- {bullets.map((bullet) => (
- <li key={bullet} className="flex gap-2.5">
- <Check
- aria-hidden
- className="mt-0.5 size-4 shrink-0 text-(--sf-primary)"
- />
- {bullet}
- </li>
- ))}
- </ul>
- )}
-
- <SfButton href={href} className="mt-8">
- {product && !product.in_stock
- ? t.viewProduct
- : cta.label}
- </SfButton>
- </div>
- </Container>
- </section>
- );
- }
|