| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- import type { ReactNode } from 'react';
- import { cn } from '@/lib/utils';
- 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 { AnnouncementBar } from './sections/announcement-bar';
- 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 { TrustBar } from './sections/trust-bar';
- import { styleAttributes, styleOf, themeStyle } from './theme';
- import { Reveal } from './reveal';
- import { WhatsAppButton } from './whatsapp';
- import type {
- FontMap,
- HeroImageSet,
- 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 'trust_bar':
- return <TrustBar 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} related={view.related ?? []} />
- );
- }
-
- const page =
- plan.pages.find((candidate) => candidate.id === view.pageId) ??
- plan.pages[0];
- const alternate = alternateBands(page?.sections ?? []);
-
- 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, index) => (
- <div
- key={section.id}
- className={cn(
- 'sf-band',
- alternate.has(index) && 'sf-band-alt',
- )}
- >
- <Section section={section} />
- </div>
- ))}
- </main>
- );
- }
-
- /** Sections that bring their own background, so "alternate" bands skip them. */
- const OWN_BACKGROUND = ['hero', 'trust_bar', 'testimonials', 'newsletter'];
-
- /**
- * For the "alternate" background style: every other section without its own background gets a soft band.
- */
- function alternateBands(sections: PlanSection[]): Set<number> {
- const bands = new Set<number>();
- let next = false;
-
- sections.forEach((section, index) => {
- if (OWN_BACKGROUND.includes(section.type)) {
- next = false;
-
- return;
- }
-
- if (next) {
- bands.add(index);
- }
-
- next = !next;
- });
-
- return bands;
- }
-
- /**
- * 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,
- featuredProducts,
- heroImages,
- }: {
- plan: StorePlan;
- fonts: FontMap;
- view: StorefrontView;
- baseUrl: string;
- language: string;
- products: StoreProduct[];
- cart: StoreCart;
- storeName: string;
- shop: ShopInfo;
- featuredProducts: Record<string, StoreProduct>;
- heroImages: HeroImageSet;
- }): ReactNode {
- const isHome = view.type === 'page' && view.pageId === 'home';
-
- return (
- <StorefrontProvider
- value={{
- baseUrl,
- isHome,
- language,
- products,
- cart,
- storeName,
- shop,
- featuredProducts,
- heroImages,
- }}
- >
- <div
- className="sf-root flex min-h-screen flex-col"
- style={themeStyle(plan.theme, fonts)}
- {...styleAttributes(plan.theme)}
- >
- <Reveal enabled={styleOf(plan.theme).motion === 'gentle'} />
- <AnnouncementBar />
- <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>
- );
- }
|