# joshwrn/scribble-ai architecture

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

- Interactive diagram: https://gitdiagram.com/joshwrn/scribble-ai
- Repository: https://github.com/joshwrn/scribble-ai

## Overview

##### Main Components

- Frontend (Client Side):
- Next.js App: Note components like src/app/layout.tsx and src/app/page.tsx which form the entry points of your application.
- UI Components: The src/components directory is rich with components that implement resizable windows (Window/*), context menus, toolbars, debug tools, and an infinite canvas (likely represented by components like Space/Space.tsx and Window/Canvas/Canvas.tsx). Also include smaller UI elements (e.g., ContextMenu, DropDownMenu, ItemList, etc.).
- Client State Management: The src/state directory holds state modules that manage UI state (canvas, windows, notifications, etc.) across the app.

- Backend / API Layer:
- API Routes: Under src/app/api, you have endpoints dealing with tasks like image URL to Base64 conversion, scenario management, and a proxy route for “fal” (potentially a custom AI or generative art feature).
- Server Utilities: In src/server, there are additional endpoints and logic (like imageUrlToBase64/fetchImageUrlToBase64.ts and scenario/fetchScenario.ts) that are likely used by API routes to process data.
- External Service Interactions: Even though this is mostly a client-side interactive experimental app, the presence of API routes indicates interactions with backend logic for generative art or image conversion.

- Utility and Supporting Layers:
- Logic: The src/logic folder contains functions for layout, image processing (convertImageToBase64, resizeImage), and mathematical utilities (rotatePoint, findCenterPoint, etc.).
- Fal Module: A dedicated area (src/fal) appears to be handling the “fal” aspect, with optimized latency logic and workflows (src/fal/workflows), possibly managing AI or generative art processing.
- Mock Data: The src/mock folder provides mock endpoints/data which likely support testing and development.
- Scripts: The src/scripts folder is used to auto-generate API routes and stores, helping to keep the codebase synchronized with architectural changes.
- Testing: The tests folder contains end-to-end tests (like window-resize.spec.ts) to ensure the dynamic UI behaves as expected.

##### Relationships and Data Flow

a. User Interaction Flow:
- A user’s browser initiates a request that is managed by Next.js. The browser loads the UI from src/app and the individual UI components. These components then interact with the state management modules.
- When a user action requires data processing (e.g., converting an image URL or invoking an AI art process), the component calls an API endpoint (e.g., api/imageUrlToBase64/route.ts or api/scenario/route.ts).

b. API Interactions:
- The API endpoints in src/app/api forward requests to server-side functions located in src/server. They may also depend on logic functions (in src/logic) for processing data and return responses back to the client.
- The fal system also uses its own workflow (src/fal/workflows) to optimize latency during these operations.

c. State and Utility Integration:
- UI components listen to changes in state objects within src/state. Global functions, helpers, and utilities from src/utils and src/logic support the business logic and ensure consistency in user interactions.

##### Important Architectural Patterns & Technologies

- Next.js Folder-based Routing: The use of src/app, with layout.tsx and API routes, indicates that routing and page assembly follow Next.js conventions.
- Component-Based UI: The separation of UI into many specialized components (windows, menus, toolbars) follows modern React design principles.
- Modular State Management: The src/state folder serves to globally manage UI and application state, ensuring state synchronization across the dynamic components.
- Separation of Concerns: Clearly separated directories for UI, business logic, server-side endpoints, and scripts demonstrate clean modularization.
- Test-Driven Development: The tests directory shows an effort to verify the behavior of interactive components (e.g., window resizing).

##### Detailing Architectural Elements

- All interaction flows from user actions in the client browser to corresponding API calls and back-end server processing.
- The modular separation of UI components with grouped directories (like Window components grouped together, context menus grouped, etc.).
- The use of Next.js intrinsic routing and API construction as central to the architecture.
- The integration of AI/generative art functionalities (possibly under “fal”) to illustrate how specific workflows tie into the overall system.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Browser("User Browser"):::browser

    subgraph "Frontend Layer"
        NLayout("Next.js Layout (layout.tsx)"):::frontend
        NPage("Next.js Page (page.tsx)"):::frontend
        NStyling("Next.js Styling (page.module.scss)"):::frontend

        subgraph "UI Components"
            UI_Main("Main UI Components"):::frontend
            UI_Window("Resizable Windows & Canvas"):::frontend
            UI_Context("Context Menus & Dropdowns"):::frontend
            UI_Space("Space / Infinite Canvas"):::frontend
        end

        State("Client State Management"):::state
    end

    subgraph "Backend/API Layer"
        subgraph "API Endpoints"
            API_Image("Image to Base64 API"):::api
            API_Scenario("Scenario API"):::api
            API_Fal("Fal Proxy API"):::api
        end

        subgraph "Server Utilities & Fal"
            subgraph "Server Utilities"
                SU_Image("Image Processing Utility"):::server
                SU_Scenario("Scenario Fetch Utility"):::server
                SU_Additional("Additional Endpoints & Handlers"):::server
            end
            subgraph "Fal Module"
                Fal_Core("Fal Core Logic"):::fal
                Fal_Workflows("Fal Workflows"):::fal
            end
        end
    end

    subgraph "Utility & Supporting Layer"
        Logic_Func("Logic Functions"):::util
        Utils_Gen("General Utilities"):::util
        Scripts_Gen("Scripts for API/Store Generation"):::scripts
    end

    subgraph "Development & Testing Layer"
        Mock_Data("Mock Data"):::testing
        Test_E2E("End-to-End Tests"):::testing
    end

    %% Connections
    Browser -->|"loads"| NLayout
    Browser -->|"loads"| NPage
    Browser -->|"loads"| NStyling

    NLayout -->|"assembles"| UI_Main
    NPage -->|"assembles"| UI_Main
    NStyling -->|"styles"| UI_Main

    UI_Main -->|"renders"| UI_Window
    UI_Main -->|"renders"| UI_Context
    UI_Main -->|"renders"| UI_Space

    UI_Window -->|"updates"| State
    UI_Context -->|"updates"| State
    UI_Space -->|"updates"| State

    UI_Main -->|"triggers"| API_Image
    UI_Main -->|"triggers"| API_Scenario
    UI_Main -->|"triggers"| API_Fal

    API_Image -->|"processes via"| SU_Image
    API_Scenario -->|"processes via"| SU_Scenario
    API_Fal -->|"delegates to"| Fal_Core
    API_Fal -->|"delegates to"| Fal_Workflows

    API_Image -->|"utilizes"| Logic_Func
    API_Scenario -->|"utilizes"| Logic_Func

    UI_Main -->|"uses"| Utils_Gen

    Scripts_Gen -->|"auto-generates"| API_Image
    Scripts_Gen -->|"auto-generates"| API_Scenario

    Test_E2E -->|"verifies"| UI_Window
    Test_E2E -->|"verifies"| UI_Context
    Test_E2E -->|"verifies"| UI_Space

    %% Click Events
    click NLayout "https://github.com/joshwrn/scribble-ai/blob/main/src/app/layout.tsx"
    click NPage "https://github.com/joshwrn/scribble-ai/blob/main/src/app/page.tsx"
    click NStyling "https://github.com/joshwrn/scribble-ai/blob/main/src/app/page.module.scss"

    click UI_Main "https://github.com/joshwrn/scribble-ai/tree/main/src/components"
    click UI_Window "https://github.com/joshwrn/scribble-ai/blob/main/src/components/Window/Window.tsx"
    click UI_Window "https://github.com/joshwrn/scribble-ai/blob/main/src/components/Window/Canvas/Canvas.tsx"
    click UI_Context "https://github.com/joshwrn/scribble-ai/tree/main/src/components/ContextMenu"
    click UI_Context "https://github.com/joshwrn/scribble-ai/tree/main/src/components/DropDownMenu"
    click UI_Space "https://github.com/joshwrn/scribble-ai/blob/main/src/components/Space/Space.tsx"

    click State "https://github.com/joshwrn/scribble-ai/tree/main/src/state"

    click API_Image "https://github.com/joshwrn/scribble-ai/blob/main/src/app/api/imageUrlToBase64/route.ts"
    click API_Scenario "https://github.com/joshwrn/scribble-ai/blob/main/src/app/api/scenario/route.ts"
    click API_Fal "https://github.com/joshwrn/scribble-ai/blob/main/src/app/api/fal/proxy/route.ts"

    click SU_Image "https://github.com/joshwrn/scribble-ai/blob/main/src/server/imageUrlToBase64/fetchImageUrlToBase64.ts"
    click SU_Scenario "https://github.com/joshwrn/scribble-ai/blob/main/src/server/scenario/fetchScenario.ts"
    click SU_Additional "https://github.com/joshwrn/scribble-ai/tree/main/src/server"

    click Fal_Core "https://github.com/joshwrn/scribble-ai/blob/main/src/fal/optimized-consistent-latency.ts"
    click Fal_Workflows "https://github.com/joshwrn/scribble-ai/tree/main/src/fal/workflows"

    click Logic_Func "https://github.com/joshwrn/scribble-ai/tree/main/src/logic"
    click Utils_Gen "https://github.com/joshwrn/scribble-ai/tree/main/src/utils"
    click Scripts_Gen "https://github.com/joshwrn/scribble-ai/tree/main/src/scripts"

    click Mock_Data "https://github.com/joshwrn/scribble-ai/tree/main/src/mock"
    click Test_E2E "https://github.com/joshwrn/scribble-ai/tree/main/tests"

    %% Styles
    classDef browser fill:#c3f7d5,stroke:#2d864d,stroke-width:2px;
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef state fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef api fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef server fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef fal fill:#e2e3e5,stroke:#41464b,stroke-width:2px;
    classDef util fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef scripts fill:#ffeeba,stroke:#856404,stroke-width:2px;
    classDef testing fill:#d6d8db,stroke:#1b1e21,stroke-width:2px;
```
