Frontend Architecture (Next.js)
Relevant source files
The following files were used as context for generating this wiki page:
The Polar frontend is a modern web application built with Next.js and TypeScript, serving as the primary dashboard and public interface for the payment infrastructure platform. It is part of a monorepo managed by Turborepo and pnpm, allowing for shared packages and consistent development workflows across different clients.
Sources: AGENTS.md:21-31, CLAUDE.md:3-8, clients/CLAUDE.md:3-5
The architecture follows a modular structure where features are grouped into specific folders, and UI components are built using a dedicated design system called Orbit. This ensures visual consistency and high developer ergonomics while managing complex data-fetching requirements via TanStack Query.
Sources: AGENTS.md:129-138, clients/CLAUDE.md:21-41
Project Structure and Monorepo Management
The frontend is organized within the clients/ directory of the monorepo. It leverages Turborepo to manage multiple applications and shared packages.
Workspace Breakdown
| Category | Directory | Description |
|---|---|---|
| Applications | apps/web | The main Next.js dashboard and public website. |
| Applications | apps/app | Mobile application built with Expo and React Native. |
| Applications | apps/orbit | Documentation and showcase for the Orbit design system. |
| Packages | packages/ui | Shared React components using Radix UI and Tailwind. |
| Packages | packages/orbit | The core design system containing components and tokens. |
| Packages | packages/client | Generated API client and data-fetching hooks. |
Sources: AGENTS.md:21-31, clients/CLAUDE.md:21-41
Application Flow
The following diagram illustrates the relationship between the web client, the backend API, and third-party integrations:
Sources: DEVELOPMENT.md:12-29
Core UI System: Orbit
Polar has transitioned to a token-based design system called Orbit. The primary primitive for all new UI development is the <Box /> component, which replaces traditional div elements and direct Tailwind utility classes for layout and styling.
The <Box /> Primitive
The Box component is a polymorphic, type-safe primitive that consumes design tokens as props. It defaults to display: flex and compiles typed props into StyleX styles at build time.
- Polymorphism: Supports various HTML elements via the
asprop (e.g.,section,nav,ul). - Tokens: Uses predefined tokens for spacing (xs to 5xl), colors (auto-resolving light/dark), and border radius.
- Pseudo-states: Supports responsive and state-based styling (e.g.,
hover,focus) through object-based props.
Sources: clients/CLAUDE.md:43-69, clients/CLAUDE.md:139-150
Design Tokens
Tokens are centrally managed to ensure consistency across the application.
| Token Type | Values / Scales |
|---|---|
| Spacing | none, xs(4px), s(8px), m(12px), l(16px), xl(24px)... |
| Colors | background-primary, text-secondary, border-warning, etc. |
| Radius | none, s(8), m(12), l(16), xl(32), full |
| Motion | Durations: fast(120ms), base(200ms); Easings: standard, spring |
Sources: clients/CLAUDE.md:104-135
Data Management and Fetching
Data fetching is handled by TanStack Query (React Query), with hooks generated from the backend's OpenAPI specification.
API Integration Workflow
- Generation: The API client is generated from the backend schema using
openapi-typescript-codegen. - Hooks: The
@polar-sh/sdkpackage provides these generated hooks for use in components. - State: Global application state is managed using Zustand.
- Validation: Request and response bodies are validated using Zod.
Sources: AGENTS.md:113-115, AGENTS.md:134-136, clients/apps/web/package.json:115
Sources: AGENTS.md:134-136, clients/CLAUDE.md:267-280
AI and Support Integration
The frontend includes sophisticated AI-driven features, such as the Polar support assistant, which utilizes the Vercel AI SDK and Anthropic models.
AI Route Handlers
Route handlers in apps/web/src/app/(main)/feedback/question/route.ts manage conversational interfaces. These handlers:
- Use Anthropic Claude 3.5 Sonnet as the underlying model.
- Implement tool calling for documentation search and human escalation.
- Integrate with PostHog for tracing and Sentry for error monitoring.
Sources: clients/apps/web/src/app/(main)/feedback/question/route.ts:1-55, clients/apps/web/package.json:44-50
Implementation Example: Support Assistant Tools
const search = tool({
description: 'Search the Polar documentation...',
execute: async ({ query }) => {
const results = await searchMintlify(mintlifyApiKey, query)
return { query, results }
},
})
Sources: clients/apps/web/src/app/(main)/feedback/question/route.ts:153-166
Development and Testing
The project emphasizes local execution and testing before submission.
Development Commands
pnpm dev: Starts the Next.js dev server with Turbopack (usually on port 3000).pnpm generate: Updates the API client from the backend OpenAPI schema.pnpm typecheck: Runs TypeScript validation across the application.
Sources: DEVELOPMENT.md:52-56, clients/CLAUDE.md:7-14, clients/apps/web/package.json:6-15
Testing Frameworks
- Unit/Integration: Handled by Vitest.
- End-to-End (E2E): Handled by Playwright.
- AI Testing: Special procedures exist for E2E flakiness analysis using AI agents to reproduce test steps in a browser environment.
Sources: clients/apps/web/package.json:17-18, clients/apps/web/e2e/healing/prompts/e2e-checkout-healing.md:1-50
Summary
The Polar frontend architecture is a strictly typed, component-driven system built on Next.js. It prioritizes developer experience through the Orbit design system and automated API client generation. By centralizing UI logic in shared packages and utilizing modern state management and AI integration libraries, the platform maintains a scalable and consistent interface for complex financial operations.
Sources: AGENTS.md:129-142, clients/CLAUDE.md:3-40