Frontend Architecture (Webapp)
Relevant source files
The following files were used as context for generating this wiki page:The Traduora frontend is a single-page application (SPA) built with Angular. It serves as the user interface for managing translation projects, terms, and teams. The architecture is modular, leveraging lazy-loading for feature areas and NGXS for predictable state management. It resides within a Yarn workspaces monorepo, alongside the API and documentation website.
This architecture emphasizes separation of concerns, with distinct modules for features like authentication and project management. It uses a centralized state management pattern to handle application data and asynchronous operations, providing a robust and maintainable structure for developers.
Technology Stack
The web application is built on Angular and a set of common libraries for functionality and styling. The core dependencies are managed in webapp/package.json.
| Library | Version | Purpose |
|---|---|---|
@angular/core | ^16.2.12 | Core framework for building the application. |
@ngxs/store | 3.8.2 | State management library, implementing the Redux pattern. |
rxjs | ^7.8.1 | Reactive programming library for managing asynchronous data streams. |
bootstrap | ^4.6.2 | CSS framework for UI components and layout. |
@ng-bootstrap/ng-bootstrap | ^15.1.2 | Angular widgets for Bootstrap. |
jwt-decode | ^3.1.2 | Library for decoding JWTs on the client-side. |
Sources: webapp/package.json:30-47
Project Configuration and Build
The project is configured using angular.json, which defines build targets, development server options, and testing configurations. The application is part of a larger monorepo managed with Yarn workspaces.
Sources: package.json:86-95, webapp/angular.json
Build Configuration
The build architect in angular.json specifies how the application is compiled.
| Configuration | Value | Description |
|---|---|---|
builder | @angular-devkit/build-angular:browser | The tool used to build the application for the browser. |
outputPath | ../dist/public | The directory where the build artifacts are placed. |
index | src/index.html | The main HTML file. |
main | src/main.ts | The main entry point of the application. |
polyfills | src/polyfills.ts | Polyfills for browser compatibility. |
tsConfig | src/tsconfig.app.json | TypeScript configuration for the application. |
styles | ["src/styles.scss"] | Global SASS/CSS styles. |
allowedCommonJsDependencies | ["lodash", "file-saver"] | Specifies CommonJS dependencies that are allowed without a warning. |
For production builds, optimizations like AOT compilation, build optimizer, and output hashing are enabled.
Sources: webapp/angular.json:12-38
Scripts
The webapp/package.json file contains scripts for common development tasks.
| Script | Command | Description |
|---|---|---|
start | yarn ng serve --open | Starts the development server and opens the app. |
build | yarn ng build | Builds the application for development. |
build:prod | yarn ng build --configuration=production | Builds the application for production. |
test | yarn ng test | Runs unit tests using Karma. |
lint | yarn ng lint | Lints the TypeScript and HTML files. |
e2e | yarn ng e2e | Runs end-to-end tests using Protractor. |
Sources: webapp/package.json:18-26
Application Modularity
The application is structured into modules to encapsulate functionality. The root module, AppModule, orchestrates the main components and imports feature modules.
The diagram below shows the high-level module structure.
This diagram illustrates how AppModule imports core and feature modules, with ProjectsModule being loaded on demand.
Sources: webapp/src/app/app.module.ts:31-33, webapp/src/app/app.module.ts:49-53, webapp/src/app/projects/projects.module.ts:43-45
Root Module (AppModule)
AppModule is the application's entry point. It is responsible for:
- Bootstrapping the
AppComponent. - Importing essential modules like
BrowserModule,HttpClientModule, andNgbModule. - Configuring the root router.
- Setting up global state management with NGXS, including
AuthState,ProjectsState, and others. - Registering global HTTP interceptors (
ErrorInterceptor,TokenInterceptor).
Sources: webapp/src/app/app.module.ts:31-43, webapp/src/app/app.module.ts:67-75
Feature Module (ProjectsModule)
ProjectsModule is a lazy-loaded module that contains all functionality related to project management.
- It declares components for listing, creating, and managing projects (e.g.,
ProjectListComponent,ProjectContainerComponent,ProjectSettingsComponent). - It defines child routes for project-specific views like terms, translations, team, and settings.
- It registers feature-specific NGXS states:
ProjectsState,TermsState,TranslationsState, andProjectLabelState.
Sources: webapp/src/app/projects/projects.module.ts:43-45, webapp/src/app/projects/projects.module.ts:46-66, webapp/src/app/projects/projects.module.ts:70-98
Routing
Routing is managed by Angular's RouterModule. The main routes are defined in AppModule, which uses lazy loading for the /projects path to improve initial load time.
This diagram shows the application's routing hierarchy, from the root to the nested routes within the ProjectsModule.
Sources: webapp/src/app/app.module.ts:45-65, webapp/src/app/projects/projects.module.ts:46-66
State Management with NGXS
The application uses NGXS for state management. State is decentralized into feature-specific state classes (e.g., ProjectsState, TermsState).
Projects State (ProjectsState)
The ProjectsState manages data related to projects, including the list of all projects and the currently selected project.
Sources: webapp/src/app/projects/stores/projects.state.ts
State Model
The shape of the projects state is defined by the ProjectsStateModel interface.
export interface ProjectsStateModel {
projects: Project[];
currentProject: Project | undefined;
currentProjectStats: ProjectStats | undefined;
isLoading: boolean;
errorMessage: string | undefined;
}
Sources: webapp/src/app/projects/stores/projects.state.ts:63-70
Actions
Actions are dispatched from components to trigger state changes. Here are some key actions from ProjectsState:
| Action | Description |
|---|---|
GetProjects | Fetches the list of all projects for the user. |
CreateProject | Creates a new project. |
UpdateProject | Updates an existing project's details. |
DeleteProject | Deletes a project. |
SetCurrentProject | Fetches a single project by ID and sets it as the active one. |
RefreshProjectStats | Refreshes the statistics for the current project. |
Sources: webapp/src/app/projects/stores/projects.state.ts:13-44
Action Flow: SetCurrentProject
The following sequence diagram illustrates the flow when a component dispatches the SetCurrentProject action.
This diagram shows how the SetCurrentProject action triggers API calls via ProjectsService to fetch project and plan data, which is then used to update the application state.
Sources: webapp/src/app/projects/stores/projects.state.ts:182-201
HTTP Interceptors
Global HTTP interceptors are registered in AppModule to handle cross-cutting concerns for all outgoing API requests.
This flowchart depicts how an HTTP request is processed by the TokenInterceptor and ErrorInterceptor before being sent to the API and how the response is handled.
TokenInterceptor: Attaches the JWT authentication token to theAuthorizationheader of outgoing requests.ErrorInterceptor: Catches HTTP errors globally. For example, it can handle 401 Unauthorized errors by dispatching aLogoutaction.
Sources: webapp/src/app/app.module.ts:67-75