# Frontend Architecture

> Learn how the LearnHouse Next.js 16 frontend is built: React 19, the App Router, Tiptap editor, runtime config and how it talks to the API.

The LearnHouse frontend is a **Next.js 16** application located at `apps/web` in the project. It is the main user interface for learners, instructors, and administrators.

## Technology Stack

- **Next.js 16** with the App Router (standalone output)
- **React 19** with Server Components
- **TypeScript 6**
- **TailwindCSS v4** for styling
- **Bun** as the package manager
- **Turbopack** for development builds (`next dev --turbopack`)
- **SWR** for client-side data fetching

## Key Dependencies

- **@tiptap/core**: Block-based rich text editor (Tiptap 3)
- **@radix-ui**: Accessible, unstyled UI primitives
- **@hocuspocus/provider**: Real-time collaboration client for boards
- **yjs**: CRDT library for conflict-free collaborative editing on boards
- **@codemirror**: Code editor component
- **recharts**: Charting and data visualization
- **formik / yup**: Form handling and validation
- **motion**: Animation library
- **i18next**: Internationalization
- **wavesurfer.js**: Audio/podcast player
- **jspdf / html2canvas**: Certificate generation

Exact versions are in `apps/web/package.json` and `bun.lock`.

## App Router

The frontend uses the Next.js App Router, which enables:

- **Server Components by default**: Pages and layouts render on the server, reducing client-side JavaScript and improving initial load times.
- **Client Components where needed**: Interactive elements opt into client-side rendering with the `"use client"` directive.
- **Streaming and Suspense**: Content is progressively streamed to the browser as it becomes ready.
- **Route Groups and Layouts**: Shared layouts and nested routing keep the codebase organized.

## Authentication

Authentication is handled via **JWT tokens** issued by the FastAPI backend. On login, the backend sets an `LH_access` cookie (8-hour expiry) and an `LH_refresh` cookie (30 days by default, renewed on each refresh). Protected routes check for a valid session before rendering.

## Runtime Configuration

The web app ships as a prebuilt image, so `NEXT_PUBLIC_*` values can't be baked in at build time. When the container starts, `server-wrapper.js` collects every `NEXT_PUBLIC_*` variable (plus `LEARNHOUSE_PLATFORM_URL`) into `runtime-config.json` for the server and `public/runtime-config.js` (`window.__RUNTIME_CONFIG__`) for the browser. Code reads them through `getConfig()` in `services/config/config.ts`, which falls back to `process.env` on the server.

## Communication with the Backend

The frontend communicates with the FastAPI backend over HTTP. API calls are made from both:

- **Server Components**: Data is fetched at request time on the server, which keeps sensitive tokens off the client.
- **Client Components**: Interactive features make API calls directly from the browser using **SWR** for caching and revalidation.

## Directory Structure

```
apps/web/
├── app/           # App Router pages and layouts
├── components/    # Reusable UI components
├── services/      # API client and service functions
├── hooks/         # Shared React hooks
├── lib/           # Utilities and client helpers
├── locales/       # i18next translation resources
├── styles/        # Global styles
├── ee-stub/       # Community stand-ins for Enterprise modules (imported as @ee/*)
└── public/        # Static assets
```

## Development

The frontend runs on **port 3000** during local development. Start the dev server with Turbopack enabled:

```bash
bun dev  # runs next dev --turbopack
```

When using the LearnHouse CLI (`learnhouse dev`), the frontend starts automatically with hot reload enabled.
