Frontend Architecture
Relevant source files
The following files were used as context for generating this wiki page:
- packages/twenty-front/src/index.tsx
- packages/twenty-front/src/modules/app/components/App.tsx
- packages/twenty-front/src/modules/ui/utilities/state/component-state/hooks/useRecoilComponentState.ts
- packages/twenty-front/src/modules/apollo/components/ApolloProvider.tsx
- CLAUDE.md
- packages/twenty-ui/package.json
- package.json
The Twenty frontend is a modern, single-page application built using React 18 and TypeScript. It resides within an Nx monorepo, which allows for a modular architecture with clear separation of concerns. The primary frontend package is twenty-front, which orchestrates the application's structure, state management, and data fetching.
This architecture emphasizes strong typing, component-based UI development, and a centralized data layer for interacting with the backend GraphQL API. Key technologies include Vite for the build tooling, Recoil for global state management, Emotion for styling, and Apollo Client for handling GraphQL operations. The frontend is composed of several key packages, including twenty-front (the main application), twenty-ui (a shared component library), and twenty-shared (common types and utilities).
Sources: CLAUDE.md
Core Technologies & Stack
The frontend stack is built on a foundation of modern and widely-adopted technologies.
| Technology | Purpose | Source |
|---|---|---|
| React 18 | Core UI library for building components. | [CLAUDE.md](https://github.com/twentyhq/twenty/blob/HEAD/CLAUDE.md) |
| TypeScript | Enforces static typing for code quality and maintainability. | [CLAUDE.md](https://github.com/twentyhq/twenty/blob/HEAD/CLAUDE.md) |
| Vite | Frontend build tool and development server. | [CLAUDE.md](https://github.com/twentyhq/twenty/blob/HEAD/CLAUDE.md) |
| Recoil | Global state management. | [CLAUDE.md](https://github.com/twentyhq/twenty/blob/HEAD/CLAUDE.md) |
| Emotion | CSS-in-JS library for styling components. | [CLAUDE.md](https://github.com/twentyhq/twenty/blob/HEAD/CLAUDE.md) |
| Apollo Client | Manages GraphQL data fetching, caching, and state. | [CLAUDE.md](https://github.com/twentyhq/twenty/blob/HEAD/CLAUDE.md) |
| Lingui | Internationalization (i18n) library. | [CLAUDE.md](https://github.com/twentyhq/twenty/blob/HEAD/CLAUDE.md) |
Package Structure
The frontend architecture is distributed across multiple packages within the Nx monorepo, promoting code reuse and separation of concerns.
This diagram illustrates the dependency flow between the main frontend packages.
twenty-front: The main React application package. It contains the application entry point, routing, core components, and modules for features.twenty-ui: A dedicated library for shared, reusable UI components like buttons, inputs, and layout elements. This ensures a consistent look and feel across the application.twenty-shared: A package containing common TypeScript types, utilities, and constants that are used by both the frontend and backend, ensuring consistency.
Sources: CLAUDE.md, package.json:113-128
Application Initialization
The application's entry point is packages/twenty-front/src/index.tsx. This file is responsible for rendering the root React component and wrapping the application with essential context providers.
The following diagram shows the hierarchy of providers that wrap the main <App /> component.
This flow shows the initialization sequence and provider wrapping at the application root.
RecoilRoot: Initializes the Recoil state management library, making global state available to all components.BrowserRouter: Enables client-side routing using React Router.ApolloProvider: Connects the application to the GraphQL backend by providing an Apollo Client instance to the component tree.
Sources: packages/twenty-front/src/index.tsx:13-20
State Management
Twenty employs a hybrid approach to state management.
Global State with Recoil
Recoil is used for managing global application state. It is initialized at the application's root in index.tsx. This allows any component in the tree to access and/or modify shared state atoms and selectors.
Sources: CLAUDE.md, packages/twenty-front/src/index.tsx:15
Component State
For state that is local to a single component or a small subtree of components, standard React hooks like useState and useReducer are used. The project also includes a custom hook, useRecoilComponentState, which leverages Recoil's atomFamily to create and manage state that is scoped to a specific component instance, identified by a unique ID. This provides a mechanism for managing state for dynamically generated UI elements.
Sources: CLAUDE.md, packages/twenty-front/src/modules/ui/utilities/state/component-state/hooks/useRecoilComponentState.ts
Data Fetching with Apollo Client
All communication with the backend is handled via GraphQL, with Apollo Client serving as the client-side library for managing queries, mutations, and caching.
ApolloProvider Setup
The ApolloProvider component, located at packages/twenty-front/src/modules/apollo/components/ApolloProvider.tsx, is responsible for creating and configuring the Apollo Client instance. It sets up the HTTP link to the GraphQL endpoint and initializes the in-memory cache. The client is then made available to the entire application via React's context API.
Sources: packages/twenty-front/src/modules/apollo/components/ApolloProvider.tsx:11-21
Data Flow Sequence
The following diagram illustrates a typical sequence for a component fetching data using Apollo Client.
This sequence shows how Apollo Client first checks its cache before making a network request, optimizing data retrieval.
Sources: CLAUDE.md, packages/twenty-front/src/modules/apollo/components/ApolloProvider.tsx
UI and Component Library (twenty-ui)
The twenty-ui package is a crucial part of the frontend architecture, providing a library of reusable React components. This enforces UI consistency and accelerates development.
Key dependencies for the UI library include:
| Dependency | Description |
|---|---|
@emotion/styled | Used for creating styled components with the CSS-in-JS pattern. |
@tabler/icons-react | Provides a set of icons used throughout the application. |
framer-motion | Powers animations and transitions in the UI. |
react-router-dom | Used for navigation components like links. |
recoil | Some UI components may interact with global state. |
twenty-shared | Depends on shared types and utilities. |
Sources: packages/twenty-ui/package.json:15-31, CLAUDE.md
Development Principles
The codebase adheres to a set of strict development principles to ensure consistency and quality.
- Functional components only: Class components are not used.
- Named exports only: Default exports are disallowed to maintain consistency.
- Types over interfaces:
typeis preferred, except when extending third-party interfaces. - String literals over enums: Standard string literals are used instead of TypeScript enums, except for GraphQL enums.
- No 'any' type allowed: Strict typing is enforced.
- Event handlers preferred over useEffect: For state updates triggered by user interactions.
Sources: CLAUDE.md:73-79