| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- import type { ReactNode } from 'react';
- import { StorefrontProvider } from './context';
- import { CartPage } from './pages/cart';
- import { CheckoutPage } from './pages/checkout';
- import { OrderPage } from './pages/order';
- import { ProductPage } from './pages/product';
- import { ShopPage } from './pages/shop';
- import { About } from './sections/about';
- import { Contact } from './sections/contact';
- import { Faq } from './sections/faq';
- import { FeaturedProduct } from './sections/featured-product';
- import { Footer } from './sections/footer';
- import { Header } from './sections/header';
- import { Hero } from './sections/hero';
- import { Newsletter } from './sections/newsletter';
- import { ProductGrid } from './sections/product-grid';
- import { Testimonials } from './sections/testimonials';
- import { themeStyle } from './theme';
- import { WhatsAppButton } from './whatsapp';
- import type {
- FontMap,
- PlanSection,
- ShopInfo,
- StoreCart,
- StorePlan,
- StoreProduct,
- StorefrontView,
- } from './types';
-
- /**
- * Draw one section. To add a new section type: add it to PlanSchema.php and
- * types.ts, write its component, and add one line here.
- */
- function Section({ section }: { section: PlanSection }) {
- switch (section.type) {
- case 'hero':
- return <Hero section={section} />;
- case 'product_grid':
- return <ProductGrid section={section} />;
- case 'featured_product':
- return <FeaturedProduct section={section} />;
- case 'about':
- return <About section={section} />;
- case 'testimonials':
- return <Testimonials section={section} />;
- case 'faq':
- return <Faq section={section} />;
- case 'newsletter':
- return <Newsletter section={section} />;
- case 'contact':
- return <Contact section={section} />;
- default:
- // A type this version of the app does not know. Skip it and keep the page working.
- return null;
- }
- }
-
- function PageBody({ plan, view }: { plan: StorePlan; view: StorefrontView }) {
- if (view.type === 'shop') {
- return <ShopPage />;
- }
-
- if (view.type === 'cart') {
- return <CartPage />;
- }
-
- if (view.type === 'checkout') {
- return (
- <CheckoutPage
- states={view.states}
- shippingByState={view.shippingByState}
- />
- );
- }
-
- if (view.type === 'order') {
- return <OrderPage order={view.order} />;
- }
-
- if (view.type === 'product') {
- return <ProductPage product={view.product} />;
- }
-
- const page =
- plan.pages.find((candidate) => candidate.id === view.pageId) ??
- plan.pages[0];
-
- return (
- <main>
- {/* Extra pages get a page heading for screen readers. The home page has its hero headline. */}
- {page && page.id !== 'home' && (
- <h1 className="sr-only">{page.title}</h1>
- )}
- {page?.sections.map((section) => (
- <Section key={section.id} section={section} />
- ))}
- </main>
- );
- }
-
- /**
- * Turn a store plan into one storefront page: the shared header, the page itself, the shared footer.
- */
- export function StorefrontRenderer({
- plan,
- fonts,
- view,
- baseUrl,
- language,
- products,
- cart,
- storeName,
- shop,
- }: {
- plan: StorePlan;
- fonts: FontMap;
- view: StorefrontView;
- baseUrl: string;
- language: string;
- products: StoreProduct[];
- cart: StoreCart;
- storeName: string;
- shop: ShopInfo;
- }): ReactNode {
- const isHome = view.type === 'page' && view.pageId === 'home';
-
- return (
- <StorefrontProvider
- value={{
- baseUrl,
- isHome,
- language,
- products,
- cart,
- storeName,
- shop,
- }}
- >
- <div
- className="sf-root flex min-h-screen flex-col"
- style={themeStyle(plan.theme, fonts)}
- >
- <Header section={plan.header} />
- <div className="flex-1">
- <PageBody plan={plan} view={view} />
- </div>
- <Footer section={plan.footer} />
- {/* Room under the footer so the WhatsApp button never covers its links. */}
- {shop.whatsapp && <div className="h-20" aria-hidden="true" />}
- <WhatsAppButton />
- </div>
- </StorefrontProvider>
- );
- }
|