# amatiasq/pensieve architecture

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

## Overview

##### Main Components
- User Interface (UI) / Frontend
– Represented mainly by the “src/5-app”, “src/7-components”, and “index.html”.
– Contains the React-based routes (e.g., CreateNote, EditNote, EditSettings) and global contexts.
– Incorporates the Monaco Editor (in “src/7-components/Editor” and its monaco extensions) for sophisticated text editing.

- Business Logic / Core Services
– Lives in folders “src/0-dom” (DOM utilities), “src/1-core” (services such as message bus, HTTP helper, event handlers like busy-indicator, keyboard, mouse, URL handling).
– Implements various application helpers that “glue” UI events with data processing.

- Domain Entities
– Found under “src/2-entities” (Note, Settings, Shortcuts).
– These encapsulate the core data models of the application.

- GitHub Integration
– This layer handles communication with GitHub, ensuring changes in notes are recorded as Git commits.

- Storage & Middleware Layer
– The “src/4-storage” directory is a dedicated storage layer that abstracts how data is saved and fetched.
– Multiple middleware components (such as CachedStore, ForageStore, GHRepoStore, MixedStore, and ResilientOnlineStore) wrap the storage mechanism, handling local (and possibly remote) caching and ensuring resiliency.
– This layer coordinates with both local persistence and remote storage (synchronizing with GitHub).

- API Endpoints / Serverless Functions
– Separate API functional code can be seen in “src/api”—this folder likely encapsulates endpoints (or “wrangler.toml” indicates possible Cloudflare Worker deployment) for handling auth, commit actions, etc.
– It plays a supporting role to the frontend, ensuring integration with services.

- Testing & DevOps
– The “cypress” folder denotes end-to-end tests.
– CI/CD workflows are defined in “.github/workflows/ci.yml”.
– Tools like ESLint and Prettier (with their respective config files) ensure code consistency.
– Vite is configured via “vite.config.ts” and project-specific TypeScript settings are in “tsconfig.json”.

##### Relationships and Interactions
– UI components communicate with the business logic via React Hooks (see “src/6-hooks”), which abstract many behaviors like note creation, navigation, or settings updates.
– The business logic uses domain entities to model data and then interacts with the storage layer (src/4-storage) for both local caching and remote persistence.
– The GitHub integration layer sits between the note operations and remote storage, converting note updates into Git operations (commits, branch updates, etc.) and handling user authentication.
– The API layer (in “src/api”) might serve as a gateway for some operations, bridging the gap between the UI and GitHub backend if needed.
– There are also utility layers (DOM and other utilities in “src/util”) that support the core functions and debugging.

##### Architectural Patterns and Principles
– Modular layering: Separation of concerns is clear from UI, business logic, domain models, storage, and external integrations.
– Middleware and cascading store mechanisms in the storage layer allow combining local and remote data strategies.
– React’s component architecture paired with hooks provides decoupled state management and user interaction handling.
– Integration with external APIs (GitHub) is abstracted into a dedicated layer, allowing for easier future extension or mock/testing.

##### Relevant Technologies
- Frontend: React (function components, hooks), TypeScript, Vite, Monaco Editor.
- Testing: Cypress for end-to-end tests.
- GitHub APIs: REST and GraphQL clients.
- CI/CD: GitHub Actions (defined in “.github/workflows”).
- Package managers: Bun and npm (package.json, bun.lockb, etc.)
- Build and code style tooling: ESLint, Prettier, Visual Studio Code configs.

##### Overall Focus
– Don’t hesitate to break down the middleware storage layer into multiple components if it aids clarity.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Frontend UI
    subgraph "Frontend UI"
        UI_App["UI/React App"]:::frontend
        UI_Components["UI Components"]:::frontend
        MainHTML["index.html"]:::frontend
        UI_App --- UI_Components
        UI_Components --- MainHTML
    end

    %% React Integration
    subgraph "React Integration"
        RH["React Hooks"]:::react
    end

    %% Business Logic
    subgraph "Business Logic"
        CS["Core Services"]:::logic
        DOM["DOM Utils"]:::logic
        CS --- DOM
    end

    %% Domain Entities
    subgraph "Domain Entities"
        NOTE["Note"]:::entity
        SET["Settings"]:::entity
        SHORT["Shortcuts"]:::entity
        NOTE --- SET
        SET --- SHORT
    end

    %% GitHub Integration
    subgraph "GitHub Integration"
        GHI["GitHub Integration"]:::github
        GHM["GH Models"]:::github
        GHI --- GHM
    end

    %% Storage & Middleware Layer
    subgraph "Storage & Middleware"
        AS["App Storage"]:::storage
        CS_cached["CachedStore"]:::storage
        FS["ForageStore"]:::storage
        GRS["GHRepoStore"]:::storage
        MS["MixedStore"]:::storage
        ROS["ResilientOnlineStore"]:::storage
        AS -->|"wrapped_by"| CS_cached
        CS_cached -->|"wrapped_by"| FS
        FS -->|"wrapped_by"| GRS
        GRS -->|"wrapped_by"| MS
        MS -->|"wrapped_by"| ROS
    end

    %% API Endpoints
    subgraph "API Endpoints"
        API_A["Auth Endpoint"]:::api
        API_C["Commit Endpoint"]:::api
        API_I["Index Endpoint"]:::api
        API_W["Wrangler Config"]:::api
        API_A --- API_C
        API_C --- API_I
        API_I --- API_W
    end

    %% Testing & DevOps
    subgraph "Testing & DevOps"
        CY["Cypress Tests"]:::devops
        GHA["GitHub Actions CI"]:::devops
        ESL["ESLint"]:::devops
        PR["Prettier"]:::devops
        VITE["Vite Config"]:::devops
        TSC["TypeScript Config"]:::devops
        CY --- GHA
        GHA --- ESL
        ESL --- PR
        PR --- VITE
        VITE --- TSC
    end

    %% Flow Connections
    UI_App -->|"triggers"| RH
    UI_Components -->|"renders"| RH
    MainHTML -->|"loads"| UI_App

    RH -->|"invokes"| CS
    RH -->|"invokes"| DOM

    CS -->|"processes"| NOTE
    CS -->|"processes"| SET
    CS -->|"processes"| SHORT

    CS -->|"stores"| AS
    CS -->|"syncs"| GHI
    CS -->|"calls"| API_A
    DOM -->|"calls"| API_I
    GHI -->|"interacts"| API_W

%% Styles
classDef frontend fill:#D6EAF8,stroke:#2874A6,stroke-width:2px;
classDef react fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
classDef logic fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
classDef entity fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
classDef github fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
classDef storage fill:#FCF3CF,stroke:#F1C40F,stroke-width:2px;
classDef api fill:#D6DBDF,stroke:#566573,stroke-width:2px;
classDef devops fill:#FDEBD0,stroke:#D35400,stroke-width:2px;

%% Click Events
click UI_App "https://github.com/amatiasq/pensieve/tree/main/src/5-app"
click UI_Components "https://github.com/amatiasq/pensieve/tree/main/src/7-components"
click MainHTML "https://github.com/amatiasq/pensieve/blob/main/index.html"
click RH "https://github.com/amatiasq/pensieve/tree/main/src/6-hooks"
click CS "https://github.com/amatiasq/pensieve/tree/main/src/1-core"
click DOM "https://github.com/amatiasq/pensieve/tree/main/src/0-dom"
click NOTE "https://github.com/amatiasq/pensieve/blob/main/src/2-entities/Note.ts"
click SET "https://github.com/amatiasq/pensieve/blob/main/src/2-entities/Settings.ts"
click SHORT "https://github.com/amatiasq/pensieve/blob/main/src/2-entities/Shortcuts.ts"
click GHI "https://github.com/amatiasq/pensieve/tree/main/src/3-github"
click GHM "https://github.com/amatiasq/pensieve/tree/main/src/3-github/models"
click AS "https://github.com/amatiasq/pensieve/tree/main/src/4-storage"
click CS_cached "https://github.com/amatiasq/pensieve/blob/main/src/4-storage/CachedStore.ts"
click FS "https://github.com/amatiasq/pensieve/blob/main/src/4-storage/ForageStore.ts"
click GRS "https://github.com/amatiasq/pensieve/blob/main/src/4-storage/GHRepoStore.ts"
click MS "https://github.com/amatiasq/pensieve/blob/main/src/4-storage/MixedStore.ts"
click ROS "https://github.com/amatiasq/pensieve/blob/main/src/4-storage/ResilientOnlineStore.ts"
click API_A "https://github.com/amatiasq/pensieve/blob/main/src/api/auth.js"
click API_C "https://github.com/amatiasq/pensieve/blob/main/src/api/commit.js"
click API_I "https://github.com/amatiasq/pensieve/blob/main/src/api/index.js"
click API_W "https://github.com/amatiasq/pensieve/blob/main/src/api/wrangler.toml"
click CY "https://github.com/amatiasq/pensieve/tree/main/cypress"
click GHA "https://github.com/amatiasq/pensieve/blob/main/.github/workflows/ci.yml"
click ESL "https://github.com/amatiasq/pensieve/blob/main/.eslintrc"
click PR "https://github.com/amatiasq/pensieve/blob/main/.prettierrc"
click VITE "https://github.com/amatiasq/pensieve/blob/main/vite.config.ts"
click TSC "https://github.com/amatiasq/pensieve/blob/main/tsconfig.json"
```
