Engineering Design
Frontend Patterns & Storybook

Frontend Patterns & Storybook

Component Architecture — Atomic Design

LayerLocationExamplesData fetching
Atomscomponents/ui/Button, Badge, Input, SpinnerNone — pure props
Moleculescomponents/ui/ProductCard, PriceTag, DownloadButtonNone — pure props
Organismscomponents/features/ProductGrid, CartDrawer, CheckoutFormVia TanStack Query
Pagesapp/**/ProductsPage, AccountPageServer Components

Server vs Client Component Rule

Push "use client" as deep as possible. Only interactive leaf components need it. The ProductCard is a Server Component — only the AddToCartButton inside it is a Client Component. Default to Server, opt into Client.

Storybook

Storybook covers all atoms and molecules (components/ui/). These components are pure — they accept props and render. Storybook can render them with mock data without needing a running Medusa backend.

# Install
npx storybook@latest init  # inside frontend/
# Story convention
components/ui/Button/Button.tsx
components/ui/Button/Button.stories.tsx
components/ui/Button/Button.test.tsx   # unit test alongside

State Management Rules

State typeToolRule
UI state (drawer open, loading)ZustandNever put server data in Zustand
Server data (products, orders)TanStack QueryStale-while-revalidate; invalidate on mutation
CartMedusa JS SDKcart_id persisted in cookie, not localStorage
URL state (filters, pagination)URL search paramsServer-readable, shareable, bookmarkable
Auth tokenHttpOnly cookieNever accessible via JS, never localStorage