AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

renderer.tsx 6.8KB

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