# knight-safe/knight-safe-interface architecture

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

- Interactive diagram: https://gitdiagram.com/knight-safe/knight-safe-interface
- Repository: https://github.com/knight-safe/knight-safe-interface

## Overview

##### Main Components
- Frontend UI Layer: This includes the React application (src/index.js, App components, AppRoute, AppLayout) and is built around a single-page application structure.
- Pages/Views: Organized under the src/page folder, these represent key user journeys, such as DashboardPage, AssetsPage, OnboardingPages, SettingsPages, etc.
- State Management: The src/atom directory contains many state “atoms” for shared state (e.g., connectModalState, chainState, profileState). These are central to managing the application’s reactive flows.
- Business Logic & Hooks: Custom hooks (in src/hooks) like useInitWalletConnect, useGetTokenRate, useLogout encapsulate key logic such as wallet connectivity and profile refresh.
- Constants and Configuration: Files under src/constant (including smart contract ABIs, token lists, wallet mapping, RPC endpoints, and error messaging) represent the configuration and integration points with blockchain networks.

##### Interactions and Relationships
- The App’s root component (src/index.js) initializes the application and sets up routing (via AppRoute) which determines which Page component to render.
- The Page components call on custom hooks to interact with the blockchain (e.g., retrieving token rates, obtaining profiles) and trigger state management via atoms.
- UI components (such as ModalConnectWallet, ModalLoading, etc.) listen to state changes and, when needed, prompt user actions or display status.
- Business logic is encapsulated in hooks that use the constants (e.g., contract ABIs, RPC endpoints) to connect with external blockchain services.
- The assets and configuration files provide supporting data for visual display and proper integration with external services.

##### Architectural Patterns and Design Principles
- Component-Based Structure: Each UI element is modular and reusable.
- Separation of Concerns: Divide the application into layers (UI, state management, business logic, configuration) so that each has a single responsibility.
- Declarative Routing: Using AppRoute to manage navigation amongst different pages.
- State Management via Atoms: Centralizes and shares state changes across a complex, dynamic UI.
- External Integrations: Abstract smart contract interactions through constants and hooks, minimizing coupling between UI and blockchain logic.

##### Specific Elements
- From the App (router) to Page components.
- From Page components to custom hooks.
- From Hooks to Constants/Smart Contract services.
- From State Atoms to UI components (modals, menus).
- UI layers (pages, layout, components).
- Logic layers (hooks, utilities).
- State management (atoms).
- External integration points (blockchain service endpoints, wallet connection functionalities).
- Integration Points and Dependencies: Clearly mark how wallet connectivity (via useInitWalletConnect and ModalConnectWallet) and contract-related constants (under src/constant/contract) interact with the business logic and UI.

##### Final Reminder
- Stay high-level but include key internal flows such as user actions triggering state changes, which then initiate blockchain connectivity or profile updates through hooks and utility functions.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend UI Layer
    subgraph "Frontend UI Layer"
        UI1["src/index.js"]:::frontend
        UI2["src/App/App.js"]:::frontend
        UI3["src/App/AppRoute.js"]:::frontend
        UI4["src/App/Applayout.js"]:::frontend
    end

    %% Pages/Views
    subgraph "Pages/Views"
        P1["DashboardPage"]:::pages
        P2["AssetsPage"]:::pages
        P3["OnboardingNewPage"]:::pages
        P4["SettingsPage"]:::pages
    end

    %% Reusable Components (UI Library)
    subgraph "Reusable Components (UI Library)"
        C1["ModalConnectWallet"]:::components
        C2["ModalLoading"]:::components
        C3["ModalConfirm"]:::components
        C4["ModalSendAsset"]:::components
        C5["ListAppsItem"]:::components
        C6["MainHeader"]:::components
        C7["TokenBox"]:::components
    end

    %% State Management (Atoms)
    subgraph "State Management (Atoms)"
        A1["chainState"]:::atoms
        A2["confirmModalState"]:::atoms
        A3["connectModalState"]:::atoms
        A4["profileState"]:::atoms
    end

    %% Business Logic & Custom Hooks
    subgraph "Business Logic & Custom Hooks"
        H1["useInitWalletConnect"]:::hooks
        H2["useGetTokenRate"]:::hooks
        H3["useRefreshProfile"]:::hooks
        H4["useLogout"]:::hooks
        U1["createWalletConnect (Utils)"]:::hooks
        U2["getKnightSafeProfile (Utils)"]:::hooks
        U3["checkIsKnightSafe (Utils)"]:::hooks
    end

    %% Constants and Configuration
    subgraph "Constants and Configuration"
        CT1["TOKEN_LIST"]:::config
        CT2["ERROR_MESSAGE"]:::config
        CT3["Contract ABIs"]:::config
        CT4["walletMapping"]:::config
        CT5["rpc"]:::config
        CT6["functionMapping"]:::config
    end

    %% Assets & Localization
    subgraph "Assets & Localization"
        AL1["Assets (Images)"]:::assets
        AL2["en.json (Localization)"]:::assets
    end

    %% Interactions
    %% Frontend UI Layer -> Pages/Views
    UI3 -->|"routes_to"| P1
    UI3 -->|"routes_to"| P2
    UI3 -->|"routes_to"| P3
    UI3 -->|"routes_to"| P4

    %% Pages/Views -> Business Logic & Custom Hooks
    P1 -->|"triggers"| H1
    P1 -->|"triggers"| H2
    P1 -->|"triggers"| H3
    P1 -->|"triggers"| H4

    P2 -->|"triggers"| H1
    P3 -->|"triggers"| H2
    P4 -->|"triggers"| H3

    %% Business Logic -> Utils
    H1 -->|"calls"| U1
    H2 -->|"calls"| U2
    H3 -->|"calls"| U3

    %% Business Logic -> Constants/Configuration
    H1 -->|"accesses"| CT3
    H2 -->|"accesses"| CT1
    H3 -->|"accesses"| CT5
    H4 -->|"accesses"| CT2

    %% Business Logic -> State Management (update state)
    H1 -->|"updates"| A3
    H2 -->|"updates"| A4

    %% State Management -> Reusable Components
    A3 -->|"triggers UI change"| C1
    A4 -->|"triggers UI change"| C2
    A2 -->|"controls"| C3
    A1 -->|"provides data"| C7

    %% Frontend Layout uses Reusable Components
    UI2 -->|"renders"| C6
    C5 -->|"complements"| C7

    %% Assets & Localization used by Frontend and Components
    UI2 -->|"renders"| AL1
    C6 -->|"utilizes"| AL2

    %% Styles
    classDef frontend fill:#a3d5ff,stroke:#2a9d8f,stroke-width:2px;
    classDef pages fill:#ffb3b3,stroke:#e63946,stroke-width:2px;
    classDef components fill:#ffe066,stroke:#ffb703,stroke-width:2px;
    classDef atoms fill:#caffbf,stroke:#2b9348,stroke-width:2px;
    classDef hooks fill:#f6bd60,stroke:#f28482,stroke-width:2px;
    classDef config fill:#d8e2dc,stroke:#e29578,stroke-width:2px;
    classDef assets fill:#bde0fe,stroke:#03045e,stroke-width:2px;

    %% Click Events - Frontend UI Layer
    click UI1 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/index.js"
    click UI2 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/App/App.js"
    click UI3 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/App/AppRoute.js"
    click UI4 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/App/Applayout.js"

    %% Click Events - Pages/Views
    click P1 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/page/DashboardPage.js"
    click P2 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/page/AssetsPage.js"
    click P3 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/page/OnboardingNewPage.js"
    click P4 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/page/SettingsPage.js"

    %% Click Events - Reusable Components
    click C1 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/component/ModalConnectWallet.js"
    click C2 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/component/ModalLoading.js"
    click C3 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/component/ModalConfirm.js"
    click C4 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/component/ModalSendAsset.js"
    click C5 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/component/ListAppsItem.js"
    click C6 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/component/MainHeader.js"
    click C7 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/component/TokenBox.js"

    %% Click Events - State Management (Atoms)
    click A1 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/atom/chainState.js"
    click A2 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/atom/confirmModalState.js"
    click A3 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/atom/connectModalState.js"
    click A4 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/atom/profileState.js"

    %% Click Events - Business Logic & Custom Hooks
    click H1 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/hooks/useInitWalletConnect.js"
    click H2 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/hooks/useGetTokenRate.js"
    click H3 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/hooks/useRefreshProfile.js"
    click H4 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/hooks/useLogout.js"
    click U1 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/utils/createWalletConnect.js"
    click U2 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/utils/getKnightSafeProfile.js"
    click U3 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/utils/checkIsKnightSafe.js"

    %% Click Events - Constants and Configuration
    click CT1 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/constant/TOKEN_LIST.js"
    click CT2 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/constant/ERROR_MESSAGE.js"
    click CT3 "https://github.com/knight-safe/knight-safe-interface/tree/main/src/constant/contract/"
    click CT4 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/constant/walletMapping.js"
    click CT5 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/constant/rpc.js"
    click CT6 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/constant/functionMapping.js"

    %% Click Events - Assets & Localization
    click AL1 "https://github.com/knight-safe/knight-safe-interface/tree/main/src/assets"
    click AL2 "https://github.com/knight-safe/knight-safe-interface/blob/main/src/lang/en.json"
```
