# mirayatech/ninjasketch architecture

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

- Interactive diagram: https://gitdiagram.com/mirayatech/ninjasketch
- Repository: https://github.com/mirayatech/ninjasketch

## Overview

##### Main Components and Layers
- User Interface Layer:
- The React components found under src/components (action-bar, control-panel, info) form the primary visual and interactive elements. These components provide the tools (pencil, line, rectangle, text) and UI elements for users to interact with.
- The main canvas area is rendered in src/App.tsx (and indirectly in other components) and is where drawing elements appear.
- Business Logic / State Management:
- The custom hooks (useHistory and usePressedKeys in src/hooks) are responsible for state management, including modification history (undo/redo) and handling keyboard events.
- External Libraries and Frameworks:
- Rough.js is integrated for achieving the sketchy, hand-drawn rendering style.
- Vite (see vite.config.ts) is used as the build tool, and the project leverages TypeScript for type safety.
- Testing frameworks like Cypress (structure under cypress directory) and Vitest or Testing Library are included for end-to-end and unit testing.

##### Relationships and Interactions
- User Interactions (via mouse, keyboard, and scroll events) are captured by the UI components and forwarded to the state management hooks. The useHistory hook, for instance, helps manage the drawing’s state for undo/redo capabilities.
- The drawing tools (action-bar/control-panel selections) modify the application state, which then triggers updates in the canvas rendering.
- Utility functions process the adjustments such as calculating coordinates, detecting proximity to points, and resizing logic. They act as the computational intermediary between raw user interaction data and the rendered output.

##### Architectural Patterns & Design Principles
- Component-Based Architecture: The clear separation into React components facilitates isolation of UI concerns. Grouping of related components (action-bar, control-panel, info) suggests a modular design.
- Separation of Concerns: UI rendering, state management (via hooks), and drawing logic (through utilities) are distinct, thereby making the system easier to reason about and maintain.
- Layered Structure: Your design distinguishes between presentation (React components), business logic (hooks and utilities), and integration with external libraries (Rough.js, Vite) while testing is isolated (Cypress).
- Emphasis on Event-Driven Interaction: The usage of custom hooks for history and pressed keys underline an event-driven approach to handling user interactions.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global User
    User("User"):::external

    %% User Interface Layer
    subgraph "User Interface Layer"
        Canvas("Canvas"):::frontend
        ActionBar("Action Bar"):::frontend
        ControlPanel("Control Panel"):::frontend
        InfoPanel("Info Panel"):::frontend
    end

    %% State Management Layer
    subgraph "State Management"
        useHistory("useHistory"):::logic
        usePressedKeys("usePressedKeys"):::logic
    end

    %% Drawing Utilities Layer
    subgraph "Drawing Utilities"
        CreateElem("create-element"):::engine
        AdjustCoords("adjust-element-coordinates"):::engine
        DrawElem("draw-element"):::engine
        NearPoint("Near Point Calculations"):::engine
        ResizedCoords("Resized Coordinates Calculations"):::engine
    end

    %% External Services
    subgraph "External Services"
        Rough("Rough.js"):::external
        Vite("Vite Build Tool"):::external
    end

    %% Testing Environment
    Cypress("Cypress Tests"):::testing

    %% Relationships and Data Flow
    User -->|"initiatesEvent"| ActionBar
    User -->|"initiatesEvent"| ControlPanel
    User -->|"initiatesEvent"| Canvas
    User -->|"initiatesEvent"| InfoPanel

    %% UI components forward user interactions to state management hooks
    Canvas -->|"sendEvent"| useHistory
    ActionBar -->|"changeState"| useHistory
    ControlPanel -->|"keyboardEvent"| usePressedKeys
    InfoPanel -->|"infoUpdate"| useHistory

    %% State management sends updates to drawing utilities
    useHistory -->|"processDrawing"| CreateElem
    useHistory -->|"processDrawing"| AdjustCoords
    useHistory -->|"processDrawing"| DrawElem
    usePressedKeys -->|"handleKey"| NearPoint

    %% Internal utilities interactions
    AdjustCoords -->|"calcAdjusted"| ResizedCoords

    %% Drawing utilities render output via Rough.js
    DrawElem -->|"renderPath"| Rough

    %% External Build Tool affects the app (indirect link to UI Canvas)
    Vite -->|"builds"| Canvas

    %% Testing interactions
    Cypress -->|"testsUI"| Canvas
    Cypress -->|"testsUI"| ActionBar
    Cypress -->|"testsState"| useHistory
    Cypress -->|"testsUtils"| DrawElem

    %% Click Events for Component Mapping
    click Canvas "https://github.com/mirayatech/ninjasketch/blob/main/src/App.tsx"
    click ActionBar "https://github.com/mirayatech/ninjasketch/blob/main/src/components/action-bar/action-bar.tsx"
    click ControlPanel "https://github.com/mirayatech/ninjasketch/blob/main/src/components/control-panel/control-panel.tsx"
    click InfoPanel "https://github.com/mirayatech/ninjasketch/blob/main/src/components/info/info.tsx"
    click useHistory "https://github.com/mirayatech/ninjasketch/blob/main/src/hooks/useHistory.ts"
    click usePressedKeys "https://github.com/mirayatech/ninjasketch/blob/main/src/hooks/usePressedKeys.ts"
    click CreateElem "https://github.com/mirayatech/ninjasketch/blob/main/src/utilities/create-element.ts"
    click AdjustCoords "https://github.com/mirayatech/ninjasketch/blob/main/src/utilities/adjust-element-coordinates.ts"
    click DrawElem "https://github.com/mirayatech/ninjasketch/blob/main/src/utilities/draw-element.ts"
    click NearPoint "https://github.com/mirayatech/ninjasketch/tree/main/src/utilities/near-point"
    click ResizedCoords "https://github.com/mirayatech/ninjasketch/tree/main/src/utilities/resized-coordinates"
    click Vite "https://github.com/mirayatech/ninjasketch/blob/main/vite.config.ts"
    click Cypress "https://github.com/mirayatech/ninjasketch/tree/main/cypress"

    %% Styles
    classDef frontend fill:#f9f,stroke:#333,stroke-width:2px;
    classDef logic fill:#ff9,stroke:#333,stroke-width:2px;
    classDef engine fill:#9f9,stroke:#333,stroke-width:2px;
    classDef external fill:#9ff,stroke:#333,stroke-width:2px;
    classDef testing fill:#f99,stroke:#333,stroke-width:2px;
```
