# makaveli2p/sketchsync architecture

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

- Interactive diagram: https://gitdiagram.com/makaveli2p/sketchsync
- Repository: https://github.com/makaveli2p/sketchsync

## Overview

##### Project Type and Purpose
- This is a full‑stack, real‑time collaborative drawing application built with Next.js (React + SSR), Node.js (WebSocket server), and TypeScript.

##### File Structure
Top‑level areas:
- pages/ – Next.js pages (SSG/SSR). Contains the room page (`[roomId].tsx`), static index page, API route (`pages/api/hello.ts`).
- modules/ – Feature modules for “home” and “room.” Each module encapsulates React components, hooks, context, helpers, animations, and modals.
- common/ – Shared UI components (modals, portals), shared hooks, Recoil atoms & hooks (global state: background, modal, room, users, savedMoves, options), utility libs, constants, types.
- server/ – Standalone Node.js WebSocket (socket.io or ws) server to mediate real‑time drawing and chat events.
- public/, styles/, tailwind.config.ts – Static assets and styling.
- next.config.mjs, tsconfig*.json, package.json – Build/deploy configurations.

##### Main Components
A. Frontend (Next.js + React)
1. Next.js Server / CDN – serves pages, static assets, SSR.
2. React App –
- Home Module (landing page, room creation/join UI)
- Common Components & Recoil State – modal manager, portals, global state atoms/hooks
B. Backend
1. Next.js API (optional REST endpoint: hello)
2. WebSocket Server (server/index.ts) – receives drawing/chat events, broadcasts to room participants
C. Data/State
- In‑memory state in server (per‑room user list, canvas history?)
- Client‑side Recoil atoms for UI state, drawing moves, users
D. External Services (if any)
- None specified (no persistent DB, uses ephemeral in‑memory).

##### Relationships & Data Flows
- Browser ↔️ Next.js Server: initial page load, SSR, static assets.
- React Components ↔️ Recoil Store: UI reads/writes global state.
- React Hooks (useSocketDraw, useMovesHandlers) ↔️ Socket library wrapper (common/lib/socket.ts).
- WebSocket Server ↔️ All connected clients in the same “room” channel.

##### Architectural Patterns & Design Principles
- Module‑based frontend: each feature (home, room) self‑contained with its own components/hooks/context.
- Shared common library: DRY UI primitives, shared state wrappers, helpers.
- State management: Recoil for client‑side global state, separating atoms by domain.
- Real‑time pub/sub via WebSocket server.
- Next.js conventions: pages → automatic routing; API routes for light REST; server as separate real‑time service.

##### Key Technologies
- Next.js (React, SSR, routing)
- React + TypeScript + Tailwind CSS
- Recoil (state management)
- Node.js WebSocket server (socket.io or ws)
- PostCSS (styling), ESLint, tsconfig, package scripts

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Browser
    Browser["Browser"]:::external

    %% Presentation Layer
    subgraph "Presentation Layer"
        NextServer["Next.js Server / CDN\n(Node + SSR)"]:::backend

        subgraph "Next.js Pages"
            Index["index.tsx"]:::frontend
            RoomPage["[roomId].tsx"]:::frontend
            AppPage["_app.tsx"]:::frontend
            DocumentPage["_document.tsx"]:::frontend
        end

        subgraph "React App"
            subgraph "Home Module"
                HomeComp["Home.tsx\n(landing/join UI)"]:::frontend
                NotFound["NotFound.tsx"]:::frontend
            end
            subgraph "Room Module"
                RoomComp["Room.tsx\n(canvas, chat, toolbar)"]:::frontend
                NameInput["NameInput.tsx"]:::frontend
                RoomContext["Room.context.tsx"]:::frontend
                CanvasHelpers["Canvas.helpers.ts"]:::frontend
                UseSocketDraw["useSocketDraw.ts"]:::frontend
                UseMovesHandlers["useMovesHandlers.ts"]:::frontend
                BackgroundModal["BackgroundModal.tsx"]:::frontend
                ShareModal["ShareModal.tsx"]:::frontend
                subgraph "Room Components"
                    CanvasComp["Canvas.tsx"]:::frontend
                    ChatComp["Chat.tsx"]:::frontend
                    ToolBar["ToolBar.tsx"]:::frontend
                end
            end
            subgraph "Common Layer"
                ModalManager["ModalManager.tsx"]:::common
                ModalAnim["ModalManager.animations.ts"]:::common
                Portal["Portal.tsx"]:::common
                subgraph "Recoil Store"
                    BGAtom["background.atom.ts"]:::state
                    ModalAtom["modal.atom.tsx"]:::state
                    OptionsAtom["options.atom.ts"]:::state
                    RoomAtom["room.atom.ts"]:::state
                    UsersAtom["users.atoms.ts"]:::state
                    SavedMovesAtom["savedMoves.atom.ts"]:::state
                end
                subgraph "Shared Hooks & Utils"
                    ViewportHook["useViewportSize.ts"]:::common
                    SocketLib["socket.ts"]:::common
                    NextColor["getNextColor.ts"]:::common
                    OptimizeImage["optimizeImage.ts"]:::common
                end
                Constants["colors.ts\ncanvasSize.ts"]:::common
                Types["global.d.ts"]:::common
            end
        end
    end

    %% API & Real-Time Layer
    subgraph "API & Real-Time Layer"
        API["Next.js API Route\n(pages/api/hello.ts)"]:::api
        WebSocket["WebSocket Server\n(server/index.ts)\n(pub/sub hub)"]:::backend
    end

    %% Data Flows
    Browser -->|"HTTP GET"| NextServer
    NextServer -->|"Serves Pages"| Index
    NextServer --> RoomPage
    Browser -->|"HTTP API"| API
    Browser -->|"WS connect"| WebSocket
    UseSocketDraw -->|"uses"| SocketLib
    UseMovesHandlers -->|"uses"| SocketLib
    SocketLib -->|"WS"| WebSocket
    WebSocket -->|"WS broadcast"| SocketLib
    SocketLib --> RoomComp
    HomeComp -->|"updates"| ModalManager
    RoomComp -->|"reads/writes"| BGAtom
    RoomComp --> ModalAtom
    RoomComp --> OptionsAtom
    RoomComp --> RoomAtom
    RoomComp --> UsersAtom
    RoomComp --> SavedMovesAtom

    %% React ↔ Recoil
    subgraph "Recoil Interaction"
        RoomComp --> BGAtom
        RoomComp --> ModalAtom
        RoomComp --> OptionsAtom
        RoomComp --> RoomAtom
        RoomComp --> UsersAtom
        RoomComp --> SavedMovesAtom
    end

    %% Click Events
    click Index "https://github.com/makaveli2p/sketchsync/blob/main/pages/index.tsx"
    click RoomPage "https://github.com/makaveli2p/sketchsync/blob/main/pages/[roomId].tsx"
    click AppPage "https://github.com/makaveli2p/sketchsync/blob/main/pages/_app.tsx"
    click DocumentPage "https://github.com/makaveli2p/sketchsync/blob/main/pages/_document.tsx"
    click API "https://github.com/makaveli2p/sketchsync/blob/main/pages/api/hello.ts"
    click WebSocket "https://github.com/makaveli2p/sketchsync/blob/main/server/index.ts"
    click HomeComp "https://github.com/makaveli2p/sketchsync/blob/main/modules/home/components/Home.tsx"
    click NotFound "https://github.com/makaveli2p/sketchsync/blob/main/modules/home/modals/NotFound.tsx"
    click RoomComp "https://github.com/makaveli2p/sketchsync/blob/main/modules/room/components/Room.tsx"
    click NameInput "https://github.com/makaveli2p/sketchsync/blob/main/modules/room/components/NameInput.tsx"
    click RoomContext "https://github.com/makaveli2p/sketchsync/blob/main/modules/room/context/Room.context.tsx"
    click CanvasHelpers "https://github.com/makaveli2p/sketchsync/blob/main/modules/room/helpers/Canvas.helpers.ts"
    click UseSocketDraw "https://github.com/makaveli2p/sketchsync/blob/main/modules/room/hooks/useSocketDraw.ts"
    click UseMovesHandlers "https://github.com/makaveli2p/sketchsync/blob/main/modules/room/hooks/useMovesHandlers.ts"
    click BackgroundModal "https://github.com/makaveli2p/sketchsync/blob/main/modules/room/modals/BackgroundModal.tsx"
    click ShareModal "https://github.com/makaveli2p/sketchsync/blob/main/modules/room/modals/ShareModal.tsx"
    click CanvasComp "https://github.com/makaveli2p/sketchsync/blob/main/modules/room/components/board/Canvas.tsx"
    click ChatComp "https://github.com/makaveli2p/sketchsync/blob/main/modules/room/components/chat/Chat.tsx"
    click ToolBar "https://github.com/makaveli2p/sketchsync/blob/main/modules/room/components/toolbar/ToolBar.tsx"
    click ModalManager "https://github.com/makaveli2p/sketchsync/blob/main/common/components/modal/components/ModalManager.tsx"
    click ModalAnim "https://github.com/makaveli2p/sketchsync/blob/main/common/components/modal/animations/ModalManager.animations.ts"
    click Portal "https://github.com/makaveli2p/sketchsync/blob/main/common/components/portal/components/Portal.tsx"
    click BGAtom "https://github.com/makaveli2p/sketchsync/blob/main/common/recoil/background/background.atom.ts"
    click ModalAtom "https://github.com/makaveli2p/sketchsync/blob/main/common/recoil/modal/modal.atom.tsx"
    click OptionsAtom "https://github.com/makaveli2p/sketchsync/blob/main/common/recoil/options/options.atom.ts"
    click RoomAtom "https://github.com/makaveli2p/sketchsync/blob/main/common/recoil/room/room.atom.ts"
    click UsersAtom "https://github.com/makaveli2p/sketchsync/blob/main/common/recoil/users/users.atoms.ts"
    click SavedMovesAtom "https://github.com/makaveli2p/sketchsync/blob/main/common/recoil/savedMoves/savedMoves.atom.ts"
    click ViewportHook "https://github.com/makaveli2p/sketchsync/blob/main/common/hooks/useViewportSize.ts"
    click SocketLib "https://github.com/makaveli2p/sketchsync/blob/main/common/lib/socket.ts"
    click NextColor "https://github.com/makaveli2p/sketchsync/blob/main/common/lib/getNextColor.ts"
    click OptimizeImage "https://github.com/makaveli2p/sketchsync/blob/main/common/lib/optimizeImage.ts"

    %% Styles
    classDef frontend fill:#8ad,stroke:#000
    classDef common fill:#add8e6,stroke:#000
    classDef backend fill:#f96,stroke:#000
    classDef api fill:#fa0,stroke:#000
    classDef state fill:#9f9,stroke:#000
    classDef external fill:#ddd,stroke:#000
```
