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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import type { ReactNode } from 'react';
  2. import { StorefrontProvider } from './context';
  3. import { CartPage } from './pages/cart';
  4. import { CheckoutPage } from './pages/checkout';
  5. import { OrderPage } from './pages/order';
  6. import { ProductPage } from './pages/product';
  7. import { ShopPage } from './pages/shop';
  8. import { About } from './sections/about';
  9. import { AnnouncementBar } from './sections/announcement-bar';
  10. import { Contact } from './sections/contact';
  11. import { Faq } from './sections/faq';
  12. import { FeaturedProduct } from './sections/featured-product';
  13. import { Footer } from './sections/footer';
  14. import { Header } from './sections/header';
  15. import { Hero } from './sections/hero';
  16. import { Newsletter } from './sections/newsletter';
  17. import { ProductGrid } from './sections/product-grid';
  18. import { Testimonials } from './sections/testimonials';
  19. import { TrustBar } from './sections/trust-bar';
  20. import { themeStyle } from './theme';
  21. import { WhatsAppButton } from './whatsapp';
  22. import type {
  23. FontMap,
  24. HeroImageSet,
  25. PlanSection,
  26. ShopInfo,
  27. StoreCart,
  28. StorePlan,
  29. StoreProduct,
  30. StorefrontView,
  31. } from './types';
  32. /**
  33. * Draw one section. To add a new section type: add it to PlanSchema.php and
  34. * types.ts, write its component, and add one line here.
  35. */
  36. function Section({ section }: { section: PlanSection }) {
  37. switch (section.type) {
  38. case 'hero':
  39. return <Hero section={section} />;
  40. case 'trust_bar':
  41. return <TrustBar section={section} />;
  42. case 'product_grid':
  43. return <ProductGrid section={section} />;
  44. case 'featured_product':
  45. return <FeaturedProduct section={section} />;
  46. case 'about':
  47. return <About section={section} />;
  48. case 'testimonials':
  49. return <Testimonials section={section} />;
  50. case 'faq':
  51. return <Faq section={section} />;
  52. case 'newsletter':
  53. return <Newsletter section={section} />;
  54. case 'contact':
  55. return <Contact section={section} />;
  56. default:
  57. // A type this version of the app does not know. Skip it and keep the page working.
  58. return null;
  59. }
  60. }
  61. function PageBody({ plan, view }: { plan: StorePlan; view: StorefrontView }) {
  62. if (view.type === 'shop') {
  63. return <ShopPage />;
  64. }
  65. if (view.type === 'cart') {
  66. return <CartPage />;
  67. }
  68. if (view.type === 'checkout') {
  69. return (
  70. <CheckoutPage
  71. states={view.states}
  72. shippingByState={view.shippingByState}
  73. />
  74. );
  75. }
  76. if (view.type === 'order') {
  77. return <OrderPage order={view.order} />;
  78. }
  79. if (view.type === 'product') {
  80. return (
  81. <ProductPage product={view.product} related={view.related ?? []} />
  82. );
  83. }
  84. const page =
  85. plan.pages.find((candidate) => candidate.id === view.pageId) ??
  86. plan.pages[0];
  87. return (
  88. <main>
  89. {/* Extra pages get a page heading for screen readers. The home page has its hero headline. */}
  90. {page && page.id !== 'home' && (
  91. <h1 className="sr-only">{page.title}</h1>
  92. )}
  93. {page?.sections.map((section) => (
  94. <Section key={section.id} section={section} />
  95. ))}
  96. </main>
  97. );
  98. }
  99. /**
  100. * Turn a store plan into one storefront page: the shared header, the page itself, the shared footer.
  101. */
  102. export function StorefrontRenderer({
  103. plan,
  104. fonts,
  105. view,
  106. baseUrl,
  107. language,
  108. products,
  109. cart,
  110. storeName,
  111. shop,
  112. featuredProducts,
  113. heroImages,
  114. }: {
  115. plan: StorePlan;
  116. fonts: FontMap;
  117. view: StorefrontView;
  118. baseUrl: string;
  119. language: string;
  120. products: StoreProduct[];
  121. cart: StoreCart;
  122. storeName: string;
  123. shop: ShopInfo;
  124. featuredProducts: Record<string, StoreProduct>;
  125. heroImages: HeroImageSet;
  126. }): ReactNode {
  127. const isHome = view.type === 'page' && view.pageId === 'home';
  128. return (
  129. <StorefrontProvider
  130. value={{
  131. baseUrl,
  132. isHome,
  133. language,
  134. products,
  135. cart,
  136. storeName,
  137. shop,
  138. featuredProducts,
  139. heroImages,
  140. }}
  141. >
  142. <div
  143. className="sf-root flex min-h-screen flex-col"
  144. style={themeStyle(plan.theme, fonts)}
  145. >
  146. <AnnouncementBar />
  147. <Header section={plan.header} />
  148. <div className="flex-1">
  149. <PageBody plan={plan} view={view} />
  150. </div>
  151. <Footer section={plan.footer} />
  152. {/* Room under the footer so the WhatsApp button never covers its links. */}
  153. {shop.whatsapp && <div className="h-20" aria-hidden="true" />}
  154. <WhatsAppButton />
  155. </div>
  156. </StorefrontProvider>
  157. );
  158. }