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.

renderer.tsx 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import type { ReactNode } from 'react';
  2. import { cn } from '@/lib/utils';
  3. import { StorefrontProvider } from './context';
  4. import { CartPage } from './pages/cart';
  5. import { CheckoutPage } from './pages/checkout';
  6. import { OrderPage } from './pages/order';
  7. import { ProductPage } from './pages/product';
  8. import { CollectionPage, ShopPage } from './pages/shop';
  9. import { About } from './sections/about';
  10. import { AnnouncementBar } from './sections/announcement-bar';
  11. import { Bento } from './sections/bento';
  12. import { Contact } from './sections/contact';
  13. import { Faq } from './sections/faq';
  14. import { FeaturedProduct } from './sections/featured-product';
  15. import { Footer } from './sections/footer';
  16. import { Header } from './sections/header';
  17. import { Hero } from './sections/hero';
  18. import { Newsletter } from './sections/newsletter';
  19. import { ProductGrid } from './sections/product-grid';
  20. import { ShopBy } from './sections/shop-by';
  21. import { Testimonials } from './sections/testimonials';
  22. import { TrustBar } from './sections/trust-bar';
  23. import { VideoBlock } from './sections/video';
  24. import { styleAttributes, styleOf, themeStyle } from './theme';
  25. import { Reveal } from './reveal';
  26. import { WhatsAppButton } from './whatsapp';
  27. import type {
  28. FontMap,
  29. HeroImageSet,
  30. PlanSection,
  31. ShopInfo,
  32. StoreCart,
  33. StorePlan,
  34. StoreProduct,
  35. StorefrontView,
  36. } from './types';
  37. /**
  38. * Draw one section. To add a new section type: add it to PlanSchema.php and
  39. * types.ts, write its component, and add one line here.
  40. */
  41. function Section({ section }: { section: PlanSection }) {
  42. switch (section.type) {
  43. case 'hero':
  44. return <Hero section={section} />;
  45. case 'trust_bar':
  46. return <TrustBar section={section} />;
  47. case 'shop_by':
  48. return <ShopBy section={section} />;
  49. case 'bento':
  50. return <Bento section={section} />;
  51. case 'video':
  52. return <VideoBlock section={section} />;
  53. case 'product_grid':
  54. return <ProductGrid section={section} />;
  55. case 'featured_product':
  56. return <FeaturedProduct section={section} />;
  57. case 'about':
  58. return <About section={section} />;
  59. case 'testimonials':
  60. return <Testimonials section={section} />;
  61. case 'faq':
  62. return <Faq section={section} />;
  63. case 'newsletter':
  64. return <Newsletter section={section} />;
  65. case 'contact':
  66. return <Contact section={section} />;
  67. default:
  68. // A type this version of the app does not know. Skip it and keep the page working.
  69. return null;
  70. }
  71. }
  72. function PageBody({ plan, view }: { plan: StorePlan; view: StorefrontView }) {
  73. if (view.type === 'shop') {
  74. return <ShopPage />;
  75. }
  76. if (view.type === 'collection') {
  77. return (
  78. <CollectionPage
  79. collection={view.collection}
  80. products={view.collectionProducts}
  81. />
  82. );
  83. }
  84. if (view.type === 'cart') {
  85. return <CartPage />;
  86. }
  87. if (view.type === 'checkout') {
  88. return (
  89. <CheckoutPage
  90. states={view.states}
  91. shippingByState={view.shippingByState}
  92. />
  93. );
  94. }
  95. if (view.type === 'order') {
  96. return <OrderPage order={view.order} />;
  97. }
  98. if (view.type === 'product') {
  99. return (
  100. <ProductPage product={view.product} related={view.related ?? []} />
  101. );
  102. }
  103. const page =
  104. plan.pages.find((candidate) => candidate.id === view.pageId) ??
  105. plan.pages[0];
  106. const alternate = alternateBands(page?.sections ?? []);
  107. return (
  108. <main>
  109. {/* Extra pages get a page heading for screen readers. The home page has its hero headline. */}
  110. {page && page.id !== 'home' && (
  111. <h1 className="sr-only">{page.title}</h1>
  112. )}
  113. {page?.sections.map((section, index) => (
  114. <div
  115. key={section.id}
  116. className={cn(
  117. 'sf-band',
  118. alternate.has(index) && 'sf-band-alt',
  119. )}
  120. >
  121. <Section section={section} />
  122. </div>
  123. ))}
  124. </main>
  125. );
  126. }
  127. /** Sections that bring their own background, so "alternate" bands skip them. */
  128. const OWN_BACKGROUND = ['hero', 'trust_bar', 'testimonials', 'newsletter'];
  129. /**
  130. * For the "alternate" background style: every other section without its own background gets a soft band.
  131. */
  132. function alternateBands(sections: PlanSection[]): Set<number> {
  133. const bands = new Set<number>();
  134. let next = false;
  135. sections.forEach((section, index) => {
  136. if (OWN_BACKGROUND.includes(section.type)) {
  137. next = false;
  138. return;
  139. }
  140. if (next) {
  141. bands.add(index);
  142. }
  143. next = !next;
  144. });
  145. return bands;
  146. }
  147. /**
  148. * Turn a store plan into one storefront page: the shared header, the page itself, the shared footer.
  149. */
  150. export function StorefrontRenderer({
  151. plan,
  152. fonts,
  153. view,
  154. baseUrl,
  155. language,
  156. products,
  157. cart,
  158. storeName,
  159. shop,
  160. featuredProducts,
  161. heroImages,
  162. }: {
  163. plan: StorePlan;
  164. fonts: FontMap;
  165. view: StorefrontView;
  166. baseUrl: string;
  167. language: string;
  168. products: StoreProduct[];
  169. cart: StoreCart;
  170. storeName: string;
  171. shop: ShopInfo;
  172. featuredProducts: Record<string, StoreProduct>;
  173. heroImages: HeroImageSet;
  174. }): ReactNode {
  175. const isHome = view.type === 'page' && view.pageId === 'home';
  176. return (
  177. <StorefrontProvider
  178. value={{
  179. baseUrl,
  180. isHome,
  181. language,
  182. products,
  183. cart,
  184. storeName,
  185. shop,
  186. featuredProducts,
  187. heroImages,
  188. }}
  189. >
  190. <div
  191. className="sf-root flex min-h-screen flex-col"
  192. style={themeStyle(plan.theme, fonts)}
  193. {...styleAttributes(plan.theme)}
  194. >
  195. <Reveal enabled={styleOf(plan.theme).motion === 'gentle'} />
  196. <AnnouncementBar />
  197. <Header section={plan.header} />
  198. <div className="flex-1">
  199. <PageBody plan={plan} view={view} />
  200. </div>
  201. <Footer section={plan.footer} />
  202. {/* Room under the footer so the WhatsApp button never covers its links. */}
  203. {shop.whatsapp && <div className="h-20" aria-hidden="true" />}
  204. <WhatsAppButton />
  205. </div>
  206. </StorefrontProvider>
  207. );
  208. }