# magicdawn/bilibili-gate architecture

> Architecture diagram of the GitHub repository magicdawn/bilibili-gate, 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/magicdawn/bilibili-gate
- Repository: https://github.com/magicdawn/bilibili-gate

## Overview

- User Interface (UI) Layer – This includes the React components located under src/components (AppRoot, Modal components, VideoCard, Header, etc.) and styling (CSS/SCSS files). These components form the visible interface that users interact with.
– Access Key Management (including TV QR code auth)
– Bilibili-related utilities (bilibili data, player configuration, risk control)
– Recommendation and service modules (for “App 推荐”, dynamic feeds, favorites, live, watchlater, etc.)
– Settings and compatibility modules (adjustments for dark mode, integration with Bilibili-Evolved, etc.)
- Utility Layer – All the common helper functions (src/utility) for operations like async handling, cookies, DOM manipulations, API calls, and state management (using valtio—a state management library).
- Configuration and Build Tools – Files like vite.config.ts, tsconfig.json, knip.config.ts, and others. These reflect the build and code quality configuration, although they are not runtime components, they are key for development and toolchain integration.

##### Relationships and flows
- That configuration files and build scripts (Vite, turbo.json, etc.) support the entire development and build lifecycle, even if not executed at runtime.

##### Important architectural patterns or design principles
- Separation of concerns: The project clearly divides UI (src/components) from business logic (src/modules) and shared utilities (src/utility).
- Modular design: With specialized directories for each major feature (for different recommendation types, dynamic feeds, favorites, etc.), the architecture follows a modular design where each module is responsible for a specific set of functionalities.
- State management: The use of valtio for handling client state and hooks for lifecycle management in React (as seen in src/common/hooks) are important design choices.
- Compatibility and extensibility: Adapters for Bilibili-Evolved and dark mode compatibility are carefully integrated, indicating the design’s attention to interoperability.

##### Relevant technologies and frameworks
- React & TypeScript for the UI.
- Vite for bundling and building the project.
- pnpm as the package manager.
- CSS/SCSS for styling.
- State management using valtio.
- Third-party integrations like ant-design components, Dark Reader for dark mode, and compatibility layers for other userscripts.

- Clear labels for each component:
– UI Components (e.g., AppRoot, VideoCard, Header, various modals)
– Modules (e.g., rec-services, access-key, dark-mode, settings)
– Utility services (e.g., async operations, cookie management, API helpers)
– Communication between modules and shared utility functions.
– For utility services,

##### Overall advice
- Don’t over-complicate by including every minor file; instead, focus on the high-level architectural components that delineate the core functionalities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    CFG["Configuration & Build Tools"]:::Config

    subgraph "UI Layer"
        A["AppRoot (UI)"]:::UI
        VC["VideoCard (UI)"]:::UI
        MD["ModalDislike (UI)"]:::UI
        MF["ModalFeed (UI)"]:::UI
        MS["ModalSettings (UI)"]:::UI
        S["Styling (UI)"]:::UI
        H["Shared Hooks"]:::Hooks
    end

    subgraph "Modules/Business Logic"
        AK["Access Key Management"]:::Module
        RS["Recommendation Services"]:::Module
        BU["Bilibili Utilities"]:::Module
        ST["Settings"]:::Module
        DM["Dark Mode"]:::Module
    end

    UF["Utility Functions"]:::Utility
    BAPI["Bilibili API (External)"]:::External
    TPL["Third Party Libraries (External)"]:::External

    %% Connections from Configuration to UI and Modules
    CFG --> A
    CFG --> AK

    %% UI triggering Modules (User interactions)
    A -->|"triggers"| AK
    A -->|"triggers"| RS
    A -->|"triggers"| BU
    A -->|"triggers"| ST
    A -->|"triggers"| DM

    %% UI components using Shared Hooks
    A -->|"uses"| H
    VC -->|"uses"| H
    MD -->|"uses"| H
    MF -->|"uses"| H
    MS -->|"uses"| H

    %% Modules calling Utility Functions
    AK -->|"calls"| UF
    RS -->|"calls"| UF
    BU -->|"calls"| UF
    ST -->|"calls"| UF
    DM -->|"calls"| UF

    %% Modules interacting with External Services
    RS -->|"fetches"| BAPI
    BU -->|"fetches"| BAPI
    ST -->|"uses"| TPL
    DM -->|"uses"| TPL

    %% Additional connection: Configuration supporting Utility Functions
    CFG --> UF

    %% Click Events for UI Layer
    click A "https://github.com/magicdawn/bilibili-gate/blob/main/src/components/AppRoot.tsx"
    click VC "https://github.com/magicdawn/bilibili-gate/tree/main/src/components/VideoCard"
    click MD "https://github.com/magicdawn/bilibili-gate/tree/main/src/components/ModalDislike"
    click MF "https://github.com/magicdawn/bilibili-gate/tree/main/src/components/ModalFeed"
    click MS "https://github.com/magicdawn/bilibili-gate/tree/main/src/components/ModalSettings"
    click S "https://github.com/magicdawn/bilibili-gate/tree/main/src/common"
    click H "https://github.com/magicdawn/bilibili-gate/tree/main/src/common/hooks"

    %% Click Events for Modules/Business Logic
    click AK "https://github.com/magicdawn/bilibili-gate/tree/main/src/modules/access-key"
    click RS "https://github.com/magicdawn/bilibili-gate/tree/main/src/modules/rec-services"
    click BU "https://github.com/magicdawn/bilibili-gate/tree/main/src/modules/bilibili"
    click ST "https://github.com/magicdawn/bilibili-gate/tree/main/src/modules/settings"
    click DM "https://github.com/magicdawn/bilibili-gate/tree/main/src/modules/dark-mode"

    %% Click Event for Utility Functions
    click UF "https://github.com/magicdawn/bilibili-gate/tree/main/src/utility"

    %% Click Event for Configuration & Build Tools (using main config file)
    click CFG "https://github.com/magicdawn/bilibili-gate/blob/main/vite.config.ts"

    %% Styles
    classDef UI fill:#cce5ff,stroke:#1a8cff,stroke-width:2px;
    classDef Module fill:#d4edda,stroke:#28a745,stroke-width:2px;
    classDef Utility fill:#fff3cd,stroke:#ffc107,stroke-width:2px;
    classDef Config fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef External fill:#f8d7da,stroke:#dc3545,stroke-width:2px;
    classDef Hooks fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
```
