# fengjing95/project-preview architecture

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

- Interactive diagram: https://gitdiagram.com/fengjing95/project-preview
- Repository: https://github.com/fengjing95/project-preview

## Overview

- User Interface Layer:
– Components folder under src/components. This includes sub-components for the Editor (monaco-editor integration), FileTree, Header (with controls such as ShareToggle and StartToggle), Preview pane, Terminal (xTerm integration), ThemeProvider for theme toggling, and several UI components (buttons, dialogs, inputs, etc.).
- Adapter/Integration Layer:
– The adapters directory contains RepositoryAdapter implementations for GitHub, GitLab, and Gitee. These adapters abstract the interactions with external repository services.
- Core / Infrastructure Layer:
– The services folder (specifically the WebContainerService) is responsible for initializing the devServer environment, mounting the virtual file system from WebContainer, and managing dependency installs and file synchronization in the browser.
- State Management:
– The store directory (global, repo & terminal) handles application state management for different parts of the application (repositories, terminal sessions, etc.).
- Utility Libraries:
– The lib folder and hooks provide utility functions and custom hooks (e.g., useResize, useContainer) that aid in component interaction and DOM management.
- Configuration & Build-Related Files:
– Various config files are available (Vite, Tailwind, TypeScript) along with an index.html, ensuring that the front-end bundling and development environment are set up accordingly.

##### Out the relationships and data flow
- That the user interacts with the UI Components (Editor, FileTree, Terminal, Preview) that are arranged in a panel-based layout (PanelGroup) with a top header for controls.
- That these UI components communicate with the state management layer (the store) for synchronizing data, such as repository information or editor state.
- The RepositoryAdapter layer fetches code from external services (GitHub, GitLab, Gitee) and feeds the data into the state/store which in turn gets rendered by the FileTree and other components.
- The WebContainerService manages the devServer, mounting the file system and handling dependency installs. It provides data to the Preview component and Terminal for live updates and interaction.
- Utilities and hooks support all layers, e.g., for clipboard operations, theming, event handling, and responsive design.

##### Architectural patterns
- Component-Based Architecture: The project uses a modular UI component approach, where each subcomponent is clearly defined and isolated (Editor, Header, etc.).
- Separation of Concerns: Adapters, services, state management, and UI presentation responsibilities are clearly separated.
- Event-Driven Interactions: Use of custom hooks and event emitter modules (e.g., evenemitter.ts) indicates that components react to state and event changes.
- Integration Layer Abstraction: The common RepositoryAdapter interface demonstrates the Strategy design pattern for handling different repository services uniformly.

##### Technology specifics
- Framework & Build: Vite, TypeScript
- Styling: TailwindCSS, shadcn/ui
- Editor Integration: monaco-editor
- Terminal: xTerm
- Container-based DevServer: WebContainer for file system mounting and dependency management

##### Elements
- Clear labels for each component (UI Components, Repository Adapters, WebContainerService, Store/State Management, Utilities/Libraries).
– Rectangles for major components (e.g., UI layers, services).

##### Final advice
- Be as detailed as possible with subcomponents so that every major functionality (code editor, terminal, file tree, repository fetching, WebContainer integration) is accounted for. Use annotations if needed to clarify the flow.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global External Node
    User["User"]:::external

    %% UI Layer
    subgraph "UI Layer"
        Header["Header"]:::ui
        ShareToggle["ShareToggle"]:::ui
        StartToggle["StartToggle"]:::ui
        Editor["Editor"]:::ui
        FileTree["FileTree"]:::ui
        Preview["Preview Pane"]:::ui
        Terminal["Terminal"]:::ui
        ThemeProvider["ThemeProvider"]:::ui
        ThemeToggle["ThemeToggle"]:::ui
        OtherUI["Other UI Components"]:::ui
        PanelGroup["PanelGroup"]:::ui
    end

    %% State Management Layer
    subgraph "State Management"
        GlobalState["Global State"]:::state
        RepoState["Repository State"]:::state
        TerminalState["Terminal State"]:::state
    end

    %% Adapter/Integration Layer
    subgraph "Adapter/Integration Layer"
        RepositoryAdapter["RepositoryAdapter"]:::adapter
        GitHubAdapter["GitHubAdapter"]:::adapter
        GitLabAdapter["GitLabAdapter"]:::adapter
        GiteeAdapter["GiteeAdapter"]:::adapter
    end

    %% Core / Infrastructure Layer
    subgraph "Core / Infrastructure"
        WebContainerService["WebContainerService"]:::core
    end

    %% Utility Libraries
    subgraph "Utility Libraries"
        Lib["Lib"]:::util
        Hooks["Hooks"]:::util
    end

    %% User interactions with UI components
    User -->|"interacts"| Header
    User -->|"interacts"| Editor
    User -->|"interacts"| FileTree
    User -->|"interacts"| Preview
    User -->|"interacts"| Terminal
    User -->|"interacts"| ThemeProvider
    User -->|"interacts"| OtherUI
    User -->|"interacts"| PanelGroup

    %% UI Components communicate with State Management
    Header <-->|"state-sync"| GlobalState
    Editor <-->|"state-sync"| GlobalState
    FileTree <-->|"state-sync"| RepoState
    Terminal <-->|"state-sync"| TerminalState

    %% Header subcomponents
    Header -->|"contains"| ShareToggle
    Header -->|"contains"| StartToggle
    ThemeProvider -->|"contains"| ThemeToggle

    %% Repository Adapters flow into State
    GitHubAdapter -->|"feeds"| RepositoryAdapter
    GitLabAdapter -->|"feeds"| RepositoryAdapter
    GiteeAdapter -->|"feeds"| RepositoryAdapter
    RepositoryAdapter -->|"updates"| RepoState

    %% WebContainerService provides live updates to UI
    WebContainerService -->|"updates"| Preview
    WebContainerService -->|"updates"| Terminal

    %% Utility Libraries support UI and Core functionality
    Lib ---|"supports"| Header
    Lib ---|"supports"| Editor
    Lib ---|"supports"| FileTree
    Lib ---|"supports"| Preview
    Lib ---|"supports"| Terminal
    Lib ---|"supports"| ThemeProvider
    Lib ---|"supports"| PanelGroup

    Hooks ---|"supports"| WebContainerService
    Hooks ---|"supports"| Editor

    %% Click Events for UI Layer
    click Editor "https://github.com/fengjing95/project-preview/blob/main/src/components/Editor/index.tsx"
    click FileTree "https://github.com/fengjing95/project-preview/blob/main/src/components/FileTree/index.tsx"
    click Header "https://github.com/fengjing95/project-preview/blob/main/src/components/Header/index.tsx"
    click ShareToggle "https://github.com/fengjing95/project-preview/blob/main/src/components/Header/ShareToggle.tsx"
    click StartToggle "https://github.com/fengjing95/project-preview/blob/main/src/components/Header/StartToggle.tsx"
    click Preview "https://github.com/fengjing95/project-preview/blob/main/src/components/Preview/index.tsx"
    click Terminal "https://github.com/fengjing95/project-preview/blob/main/src/components/Terminal/index.tsx"
    click ThemeProvider "https://github.com/fengjing95/project-preview/blob/main/src/components/ThemeProvider/index.tsx"
    click ThemeToggle "https://github.com/fengjing95/project-preview/blob/main/src/components/ThemeProvider/ThemeToggle.tsx"
    click OtherUI "https://github.com/fengjing95/project-preview/tree/main/src/components/ui"
    click PanelGroup "https://github.com/fengjing95/project-preview/blob/main/src/components/PanelGroup/index.tsx"

    %% Click Events for Adapter/Integration Layer
    click GitHubAdapter "https://github.com/fengjing95/project-preview/blob/main/src/adapters/GitHubAdapter.ts"
    click GitLabAdapter "https://github.com/fengjing95/project-preview/blob/main/src/adapters/GitLabAdapter.ts"
    click GiteeAdapter "https://github.com/fengjing95/project-preview/blob/main/src/adapters/GiteeAdapter.ts"
    click RepositoryAdapter "https://github.com/fengjing95/project-preview/blob/main/src/adapters/RepositoryAdapter.ts"

    %% Click Event for Core / Infrastructure Layer
    click WebContainerService "https://github.com/fengjing95/project-preview/blob/main/src/services/WebContainerService.ts"

    %% Click Events for State Management
    click GlobalState "https://github.com/fengjing95/project-preview/blob/main/src/store/global.ts"
    click RepoState "https://github.com/fengjing95/project-preview/blob/main/src/store/repo.ts"
    click TerminalState "https://github.com/fengjing95/project-preview/blob/main/src/store/terminal.ts"

    %% Click Events for Utility Libraries
    click Lib "https://github.com/fengjing95/project-preview/tree/main/src/lib"
    click Hooks "https://github.com/fengjing95/project-preview/tree/main/src/hooks"

    %% Styles
    classDef ui fill:#FDE68A,stroke:#F59E0B,stroke-width:2px;
    classDef adapter fill:#A7F3D0,stroke:#10B981,stroke-width:2px;
    classDef core fill:#BFDBFE,stroke:#3B82F6,stroke-width:2px;
    classDef state fill:#E9D5FF,stroke:#8B5CF6,stroke-width:2px;
    classDef util fill:#FECACA,stroke:#EF4444,stroke-width:2px;
    classDef external fill:#E5E7EB,stroke:#6B7280,stroke-width:2px;
```
