AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

renderer.tsx 6.1KB

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