Frontend Guide
Next.js 16 (App Router) + React 19 + TypeScript, in frontend/. Styling is
Tailwind with shadcn/ui primitives; server state is TanStack Query.
npm run dev:frontend # :3001
npm run test:frontend
npm run typecheck:frontendStructure
app/
├── (auth)/ login, signup, forgot-password, complete-registration
├── (dashboard)/
│ ├── dashboard/ analytics, performance, ai-insights, assistant, creative,
│ │ optimize, products, campaigns, team, settings, support,
│ │ and one page per connected platform
│ ├── campaigns/ the campaign creation wizard
│ ├── onboarding/ first-run flow
│ ├── marketing-plan/ AI-generated plan
│ └── product-segments/
├── admin/ super-admin console
├── oauth/*/success/ per-platform OAuth landing pages
└── invite/, impersonate/, request-access/, terms/
components/
├── ui/ shadcn primitives
├── campaigns/ wizard steps, payload builders, structure views
├── dashboard/, analytics/, ai/, agents/, onboarding/, admin/
└── editor.tsx TipTap rich-text editor
lib/
├── api-client.ts Axios instance with auth + tenancy interceptors
├── api/ typed service wrappers (ai-service, onboarding-service)
├── firebase/config.ts client SDK
└── constants/roles.tsThe API client
lib/api-client.ts is the only correct way to reach the backend. Its request
interceptor attaches the Firebase ID token and the selected organization id:
config.headers.Authorization = `Bearer ${await user.getIdToken()}`;
config.headers['x-organization-id'] = localStorage.getItem('organizationId');It also sets baseURL from NEXT_PUBLIC_API_URL (appending /api if absent)
and strips a leading /api/ from call sites so both apiClient.get('/campaigns')
and apiClient.get('/api/campaigns') resolve correctly.
// ✅
const { data } = await apiClient.get('/product-segments');
// ❌ no auth header, no org header — 401 or 400
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/product-segments`);Roles in the UI
The backend enforces roles; the UI should reflect them so a VIEWER is not
shown buttons that will 403. The current user's role for the selected org comes
from the auth context (useAuth()), which reads it from GET /api/users/me.
Treat hiding a control as a courtesy, never as the enforcement — that lives on
the server.
lib/constants/roles.tspredates theVIEWERrole and is not imported anywhere. Do not reach for it; it lists onlyOWNER/ADMIN/MEMBER.
Conventions
'use client'only where you need browser APIs, state, or effects.- TanStack Query for anything server-owned; keep query keys org-aware so switching organization refetches rather than showing the previous tenant's data from cache.
- shadcn primitives from
components/uibefore adding a dependency. - Types come from the API response shapes — do not redeclare backend models by hand where a shared shape already exists.
Tests
Vitest unit tests live beside the code in __tests__/ directories — currently
concentrated on the campaign wizard's payload builders and validation, which is
where the logic is. Playwright smoke tests are in e2e/:
npm run test:e2e:frontend # needs the dashboard running on :3001Troubleshooting
| Symptom | Cause |
|---|---|
401 on every call | Not signed in, or the call bypassed apiClient. |
400 ORGANIZATION_ID_REQUIRED | No organization selected — localStorage.organizationId is empty. |
403 ORGANIZATION_ACCESS_DENIED | The stored org id is stale; the user is no longer a member. Re-select the organization. |
403 INSUFFICIENT_ROLE | The action needs a higher role than the user has in this org. |
| Stale data after switching org | A query key that does not include the organization id. |