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

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