# controlcoder/sketch-velvet architecture

> Architecture diagram of the GitHub repository controlcoder/sketch-velvet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/controlcoder/sketch-velvet
- Repository: https://github.com/controlcoder/sketch-velvet

## Overview

#### Purpose and Entry Points
A full-stack collaborative drawing-board application: React client renders and edits a canvas; Node/Express server provides authentication, board persistence/sharing, and real-time board updates. Client bootstraps at `client/src/main.tsx` and routes through `client/src/routes/AppRouter.tsx`; server HTTP and socket startup are separated in `server/src/server.ts` and `server/src/app.ts`.

#### Client Application Shell and Access Control
Page-level flows cover landing, authentication, dashboard, and individual boards. Authentication state is centralized in `client/src/context/AuthContext.tsx`; route guards distinguish anonymous versus authenticated access in `client/src/components/Routes/ProtectedRoute.tsx` and `client/src/components/Routes/PublicRoute.tsx`. API clients are partitioned by concern in `client/src/api/auth.api.ts` and `client/src/api/board.api.ts`, sharing Axios setup in `client/src/api/axios.ts`.

#### Board Workspace and Canvas Rendering
`client/src/pages/Board/Board.tsx` composes the interactive workspace: canvas, toolbar, history, zoom, persistence, and collaboration hooks. The rendering boundary is `client/src/components/Canvas/Canvas.tsx`, with camera coordinate management in `client/src/components/Canvas/Camera.ts` and drawing orchestration in `client/src/components/Canvas/Renderer.ts`. Canvas elements are client-side drawing-model objects rather than DOM shapes.

#### Editing and Drawing Pipeline
Pointer/tool actions create or incrementally update drawable elements through `client/src/tools/createElement.ts` and `client/src/tools/updateDrawingElement.ts`, with tool implementations such as `client/src/tools/drawRectangle.ts` and `client/src/tools/drawPencil.ts`. Selection, hit testing, and resize behavior form a distinct manipulation layer under `client/src/editing/selection/hitTest.ts`, `client/src/editing/resize/resizeElement.ts`, and `client/src/editing/move/moveElement.ts`. This layer feeds the renderer and board state, while `client/src/components/TextEditor/TextEditor.tsx` handles text-specific input.

#### State History, Persistence, and Collaboration
Board changes are tracked for undo/redo by `client/src/hooks/useHistory.ts` and propagated via `client/src/hooks/historyChange.ts`. `client/src/hooks/useAutosave.ts` and `client/src/hooks/useSavedElements.ts` bridge in-memory elements to persisted board state. `client/src/hooks/useBoardSocket.ts`, backed by `client/src/config/socket.ts`, is the real-time synchronization boundary; it connects board editing events to the server socket namespace/handlers.

#### Server HTTP, Authorization, and Domain Services
Express routes divide auth and board operations in `server/src/routes/auth.routes.ts` and `server/src/routes/board.routes.ts`. Controllers are thin HTTP adapters (`server/src/controllers/auth.controller.ts`, `server/src/controllers/board.controller.ts`), delegating domain work to `server/src/services/auth.service.ts`, `server/src/services/board.service.ts`, and `server/src/services/permission.service.ts`. JWT authentication is enforced at the middleware boundary in `server/src/middleware/auth.middleware.ts`; centralized error handling is in `server/src/middleware/error.middleware.ts`.

#### Realtime and Data Infrastructure
Socket server setup and board-event handling reside in `server/src/sockets/server.ts` and `server/src/sockets/board.socket.ts`, with socket middleware/error adapters protecting the realtime boundary. Prisma is the persistence runtime, configured by `server/src/config/prisma.ts` and modeled in `server/prisma/schema.prisma`; migrations establish users, boards, memberships, and viewer permissions under `server/prisma/migrations/`. The material external interfaces are the browser Canvas API, HTTP/JSON API, Socket.IO-style realtime transport, JWTs, and the Prisma-supported database.

Main entry points: [`client/src/pages/Board/Board.tsx`](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/pages/Board/Board.tsx) (Board workspace), [`client/src/components/Canvas/Canvas.tsx`](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/components/Canvas/Canvas.tsx) (Canvas renderer), [`server/src/services/board.service.ts`](https://github.com/controlcoder/sketch-velvet/blob/main/server/src/services/board.service.ts) (Domain services), [`client/src/routes/AppRouter.tsx`](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/routes/AppRouter.tsx) (App router)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["React client"]
  node_client_bootstrap["Client bootstrap<br/>React entry<br/>[main.tsx]"]
  node_router["App router<br/>route composition<br/>[AppRouter.tsx]"]
  node_auth_context["Auth state<br/>React context<br/>[AuthContext.tsx]"]
  node_route_guards["Route guards<br/>access control<br/>[ProtectedRoute.tsx]"]
  node_api_clients["HTTP API clients<br/>Axios adapters<br/>[axios.ts]"]
  node_board_workspace["Board workspace<br/>interactive page<br/>[Board.tsx]"]
  node_canvas_renderer["Canvas renderer<br/>rendering boundary<br/>[Canvas.tsx]"]
  node_drawing_tools["Drawing tools<br/>element creation<br/>[createElement.ts]"]
  node_editing_layer["Manipulation layer<br/>selection and transform<br/>[hitTest.ts]"]
  node_text_editor["Text editor<br/>text input<br/>[TextEditor.tsx]"]
  node_history_persistence["History and autosave<br/>board state hooks<br/>[useHistory.ts]"]
  node_board_socket_client{{"Board socket client<br/>realtime sync<br/>[useBoardSocket.ts]"}}
end

subgraph group_server["Node/Express server"]
  node_http_server["HTTP startup<br/>Express bootstrap<br/>[server.ts]"]
  node_express_app["Express app<br/>HTTP application<br/>[app.ts]"]
  node_http_routes["Auth and board routes<br/>HTTP routes<br/>[board.routes.ts]"]
  node_auth_middleware["JWT middleware<br/>HTTP authorization<br/>[auth.middleware.ts]"]
  node_domain_services["Domain services<br/>auth, board, permissions<br/>[board.service.ts]"]
  node_socket_server{{"Socket board handlers<br/>realtime server<br/>[board.socket.ts]"}}
end

subgraph group_data["Persistence"]
  node_prisma[("Prisma data runtime<br/>persistence adapter<br/>[prisma.ts]")]
  node_database_schema[("Board data model<br/>Prisma schema<br/>[schema.prisma]")]
end

node_browser_canvas(("Browser Canvas API<br/>external rendering API"))

node_client_bootstrap -->|"mounts"| node_router
node_router -->|"uses"| node_auth_context
node_router -->|"enforces access"| node_route_guards
node_route_guards -->|"admits authenticated users"| node_board_workspace
node_auth_context -->|"auth requests"| node_api_clients
node_board_workspace -->|"renders board"| node_canvas_renderer
node_board_workspace -->|"dispatches tools"| node_drawing_tools
node_board_workspace -->|"selects and transforms"| node_editing_layer
node_board_workspace -->|"edits text"| node_text_editor
node_board_workspace -->|"tracks and saves changes"| node_history_persistence
node_board_workspace -->|"synchronizes edits"| node_board_socket_client
node_drawing_tools -->|"updates elements"| node_canvas_renderer
node_editing_layer -->|"updates elements"| node_canvas_renderer
node_canvas_renderer -->|"draws to"| node_browser_canvas
node_history_persistence -->|"persists boards"| node_api_clients
node_api_clients -->|"HTTP/JSON"| node_http_server
node_http_server -->|"starts"| node_express_app
node_express_app -->|"mounts"| node_http_routes
node_http_routes -->|"protects board operations"| node_auth_middleware
node_auth_middleware -->|"authorizes requests"| node_domain_services
node_http_routes -->|"delegates controllers"| node_domain_services
node_domain_services -->|"reads and writes"| node_prisma
node_prisma -->|"uses model"| node_database_schema
node_board_socket_client -->|"realtime board events"| node_socket_server
node_socket_server -->|"authorizes and persists events"| node_domain_services

click node_client_bootstrap "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/main.tsx"
click node_router "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/routes/AppRouter.tsx"
click node_auth_context "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/context/AuthContext.tsx"
click node_route_guards "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/components/Routes/ProtectedRoute.tsx"
click node_api_clients "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/api/axios.ts"
click node_board_workspace "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/pages/Board/Board.tsx"
click node_canvas_renderer "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/components/Canvas/Canvas.tsx"
click node_drawing_tools "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/tools/createElement.ts"
click node_editing_layer "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/editing/selection/hitTest.ts"
click node_text_editor "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/components/TextEditor/TextEditor.tsx"
click node_history_persistence "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/hooks/useHistory.ts"
click node_board_socket_client "https://github.com/controlcoder/sketch-velvet/blob/main/client/src/hooks/useBoardSocket.ts"
click node_http_server "https://github.com/controlcoder/sketch-velvet/blob/main/server/src/server.ts"
click node_express_app "https://github.com/controlcoder/sketch-velvet/blob/main/server/src/app.ts"
click node_http_routes "https://github.com/controlcoder/sketch-velvet/blob/main/server/src/routes/board.routes.ts"
click node_auth_middleware "https://github.com/controlcoder/sketch-velvet/blob/main/server/src/middleware/auth.middleware.ts"
click node_domain_services "https://github.com/controlcoder/sketch-velvet/blob/main/server/src/services/board.service.ts"
click node_socket_server "https://github.com/controlcoder/sketch-velvet/blob/main/server/src/sockets/board.socket.ts"
click node_prisma "https://github.com/controlcoder/sketch-velvet/blob/main/server/src/config/prisma.ts"
click node_database_schema "https://github.com/controlcoder/sketch-velvet/blob/main/server/prisma/schema.prisma"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_client_bootstrap,node_router,node_auth_context,node_route_guards,node_api_clients,node_board_workspace,node_canvas_renderer,node_drawing_tools,node_editing_layer,node_text_editor,node_history_persistence,node_board_socket_client toneBlue
class node_http_server,node_express_app,node_http_routes,node_auth_middleware,node_domain_services,node_socket_server toneAmber
class node_prisma,node_database_schema toneMint
class node_browser_canvas toneNeutral
```

## Components

### React client

- [Client bootstrap](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/main.tsx): React entry · `client/src/main.tsx`
- [App router](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/routes/AppRouter.tsx): route composition · `client/src/routes/AppRouter.tsx`
- [Auth state](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/context/AuthContext.tsx): React context · `client/src/context/AuthContext.tsx`
- [Route guards](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/components/Routes/ProtectedRoute.tsx): access control · `client/src/components/Routes/ProtectedRoute.tsx`
- [HTTP API clients](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/api/axios.ts): Axios adapters · Auth and board clients share Axios setup. · `client/src/api/axios.ts`
- [Board workspace](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/pages/Board/Board.tsx): interactive page · Composes editing, history, persistence, and collaboration. · `client/src/pages/Board/Board.tsx`
- [Canvas renderer](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/components/Canvas/Canvas.tsx): rendering boundary · Renders drawing-model elements through camera and renderer layers. · `client/src/components/Canvas/Canvas.tsx`
- [Drawing tools](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/tools/createElement.ts): element creation · Creates and incrementally updates drawable elements. · `client/src/tools/createElement.ts`
- [Manipulation layer](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/editing/selection/hitTest.ts): selection and transform · Handles hit testing, resize, and movement. · `client/src/editing/selection/hitTest.ts`
- [Text editor](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/components/TextEditor/TextEditor.tsx): text input · `client/src/components/TextEditor/TextEditor.tsx`
- [History and autosave](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/hooks/useHistory.ts): board state hooks · Tracks undo/redo and saves in-memory elements. · `client/src/hooks/useHistory.ts`
- [Board socket client](https://github.com/controlcoder/sketch-velvet/blob/main/client/src/hooks/useBoardSocket.ts): realtime sync · `client/src/hooks/useBoardSocket.ts`

### Node/Express server

- [HTTP startup](https://github.com/controlcoder/sketch-velvet/blob/main/server/src/server.ts): Express bootstrap · Separates HTTP application construction from server startup. · `server/src/server.ts`
- [Express app](https://github.com/controlcoder/sketch-velvet/blob/main/server/src/app.ts): HTTP application · `server/src/app.ts`
- [Auth and board routes](https://github.com/controlcoder/sketch-velvet/blob/main/server/src/routes/board.routes.ts): HTTP routes · Routes dispatch to thin auth and board controllers. · `server/src/routes/board.routes.ts`
- [JWT middleware](https://github.com/controlcoder/sketch-velvet/blob/main/server/src/middleware/auth.middleware.ts): HTTP authorization · `server/src/middleware/auth.middleware.ts`
- [Domain services](https://github.com/controlcoder/sketch-velvet/blob/main/server/src/services/board.service.ts): auth, board, permissions · Owns authentication, board operations, and sharing permissions. · `server/src/services/board.service.ts`
- [Socket board handlers](https://github.com/controlcoder/sketch-velvet/blob/main/server/src/sockets/board.socket.ts): realtime server · Socket setup and board event handling boundary. · `server/src/sockets/board.socket.ts`

### Persistence

- [Prisma data runtime](https://github.com/controlcoder/sketch-velvet/blob/main/server/src/config/prisma.ts): persistence adapter · `server/src/config/prisma.ts`
- [Board data model](https://github.com/controlcoder/sketch-velvet/blob/main/server/prisma/schema.prisma): Prisma schema · Models users, boards, memberships, and viewer permissions. · `server/prisma/schema.prisma`

### Other components

- **Browser Canvas API**: external rendering API

## Connections

- Client bootstrap → App router: mounts
- App router → Auth state: uses
- App router → Route guards: enforces access
- Route guards → Board workspace: admits authenticated users
- Auth state → HTTP API clients: auth requests
- Board workspace → Canvas renderer: renders board
- Board workspace → Drawing tools: dispatches tools
- Board workspace → Manipulation layer: selects and transforms
- Board workspace → Text editor: edits text
- Board workspace → History and autosave: tracks and saves changes
- Board workspace → Board socket client: synchronizes edits
- Drawing tools → Canvas renderer: updates elements
- Manipulation layer → Canvas renderer: updates elements
- Canvas renderer → Browser Canvas API: draws to
- History and autosave → HTTP API clients: persists boards
- HTTP API clients → HTTP startup: HTTP/JSON
- HTTP startup → Express app: starts
- Express app → Auth and board routes: mounts
- Auth and board routes → JWT middleware: protects board operations
- JWT middleware → Domain services: authorizes requests
- Auth and board routes → Domain services: delegates controllers
- Domain services → Prisma data runtime: reads and writes
- Prisma data runtime → Board data model: uses model
- Board socket client → Socket board handlers: realtime board events
- Socket board handlers → Domain services: authorizes and persists events
