# monapdx/storyplay architecture

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

- Interactive diagram: https://gitdiagram.com/monapdx/storyplay
- Repository: https://github.com/monapdx/storyplay

## Overview

#### Purpose and Entry Points
StoryPlay is a client-side visual authoring tool for node-based interactive stories, chats, timed branches, and mini-games. React/Vite bootstraps at `src/main.tsx`; `src/App.tsx` selects editor, player (`#/play`), and documentation (`#/docs`) surfaces through hash routing (`src/hooks/useHashRoute.ts`).

#### Canonical Story Model
The editor’s project state is a story containing nodes, variables, and characters. Core TypeScript contracts live in `src/types/story.ts`, `src/types/storyCore.ts`, and `src/types/storyBlocks.ts`. Node-to-node links are implied by choice target IDs rather than independently persisted edges; graph conversion and relationship logic reside in `src/utils/nodeGraphLinks.ts` and `src/utils/nodeHelpers.ts`.

#### Editor State and Persistence
`src/hooks/useStoryState.ts` is the central editor-state boundary: it coordinates mutations, undo/history, project loading, and persistence. Browser-local project snapshots are handled by `src/utils/storyProjectStorage.ts`; undo/redo history by `src/utils/storyUndoHistory.ts`. The canvas and inspectors consume this state rather than owning canonical story data.

#### Visual Graph Authoring
The React Flow canvas renders and edits the node graph: `src/components/canvas/StoryCanvas.tsx`, `src/components/canvas/StoryNode.tsx`, and `src/components/canvas/StoryEdge.tsx`. Canvas connection events become choice targets in the story model. The sidebar edits selected-node content and choice metadata through `src/components/editor/SidebarEditor.tsx`, `src/components/editor/ChoicesEditor.tsx`, and condition/effect editors.

#### Runtime Preview and Player
Interactive execution is isolated from editing in `src/hooks/usePlayState.ts`, with block/choice behavior supported by `src/utils/storyLogic.ts`, `src/utils/chatPlay.ts`, and `src/utils/playEntryNode.ts`. `src/components/preview/StoryPreview.tsx` provides embedded quick preview; `src/components/player/PlayerPage.tsx` is the standalone hash-route player. Player state applies choice and timed effects, resolves permitted paths, and supports chat-specific interaction.

#### Specialized Content Systems
Characters and variables are story-wide entities managed in `src/components/entities/CharacterManager.tsx` and `src/components/editor/VariablesScreen.tsx`; token/entity resolution is centralized in `src/utils/storyEntities.ts` and `src/utils/storyVariables.ts`. Mini-game nodes have dedicated editor and preview shells (`src/components/minigame/MiniGameEditorShell.tsx`, `src/components/minigame/MiniGamePreview.tsx`) and typed definitions (`src/types/minigames.ts`), while canvas block views expose their summaries.

#### Validation, Interchange, and Deployment
Diagnostics inspect graph integrity and story references through `src/components/editor/StoryDiagnostics.tsx` and `src/utils/graphHealth.ts`. Import/export forms a versioned interchange boundary: `src/utils/importStoryPlayProject.ts`, `src/utils/serializeStoryPlayExport.ts`, and `schemas/storyplay-export.v1.schema.json`. The app is a static Vite deployment; GitHub Pages deployment is configured in `.github/workflows/deploy.yml`, with optional itch.io packaging/publishing scripts in `scripts/deploy-itch.mjs`.

Main entry points: [`src/hooks/useStoryState.ts`](https://github.com/monapdx/storyplay/blob/main/src/hooks/useStoryState.ts) (Editor story state), [`src/App.tsx`](https://github.com/monapdx/storyplay/blob/main/src/App.tsx) (Route surface selector), [`src/hooks/usePlayState.ts`](https://github.com/monapdx/storyplay/blob/main/src/hooks/usePlayState.ts) (Play state), [`src/components/canvas/StoryCanvas.tsx`](https://github.com/monapdx/storyplay/blob/main/src/components/canvas/StoryCanvas.tsx) (Visual story canvas)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Application"]
  node_bootstrap["React/Vite bootstrap<br/>entry point<br/>[main.tsx]"]
  node_app["Route surface selector<br/>application shell<br/>[App.tsx]"]
  node_routing["Hash routing<br/>routing hook<br/>[useHashRoute.ts]"]
end

subgraph group_authoring["Authoring"]
  node_editor_state{{"Editor story state<br/>state boundary<br/>[useStoryState.ts]"}}
  node_storage[("Local project storage<br/>browser persistence")]
  node_undo["Undo/redo history<br/>history store"]
  node_canvas["Visual story canvas<br/>React Flow editor<br/>[StoryCanvas.tsx]"]
  node_sidebar["Node &amp; choice inspector<br/>editor panel<br/>[SidebarEditor.tsx]"]
  node_entities["Characters &amp; variables<br/>story-wide entities"]
  node_minigames["Mini-game authoring<br/>specialized editor"]
  node_diagnostics["Story diagnostics<br/>validation UI"]
end

subgraph group_model["Story model"]
  node_story_types["Story contracts<br/>TypeScript model<br/>[story.ts]"]
  node_graph_links["Story graph links<br/>graph utilities<br/>[nodeGraphLinks.ts]"]
end

subgraph group_runtime["Runtime"]
  node_play_state{{"Play state<br/>runtime state<br/>[usePlayState.ts]"}}
  node_story_logic["Execution logic<br/>runtime utilities<br/>[storyLogic.ts]"]
  node_preview["Embedded preview<br/>preview surface<br/>[StoryPreview.tsx]"]
  node_player["Standalone player<br/>player route<br/>[PlayerPage.tsx]"]
end

subgraph group_interop["Interchange &amp; delivery"]
  node_interchange["Versioned import/export<br/>interchange boundary"]
  node_deployment["Static deployment<br/>delivery pipeline<br/>[deploy.yml]"]
end

node_bootstrap -->|"mounts"| node_app
node_app -->|"reads route"| node_routing
node_app -->|"editor surface"| node_canvas
node_app -->|"#/play"| node_player
node_editor_state -->|"owns story"| node_story_types
node_editor_state -->|"loads and saves"| node_storage
node_editor_state -->|"records mutations"| node_undo
node_canvas -->|"reads and mutates"| node_editor_state
node_canvas -->|"renders links"| node_graph_links
node_sidebar -->|"edits selected node"| node_editor_state
node_entities -->|"edits global entities"| node_editor_state
node_minigames -->|"edits mini-game nodes"| node_editor_state
node_diagnostics -->|"inspects story"| node_editor_state
node_diagnostics -->|"checks graph health"| node_graph_links
node_preview -->|"runs preview"| node_play_state
node_player -->|"runs story"| node_play_state
node_play_state -->|"evaluates paths and effects"| node_story_logic
node_play_state -->|"executes story"| node_story_types
node_interchange -->|"imports and exports"| node_editor_state
node_deployment -.->|"publishes static app"| node_bootstrap

click node_bootstrap "https://github.com/monapdx/storyplay/blob/main/src/main.tsx"
click node_app "https://github.com/monapdx/storyplay/blob/main/src/App.tsx"
click node_routing "https://github.com/monapdx/storyplay/blob/main/src/hooks/useHashRoute.ts"
click node_story_types "https://github.com/monapdx/storyplay/blob/main/src/types/story.ts"
click node_graph_links "https://github.com/monapdx/storyplay/blob/main/src/utils/nodeGraphLinks.ts"
click node_editor_state "https://github.com/monapdx/storyplay/blob/main/src/hooks/useStoryState.ts"
click node_storage "https://github.com/monapdx/storyplay/blob/main/src/utils/storyProjectStorage.ts"
click node_undo "https://github.com/monapdx/storyplay/blob/main/src/utils/storyUndoHistory.ts"
click node_canvas "https://github.com/monapdx/storyplay/blob/main/src/components/canvas/StoryCanvas.tsx"
click node_sidebar "https://github.com/monapdx/storyplay/blob/main/src/components/editor/SidebarEditor.tsx"
click node_entities "https://github.com/monapdx/storyplay/blob/main/src/components/entities/CharacterManager.tsx"
click node_minigames "https://github.com/monapdx/storyplay/blob/main/src/components/minigame/MiniGameEditorShell.tsx"
click node_diagnostics "https://github.com/monapdx/storyplay/blob/main/src/components/editor/StoryDiagnostics.tsx"
click node_play_state "https://github.com/monapdx/storyplay/blob/main/src/hooks/usePlayState.ts"
click node_story_logic "https://github.com/monapdx/storyplay/blob/main/src/utils/storyLogic.ts"
click node_preview "https://github.com/monapdx/storyplay/blob/main/src/components/preview/StoryPreview.tsx"
click node_player "https://github.com/monapdx/storyplay/blob/main/src/components/player/PlayerPage.tsx"
click node_interchange "https://github.com/monapdx/storyplay/blob/main/src/utils/importStoryPlayProject.ts"
click node_deployment "https://github.com/monapdx/storyplay/blob/main/.github/workflows/deploy.yml"

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_bootstrap,node_app,node_routing toneBlue
class node_editor_state,node_storage,node_undo,node_canvas,node_sidebar,node_entities,node_minigames,node_diagnostics toneAmber
class node_story_types,node_graph_links toneMint
class node_play_state,node_story_logic,node_preview,node_player toneRose
class node_interchange,node_deployment toneIndigo
```

## Components

### Application

- [React/Vite bootstrap](https://github.com/monapdx/storyplay/blob/main/src/main.tsx): entry point · `src/main.tsx`
- [Route surface selector](https://github.com/monapdx/storyplay/blob/main/src/App.tsx): application shell · Selects editor, player, and documentation hash routes. · `src/App.tsx`
- [Hash routing](https://github.com/monapdx/storyplay/blob/main/src/hooks/useHashRoute.ts): routing hook · `src/hooks/useHashRoute.ts`

### Authoring

- [Editor story state](https://github.com/monapdx/storyplay/blob/main/src/hooks/useStoryState.ts): state boundary · Owns mutations, loading, persistence, and undo coordination. · `src/hooks/useStoryState.ts`
- [Local project storage](https://github.com/monapdx/storyplay/blob/main/src/utils/storyProjectStorage.ts): browser persistence · `src/utils/storyProjectStorage.ts`
- [Undo/redo history](https://github.com/monapdx/storyplay/blob/main/src/utils/storyUndoHistory.ts): history store · `src/utils/storyUndoHistory.ts`
- [Visual story canvas](https://github.com/monapdx/storyplay/blob/main/src/components/canvas/StoryCanvas.tsx): React Flow editor · Renders graph nodes and writes connections as choice targets. · `src/components/canvas/StoryCanvas.tsx`
- [Node & choice inspector](https://github.com/monapdx/storyplay/blob/main/src/components/editor/SidebarEditor.tsx): editor panel · Edits selected-node content, choices, conditions, and effects. · `src/components/editor/SidebarEditor.tsx`
- [Characters & variables](https://github.com/monapdx/storyplay/blob/main/src/components/entities/CharacterManager.tsx): story-wide entities · Manages global entities and variable definitions. · `src/components/entities/CharacterManager.tsx`
- [Mini-game authoring](https://github.com/monapdx/storyplay/blob/main/src/components/minigame/MiniGameEditorShell.tsx): specialized editor · Edits typed mini-game node definitions. · `src/components/minigame/MiniGameEditorShell.tsx`
- [Story diagnostics](https://github.com/monapdx/storyplay/blob/main/src/components/editor/StoryDiagnostics.tsx): validation UI · Reports graph integrity and reference issues. · `src/components/editor/StoryDiagnostics.tsx`

### Story model

- [Story contracts](https://github.com/monapdx/storyplay/blob/main/src/types/story.ts): TypeScript model · Defines stories, core nodes, blocks, variables, and characters. · `src/types/story.ts`
- [Story graph links](https://github.com/monapdx/storyplay/blob/main/src/utils/nodeGraphLinks.ts): graph utilities · Derives node relationships from choice target IDs. · `src/utils/nodeGraphLinks.ts`

### Runtime

- [Play state](https://github.com/monapdx/storyplay/blob/main/src/hooks/usePlayState.ts): runtime state · Isolates interactive execution from editor state. · `src/hooks/usePlayState.ts`
- [Execution logic](https://github.com/monapdx/storyplay/blob/main/src/utils/storyLogic.ts): runtime utilities · Evaluates blocks, choices, chat behavior, and entry nodes. · `src/utils/storyLogic.ts`
- [Embedded preview](https://github.com/monapdx/storyplay/blob/main/src/components/preview/StoryPreview.tsx): preview surface · `src/components/preview/StoryPreview.tsx`
- [Standalone player](https://github.com/monapdx/storyplay/blob/main/src/components/player/PlayerPage.tsx): player route · Runs stories at the #/play route. · `src/components/player/PlayerPage.tsx`

### Interchange & delivery

- [Versioned import/export](https://github.com/monapdx/storyplay/blob/main/src/utils/importStoryPlayProject.ts): interchange boundary · Imports projects and serializes schema-backed exports. · `src/utils/importStoryPlayProject.ts`
- [Static deployment](https://github.com/monapdx/storyplay/blob/main/.github/workflows/deploy.yml): delivery pipeline · Deploys the Vite app to GitHub Pages; itch.io packaging is optional. · `.github/workflows/deploy.yml`

## Connections

- React/Vite bootstrap → Route surface selector: mounts
- Route surface selector → Hash routing: reads route
- Route surface selector → Visual story canvas: editor surface
- Route surface selector → Standalone player: #/play
- Editor story state → Story contracts: owns story
- Editor story state → Local project storage: loads and saves
- Editor story state → Undo/redo history: records mutations
- Visual story canvas → Editor story state: reads and mutates
- Visual story canvas → Story graph links: renders links
- Node & choice inspector → Editor story state: edits selected node
- Characters & variables → Editor story state: edits global entities
- Mini-game authoring → Editor story state: edits mini-game nodes
- Story diagnostics → Editor story state: inspects story
- Story diagnostics → Story graph links: checks graph health
- Embedded preview → Play state: runs preview
- Standalone player → Play state: runs story
- Play state → Execution logic: evaluates paths and effects
- Play state → Story contracts: executes story
- Versioned import/export → Editor story state: imports and exports
- Static deployment → React/Vite bootstrap: publishes static app
