AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

featured-product.tsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { Check } from 'lucide-react';
  2. import { cn } from '@/lib/utils';
  3. import { useStorefront } from '../context';
  4. import { ProductImage } from '../product-card';
  5. import { storefrontText } from '../text';
  6. import { formatPrice } from '../theme';
  7. import type { FeaturedProductSection } from '../types';
  8. import { Container, Eyebrow, SfButton } from '../ui';
  9. /**
  10. * One product, shown large.
  11. *
  12. * - Linked to a real product: its real name, price, photo and page are used. The AI's words stay as the description.
  13. * - Not linked, store has real products: no price is shown, because the AI's price is only an example.
  14. * - Not linked, store has no products yet: the AI's example name and price are shown, so a new store never looks empty.
  15. */
  16. export function FeaturedProduct({
  17. section,
  18. }: {
  19. section: FeaturedProductSection;
  20. }) {
  21. const { eyebrow, description, bullets, cta, productSlug } = section.content;
  22. const { featuredProducts, products, language } = useStorefront();
  23. const t = storefrontText(language);
  24. const product = productSlug ? featuredProducts[productSlug] : undefined;
  25. const showExamplePrice = !product && products.length === 0;
  26. const name = product?.name ?? section.content.name;
  27. const href = product ? `/products/${product.slug}` : cta.href;
  28. return (
  29. <section id={section.id} className="scroll-mt-20 bg-(--sf-surface)">
  30. <Container className="grid items-center gap-10 py-16 md:grid-cols-2 md:py-20">
  31. <div
  32. className={cn(
  33. 'relative',
  34. section.variant === 'image-right' && 'md:order-last',
  35. )}
  36. >
  37. <ProductImage
  38. src={product?.image ?? null}
  39. alt={name}
  40. seed={2}
  41. className="aspect-square w-full"
  42. />
  43. {product && !product.in_stock && (
  44. <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)">
  45. {t.soldOut}
  46. </span>
  47. )}
  48. </div>
  49. <div>
  50. {eyebrow && <Eyebrow>{eyebrow}</Eyebrow>}
  51. <h2 className="sf-heading mt-3 text-3xl leading-tight font-semibold sm:text-4xl">
  52. {name}
  53. </h2>
  54. {product && (
  55. <p
  56. className="mt-3 text-2xl font-semibold tabular-nums"
  57. data-test="featured-price"
  58. >
  59. {formatPrice(product.price)}
  60. {product.compare_at_price !== null && (
  61. <s className="ml-3 text-base font-normal text-(--sf-muted)">
  62. {formatPrice(product.compare_at_price)}
  63. </s>
  64. )}
  65. </p>
  66. )}
  67. {showExamplePrice && (
  68. <p className="mt-3 text-2xl font-semibold tabular-nums">
  69. {formatPrice(section.content.price)}
  70. </p>
  71. )}
  72. <p className="mt-4 max-w-md text-(--sf-muted)">
  73. {description}
  74. </p>
  75. {bullets.length > 0 && (
  76. <ul className="mt-6 space-y-2.5 text-sm">
  77. {bullets.map((bullet) => (
  78. <li key={bullet} className="flex gap-2.5">
  79. <Check
  80. aria-hidden
  81. className="mt-0.5 size-4 shrink-0 text-(--sf-primary)"
  82. />
  83. {bullet}
  84. </li>
  85. ))}
  86. </ul>
  87. )}
  88. <SfButton href={href} className="mt-8">
  89. {product && !product.in_stock
  90. ? t.viewProduct
  91. : cta.label}
  92. </SfButton>
  93. </div>
  94. </Container>
  95. </section>
  96. );
  97. }