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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. {product.price_varies
  60. ? t.from(formatPrice(product.price))
  61. : formatPrice(product.price)}
  62. {!product.price_varies &&
  63. product.compare_at_price !== null && (
  64. <s className="ml-3 text-base font-normal text-(--sf-muted)">
  65. {formatPrice(product.compare_at_price)}
  66. </s>
  67. )}
  68. </p>
  69. )}
  70. {showExamplePrice && (
  71. <p className="mt-3 text-2xl font-semibold tabular-nums">
  72. {formatPrice(section.content.price)}
  73. </p>
  74. )}
  75. <p className="mt-4 max-w-md text-(--sf-muted)">
  76. {description}
  77. </p>
  78. {bullets.length > 0 && (
  79. <ul className="mt-6 space-y-2.5 text-sm">
  80. {bullets.map((bullet) => (
  81. <li key={bullet} className="flex gap-2.5">
  82. <Check
  83. aria-hidden
  84. className="mt-0.5 size-4 shrink-0 text-(--sf-primary)"
  85. />
  86. {bullet}
  87. </li>
  88. ))}
  89. </ul>
  90. )}
  91. <SfButton href={href} className="mt-8">
  92. {product && !product.in_stock
  93. ? t.viewProduct
  94. : cta.label}
  95. </SfButton>
  96. </div>
  97. </Container>
  98. </section>
  99. );
  100. }