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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. takingOrders={view.takingOrders}
  93. payWays={view.payWays}
  94. />
  95. );
  96. }
  97. if (view.type === 'order') {
  98. return <OrderPage order={view.order} />;
  99. }
  100. if (view.type === 'product') {
  101. return (
  102. <ProductPage product={view.product} related={view.related ?? []} />
  103. );
  104. }
  105. const page =
  106. plan.pages.find((candidate) => candidate.id === view.pageId) ??
  107. plan.pages[0];
  108. const alternate = alternateBands(page?.sections ?? []);
  109. return (
  110. <main>
  111. {/* Extra pages get a page heading for screen readers. The home page has its hero headline. */}
  112. {page && page.id !== 'home' && (
  113. <h1 className="sr-only">{page.title}</h1>
  114. )}
  115. {page?.sections.map((section, index) => (
  116. <div
  117. key={section.id}
  118. data-section={section.id}
  119. data-section-type={section.type}
  120. className={cn(
  121. 'sf-band',
  122. alternate.has(index) && 'sf-band-alt',
  123. )}
  124. >
  125. <Section section={section} />
  126. </div>
  127. ))}
  128. </main>
  129. );
  130. }
  131. /** Sections that bring their own background, so "alternate" bands skip them. */
  132. const OWN_BACKGROUND = ['hero', 'trust_bar', 'testimonials', 'newsletter'];
  133. /**
  134. * For the "alternate" background style: every other section without its own background gets a soft band.
  135. */
  136. function alternateBands(sections: PlanSection[]): Set<number> {
  137. const bands = new Set<number>();
  138. let next = false;
  139. sections.forEach((section, index) => {
  140. if (OWN_BACKGROUND.includes(section.type)) {
  141. next = false;
  142. return;
  143. }
  144. if (next) {
  145. bands.add(index);
  146. }
  147. next = !next;
  148. });
  149. return bands;
  150. }
  151. /**
  152. * Turn a store plan into one storefront page: the shared header, the page itself, the shared footer.
  153. */
  154. export function StorefrontRenderer({
  155. plan,
  156. fonts,
  157. view,
  158. baseUrl,
  159. language,
  160. products,
  161. cart,
  162. storeName,
  163. shop,
  164. featuredProducts,
  165. heroImages,
  166. }: {
  167. plan: StorePlan;
  168. fonts: FontMap;
  169. view: StorefrontView;
  170. baseUrl: string;
  171. language: string;
  172. products: StoreProduct[];
  173. cart: StoreCart;
  174. storeName: string;
  175. shop: ShopInfo;
  176. featuredProducts: Record<string, StoreProduct>;
  177. heroImages: HeroImageSet;
  178. }): ReactNode {
  179. const isHome = view.type === 'page' && view.pageId === 'home';
  180. return (
  181. <StorefrontProvider
  182. value={{
  183. baseUrl,
  184. isHome,
  185. language,
  186. products,
  187. cart,
  188. storeName,
  189. shop,
  190. featuredProducts,
  191. heroImages,
  192. }}
  193. >
  194. <div
  195. className="sf-root flex min-h-screen flex-col"
  196. style={themeStyle(plan.theme, fonts)}
  197. {...styleAttributes(plan.theme)}
  198. >
  199. <Reveal enabled={styleOf(plan.theme).motion === 'gentle'} />
  200. <AnnouncementBar />
  201. <Header section={plan.header} />
  202. <div className="flex-1">
  203. <PageBody plan={plan} view={view} />
  204. </div>
  205. <Footer section={plan.footer} />
  206. {/* Room under the footer so the WhatsApp button never covers its links. */}
  207. {shop.whatsapp && <div className="h-20" aria-hidden="true" />}
  208. <WhatsAppButton />
  209. </div>
  210. </StorefrontProvider>
  211. );
  212. }