# hunga516/vi-frontend architecture

> Architecture diagram of the GitHub repository hunga516/vi-frontend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hunga516/vi-frontend
- Repository: https://github.com/hunga516/vi-frontend

## Overview

##### Project Type & Purpose
- This is a full-stack web application built on Next.js.
- Its purpose appears to be delivering a web interface for user authentication (sign‑in/sign‑up), home page functionalities (with community (“cong-dong”), vi‑short, etc.), and API endpoints, while also providing server‑side functionality using TRPC and database operations.
- The presence of modules (like src/modules/auth and src/modules/home) indicates a separation of concerns with dedicated components for authentication and home page management.

##### Main Components
a. Frontend (Next.js Routes/Pages):
- The app directory contains multiple route segments (e.g., (auth), (home)) that define the user-facing pages.
- The layout components in the various subdirectories (like src/app/(auth)/layout.tsx and src/app/(home)/layout.tsx) provide structural frameworks.
- UI components (in src/components, src/components/ui, and modules directories) serve as the building blocks for the frontend interface.

b. Backend API:
- The API routes (under src/app/api) expose endpoints like /api/trpc and /api/users/webhook, which handle TRPC calls and webhooks.
- The TRPC integration (with files in src/trpc and src/app/api/trpc) connects frontend requests to server-side business logic.

c. Database & Data Layer:
- The project uses Drizzle (see drizzle.config.ts) and has a dedicated database folder (src/db) with schemas and connection files.
- Additional utilities for Redis connections (src/lib/redis.ts) and rate limiting (src/lib/ratelimit.ts) hint at caching and request control mechanisms.

d. Modules and Business Logic:
- The modules directory (e.g., src/modules/auth and src/modules/home) separates feature-specific logic and UI.
- Each module encapsulates its own UI components, server-side logic, or procedures (for instance, in category/server).

##### Architectural Relationships & Interactions
- The frontend communicates with backend API endpoints via fetch/TRPC calls.
- Authentication flows are handled in the (auth) route, which interacts with backend security and database modules.
- The TRPC server acts as a bridge between API requests from the frontend and the business logic inside various modules.
- Database operations, performed through Drizzle in the src/db directory, support dynamic data fetching for pages and API responses.
- Shared UI and utility components facilitate cross-feature consistency (e.g., shared components like navigation, modals, etc.).

##### Architectural Patterns and Design Principles
- The project uses Next.js’s app router to manage routes and layouts, following a component-based architecture.
- Modularization is evident with dedicated directories for modules (auth, home, category) promoting separation of concerns.
- The TRPC integration suggests an API design that embraces type safety and end-to-end contracts.
- Use of middleware (src/middleware.ts) signals that cross-cutting concerns (such as authentication or error handling) are managed centrally.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend Layer"
        FE["Next.js Frontend"]:::frontend
        FEAuth["Authentication Routes"]:::frontend
        FEHome["Home Routes"]:::frontend
    end

    subgraph "API Layer"
        API["TRPC API Endpoint"]:::backend
        WH["Webhook API Endpoint"]:::backend
        TRPC["TRPC Integration"]:::backend
    end

    subgraph "Business Logic & Modules"
        AuthMod["Authentication Module"]:::module
        HomeMod["Home Module"]:::module
    end

    subgraph "Data & Utility Layer"
        DB["Database (Drizzle)"]:::database
        Drizzle["Drizzle Configuration"]:::database
        Util["Utilities & Middleware"]:::utility
    end

    %% Connections between Frontend and API Layer
    FEAuth -->|"calls"| API
    FEHome -->|"calls"| API

    %% API Layer routing to TRPC integration
    API -->|"routes"| TRPC

    %% TRPC integration invoking business logic modules
    TRPC -->|"handles_auth"| AuthMod
    TRPC -->|"handles_home"| HomeMod

    %% Modules interacting with Database layer
    AuthMod -->|"queries"| DB
    HomeMod -->|"queries"| DB

    %% Modules and API layer using Utility & Middleware
    API -->|"middleware"| Util
    AuthMod -->|"uses"| Util
    HomeMod -->|"uses"| Util

    %% Click Events
    click FE "https://github.com/hunga516/vi-frontend/tree/main/src/app"
    click FEAuth "https://github.com/hunga516/vi-frontend/tree/main/src/app/(auth)"
    click FEHome "https://github.com/hunga516/vi-frontend/tree/main/src/app/(home)"
    click AuthMod "https://github.com/hunga516/vi-frontend/tree/main/src/modules/auth"
    click HomeMod "https://github.com/hunga516/vi-frontend/tree/main/src/modules/home"
    click API "https://github.com/hunga516/vi-frontend/tree/main/src/app/api/trpc"
    click TRPC "https://github.com/hunga516/vi-frontend/tree/main/src/trpc"
    click WH "https://github.com/hunga516/vi-frontend/tree/main/src/app/api/users/webhook"
    click DB "https://github.com/hunga516/vi-frontend/tree/main/src/db"
    click Drizzle "https://github.com/hunga516/vi-frontend/blob/main/drizzle.config.ts"
    click Util "https://github.com/hunga516/vi-frontend/tree/main/src/lib"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef module fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef database fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef utility fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
```
