# nguyendatgh/chat_app-client architecture

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

- Interactive diagram: https://gitdiagram.com/nguyendatgh/chat_app-client
- Repository: https://github.com/nguyendatgh/chat_app-client

## Overview

##### Main Components
- Entry Point & Build Setup: Represent index.html (the static HTML file) that loads the React application through main.tsx. Also indicate that Vite is responsible for module bundling and HMR.
- Views / Pages: Show the pages folder, which includes different route-based components (e.g., the auth pages like login and register, an app page, a 404 page, and loading state). Indicate that the router (src/router) is the mechanism that handles navigation between these pages.
- API Layer: Illustrate the API folder (src/api) that contains modules for authentication (authApi.ts) and user services (userApi.ts). Include the axiosInstance.ts as the common mechanism for making HTTP calls.
- Custom Hooks & Utilities: Include the hooks directory, showing both specific hooks for mutations (like useAddContactMutation, useAuthMutation, etc.) and general-purpose hooks. Similarly, include the utils (error handler, localStorage helpers) as supporting libraries.
- State Management: Highlight state management through the src/context and src/store directories. These manage global states such as theme, authentication, contacts, conversation, modal, and socket connections.
- Styling & Assets: Include the styles (globalStyle, theme, etc.) and assets folders to denote the UI theming and resources.

##### Relationships and Interactions
- The Browser -> Loads index.html which hooks up main.tsx to bootstrap the React application.
- main.tsx initializes the global contexts (theme, auth, contacts, etc.) and sets up the router (from src/router), which controls navigation across pages.
- Custom hooks interlink with both API calls and global state. They encapsulate business logic by triggering mutations, handling API responses, and updating UI based on state.
- Styling: Global styles and theme definitions (in src/styles) are applied across UI components. Show these as a shared resource, enhancing the overall consistency.

##### Architectural Patterns
- Modular/Component-Based Architecture: Distinct folders for components ensure separation of UI concerns.
- Unidirectional Data Flow: Highlight context providers and state management, indicating that data flows from these central stores to consuming components.
- Routing: A single-page application mechanism where the router directs which page component is rendered.
- API Abstraction: The API folder that abstracts HTTP calls so that React components remain decoupled from low-level networking details.

##### Essential Elements
- Clear Labels: Each node should be clearly labeled (e.g., “Main Entry (main.tsx)”, “Router (src/router)”, “Auth Pages (login & register)”, “UI Components (Buttons, Modal, Sidebar, etc.)”, “API (authApi, userApi)”, “State Management (authContext, contactsContext, etc.)”).

- At the very top, the Client (Browser) loads index.html.
- The React initialization in main.tsx sits in the center, which initializes contexts and the Router.
- The Router splits into multiple paths (pages like auth, app, 404, etc.).
- Each Page block is composed of several UI components, which further interact with hooks and API modules.
- The API and global state sections are depicted as separate interacting components accessible by multiple pages/components.
- This not only gives an overview of the static structure but also conveys the dynamic flows and dependencies.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Point & Build Setup
    Browser("Browser"):::entry
    Index("index.html"):::entry
    Main("main.tsx"):::entry

    Browser -->|"loads"| Index
    Index -->|"bootstraps"| Main
    click Index "https://github.com/nguyendatgh/chat_app-client/blob/master/index.html"
    click Main "https://github.com/nguyendatgh/chat_app-client/blob/master/src/main.tsx"

    %% Global Initialization from Main
    Main -->|"initializes"| Router
    Main -->|"sets up"| Contexts
    Main -->|"sets up"| Store

    Router("Router (src/router)"):::pages
    click Router "https://github.com/nguyendatgh/chat_app-client/blob/master/src/router/index.tsx"

    Contexts("Contexts (Global State)"):::state
    click Contexts "https://github.com/nguyendatgh/chat_app-client/tree/master/src/context"

    Store("Store (State Management)"):::state
    click Store "https://github.com/nguyendatgh/chat_app-client/tree/master/src/store"

    %% Pages / Views Layer
    Pages("Pages"):::pages
    click Pages "https://github.com/nguyendatgh/chat_app-client/tree/master/src/pages"
    Router -->|"navigates to"| Pages
    Contexts -->|"provides state"| Pages
    Store -->|"provides state"| Pages

    %% UI Components Layer
    UIComponents("UI Components"):::components
    click UIComponents "https://github.com/nguyendatgh/chat_app-client/tree/master/src/components"
    Pages -->|"renders"| UIComponents

    ThemeToggle("ThemeToggle"):::components
    click ThemeToggle "https://github.com/nguyendatgh/chat_app-client/blob/master/src/components/ThemeToggle/themeToggle.tsx"

    Buttons("Buttons"):::components
    click Buttons "https://github.com/nguyendatgh/chat_app-client/blob/master/src/components/buttons/button.tsx"

    Contacts("Contacts"):::components
    click Contacts "https://github.com/nguyendatgh/chat_app-client/tree/master/src/components/contacts"

    Dropdown("Dropdown"):::components
    click Dropdown "https://github.com/nguyendatgh/chat_app-client/blob/master/src/components/dropdown/index.tsx"

    Forms("Forms (AuthForm)"):::components
    click Forms "https://github.com/nguyendatgh/chat_app-client/blob/master/src/components/forms/authForm.tsx"

    MessageInput("MessageInput"):::components
    click MessageInput "https://github.com/nguyendatgh/chat_app-client/blob/master/src/components/inputs/messageInput.tsx"

    TextInput("TextInput"):::components
    click TextInput "https://github.com/nguyendatgh/chat_app-client/blob/master/src/components/inputs/textInput.tsx"

    Links("Links"):::components
    click Links "https://github.com/nguyendatgh/chat_app-client/blob/master/src/components/links/link.tsx"

    Messages("Messages"):::components
    click Messages "https://github.com/nguyendatgh/chat_app-client/tree/master/src/components/messages"

    Modal("Modal"):::components
    click Modal "https://github.com/nguyendatgh/chat_app-client/tree/master/src/components/modal"

    Sidebar("Sidebar"):::components
    click Sidebar "https://github.com/nguyendatgh/chat_app-client/tree/master/src/components/sidebar"

    %% Connect UI Components to their subcomponents
    UIComponents -->|"includes"| ThemeToggle
    UIComponents -->|"includes"| Buttons
    UIComponents -->|"includes"| Contacts
    UIComponents -->|"includes"| Dropdown
    UIComponents -->|"includes"| Forms
    UIComponents -->|"includes"| MessageInput
    UIComponents -->|"includes"| TextInput
    UIComponents -->|"includes"| Links
    UIComponents -->|"includes"| Messages
    UIComponents -->|"includes"| Modal
    UIComponents -->|"includes"| Sidebar

    %% Custom Hooks & Utilities Layer
    Hooks("Custom Hooks"):::hooks
    click Hooks "https://github.com/nguyendatgh/chat_app-client/tree/master/src/hooks"
    UIComponents -->|"uses"| Hooks

    AddContact("useAddContactMutation"):::hooks
    click AddContact "https://github.com/nguyendatgh/chat_app-client/blob/master/src/hooks/mutations/useAddContactMutation.tsx"
    AuthMutation("useAuthMutation"):::hooks
    click AuthMutation "https://github.com/nguyendatgh/chat_app-client/blob/master/src/hooks/mutations/useAuthMutation.tsx"
    LoginMutation("useLoginMutation"):::hooks
    click LoginMutation "https://github.com/nguyendatgh/chat_app-client/blob/master/src/hooks/mutations/useLoginMutation.tsx"
    RegisterMutation("useRegisterMutation"):::hooks
    click RegisterMutation "https://github.com/nguyendatgh/chat_app-client/blob/master/src/hooks/mutations/useRegisterMutation.tsx"
    OnScreen("useOnScreen"):::hooks
    click OnScreen "https://github.com/nguyendatgh/chat_app-client/blob/master/src/hooks/useOnScreen.tsx"
    QueryParams("useQueryParams"):::hooks
    click QueryParams "https://github.com/nguyendatgh/chat_app-client/blob/master/src/hooks/useQueryParams.tsx"

    Hooks -->|"mutation hook"| AddContact
    Hooks -->|"mutation hook"| AuthMutation
    Hooks -->|"mutation hook"| LoginMutation
    Hooks -->|"mutation hook"| RegisterMutation
    Hooks -->|"other hook"| OnScreen
    Hooks -->|"other hook"| QueryParams

    Utilities("Utilities"):::hooks
    click Utilities "https://github.com/nguyendatgh/chat_app-client/tree/master/src/utils"
    Hooks -->|"utilizes"| Utilities

    %% API Layer
    API("API Layer"):::api
    click API "https://github.com/nguyendatgh/chat_app-client/tree/master/src/api"
    Hooks -->|"calls"| API

    AuthAPI("Auth API"):::api
    click AuthAPI "https://github.com/nguyendatgh/chat_app-client/blob/master/src/api/authApi.ts"
    UserAPI("User API"):::api
    click UserAPI "https://github.com/nguyendatgh/chat_app-client/blob/master/src/api/userApi.ts"
    Axios("Axios Instance"):::api
    click Axios "https://github.com/nguyendatgh/chat_app-client/blob/master/src/api/axiosInstance.ts"

    API -->|"provides"| AuthAPI
    API -->|"provides"| UserAPI
    API -->|"utilizes"| Axios

    %% Styling & Assets
    Styles("Styles"):::styling
    click Styles "https://github.com/nguyendatgh/chat_app-client/tree/master/src/styles"
    Assets("Assets"):::styling
    click Assets "https://github.com/nguyendatgh/chat_app-client/tree/master/src/assets"
    UIComponents -->|"styled by"| Styles

    %% Linting & Configuration
    Config("Configuration & Tooling"):::config
    ESLint("ESLint"):::config
    click ESLint "https://github.com/nguyendatgh/chat_app-client/blob/master/eslint.config.js"
    TSConfig("TypeScript Configs"):::config
    click TSConfig "https://github.com/nguyendatgh/chat_app-client/blob/master/tsconfig.json"
    ViteConfig("Vite Config"):::config
    click ViteConfig "https://github.com/nguyendatgh/chat_app-client/blob/master/vite.config.ts"
    EnvTypes("Environment Types"):::config
    click EnvTypes "https://github.com/nguyendatgh/chat_app-client/blob/master/src/vite-env.d.ts"

    Config --> ESLint
    Config --> TSConfig
    Config --> ViteConfig
    Config --> EnvTypes

    %% Connections between layers
    Pages -->|"composes"| UIComponents
    UIComponents -->|"triggers"| Hooks
    Hooks -->|"updates"| Contexts
    Hooks -->|"updates"| Store
    API ---|"responds to"| Hooks
    Styles ---|"applied to"| UIComponents

    %% Style Definitions
    classDef entry fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef pages fill:#87CEFA,stroke:#333,stroke-width:2px;
    classDef components fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef state fill:#FFA500,stroke:#333,stroke-width:2px;
    classDef api fill:#FFFF99,stroke:#333,stroke-width:2px;
    classDef hooks fill:#DDA0DD,stroke:#333,stroke-width:2px;
    classDef config fill:#D3D3D3,stroke:#333,stroke-width:2px;
    classDef styling fill:#E0FFFF,stroke:#333,stroke-width:2px;
```
