# adityayaduvanshi/platejs-template architecture

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

- Interactive diagram: https://gitdiagram.com/adityayaduvanshi/platejs-template
- Repository: https://github.com/adityayaduvanshi/platejs-template

## Overview

##### Project Type and Purpose
- This is a full-stack Next.js 15 “App Directory” template that provides a rich-text AI-powered editor using the Plate framework, shadcn/ui components, and integrates with OpenAI and UploadThing.
- The user-facing editor lives at `/editor` (src/app/editor/page.tsx) and communicates with serverless API routes under `src/app/api` to perform AI completions and file uploads.

##### Main Components
a. Frontend (Browser / Client)
- Next.js App Router pages (src/app/layout.tsx, page.tsx, editor/page.tsx)
- React component tree:
– Core Plate editor (`components/editor/*`)
– Plugins (`components/editor/plugins/*`)
– UI primitives (`components/ui/*`)
- Custom hooks (`src/hooks/*`) and utility libs (`src/lib/*`)

b. Serverless API Layer (Next.js Route Handlers)
- `/api/ai/command` – handles custom AI commands
- `/api/ai/copilot` – handles Copilot-style completions
- `/api/uploadthing` – proxies file uploads via UploadThing

c. External Services
- OpenAI API (for AI completions)
- UploadThing (for file storage)

##### Relationships and Data Flow
1. User navigates to `/editor` in browser.
2. Next.js SSR/SSG serves HTML and hydration scripts.
3. The Plate editor component initializes with plugins and UI.
4. On user actions (e.g. invoke AI, upload media), the front end issues fetch() calls to `/api/ai/*` or `/api/uploadthing`.
5. API route handlers validate the request, read `OPENAI_API_KEY` or `UPLOADTHING_TOKEN`, call the external service, and return JSON or file URLs.
6. Front end updates editor state/UI based on API responses.

##### Architectural Patterns & Design Principles
- File-based routing via Next.js App Directory – clear separation of pages vs. API.
- Serverless functions (Route Handlers in `route.ts`) isolate backend logic.
- Plugin architecture in Plate – each editor feature is a self-contained module under `components/editor/plugins`.
- Componentize UI with shadcn/ui – low-level primitives (`components/ui`) ensure consistency.
- Utility layers (`hooks`, `lib`) encapsulate cross-cutting concerns (debouncing, transforms, upload helpers).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Tier"
        subgraph "Next.js App"
            Layout["/src/app/layout.tsx - Root Layout"]:::frontend
            Home["/src/app/page.tsx - Home Page"]:::frontend
            EditorPage["/src/app/editor/page.tsx - Editor Page"]:::frontend
        end
        subgraph "Plate Editor"
            PlateCore["Plate Editor Core"]:::frontend
            BaseKit["Plate Base Kit"]:::frontend
            FullKit["Plate Full Kit"]:::frontend
            Types["Plate Types"]:::frontend
            ChatHook["Chat Hook (use-chat)"]:::frontend
            Transforms["Editor Transforms"]:::frontend
            Settings["Settings Dialog"]:::frontend
            subgraph "Plugins"
                AIPlugin["AI Plugin"]:::frontend
                CopilotPlugin["Copilot Plugin"]:::frontend
                BasicBlocks["Basic Blocks Plugins"]:::frontend
                MediaPlugins["Media Plugins"]:::frontend
            end
        end
        subgraph "UI Components"
            UIPRIM["UI Primitives Library"]:::frontend
        end
        subgraph "Hooks & Utils"
            Debounce["Debounce Hook"]:::frontend
            UploadHook["Upload File Hook"]:::frontend
            TouchHook["Touch Device Hook"]:::frontend
            Mounted["Mounted State Hook"]:::frontend
            MDTransform["Markdown Joiner"]:::frontend
            UploadClient["UploadThing Client"]:::frontend
            Utils["Misc Utils"]:::frontend
        end
    end

    subgraph "Server Tier"
        subgraph "AI API"
            AICommand["/src/app/api/ai/command - AI Command Handler"]:::backend
            AICopilot["/src/app/api/ai/copilot - AI Copilot Handler"]:::backend
        end
        UploadAPI["/src/app/api/uploadthing - UploadThing Proxy"]:::backend
    end

    subgraph "External Services"
        OpenAI["OpenAI API"]:::external
        UploadThing["UploadThing Service"]:::external
    end

    %% Data Flow
    Layout -->|serves| Home
    Layout -->|serves| EditorPage
    EditorPage -->|initializes| PlateCore
    PlateCore --> BaseKit
    PlateCore --> FullKit
    PlateCore --> Types
    PlateCore --> ChatHook
    PlateCore --> Transforms
    PlateCore --> Settings
    PlateCore --> AIPlugin
    PlateCore --> CopilotPlugin
    PlateCore --> BasicBlocks
    PlateCore --> MediaPlugins
    PlateCore --> UIPRIM
    PlateCore --> Debounce
    PlateCore --> UploadHook
    PlateCore --> TouchHook
    PlateCore --> Mounted
    PlateCore --> MDTransform
    PlateCore --> UploadClient
    PlateCore --> Utils

    ChatHook -->|fetch| AICommand
    ChatHook -->|fetch| AICopilot
    UploadHook -->|fetch| UploadAPI

    AICommand -->|calls| OpenAI
    AICopilot -->|calls| OpenAI
    UploadAPI -->|calls| UploadThing

    OpenAI -->|response| AICommand
    OpenAI -->|response| AICopilot
    UploadThing -->|response| UploadAPI

    AICommand -->|respond| ChatHook
    AICopilot -->|respond| ChatHook
    UploadAPI -->|respond| UploadHook

    %% Click Events
    click Layout "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/app/layout.tsx"
    click Home "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/app/page.tsx"
    click EditorPage "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/app/editor/page.tsx"
    click PlateCore "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/components/editor/plate-editor.tsx"
    click BaseKit "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/components/editor/editor-base-kit.tsx"
    click FullKit "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/components/editor/editor-kit.tsx"
    click Types "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/components/editor/plate-types.ts"
    click ChatHook "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/components/editor/use-chat.ts"
    click Transforms "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/components/editor/transforms.ts"
    click Settings "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/components/editor/settings-dialog.tsx"
    click AIPlugin "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/components/editor/plugins/ai-kit.tsx"
    click CopilotPlugin "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/components/editor/plugins/copilot-kit.tsx"
    click BasicBlocks "https://github.com/adityayaduvanshi/platejs-template/tree/main/src/components/editor/plugins"
    click MediaPlugins "https://github.com/adityayaduvanshi/platejs-template/tree/main/src/components/editor/plugins"
    click UIPRIM "https://github.com/adityayaduvanshi/platejs-template/tree/main/src/components/ui"
    click Debounce "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/hooks/use-debounce.ts"
    click UploadHook "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/hooks/use-upload-file.ts"
    click TouchHook "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/hooks/use-is-touch-device.ts"
    click Mounted "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/hooks/use-mounted.ts"
    click MDTransform "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/lib/markdown-joiner-transform.ts"
    click UploadClient "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/lib/uploadthing.ts"
    click Utils "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/lib/utils.ts"
    click AICommand "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/app/api/ai/command/route.ts"
    click AICopilot "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/app/api/ai/copilot/route.ts"
    click UploadAPI "https://github.com/adityayaduvanshi/platejs-template/blob/main/src/app/api/uploadthing/route.ts"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366D6,color:#000;
    classDef backend fill:#DFFFE0,stroke:#228B22,color:#000;
    classDef external fill:#F0F0F0,stroke:#666,color:#000;
```
