How monapdx/storyplay works
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: useStoryState.ts Editor story state · App.tsx Route surface selector · usePlayState.ts Play state · StoryCanvas.tsx Visual story canvas
Components 19
Application
- React/Vite bootstrap
src/main.tsx - Route surface selector · Selects editor, player, and documentation hash routes.
src/App.tsx - Hash routing
src/hooks/useHashRoute.ts
Authoring
- Editor story state · Owns mutations, loading, persistence, and undo coordination.
src/hooks/useStoryState.ts - Local project storage
src/utils/storyProjectStorage.ts - Undo/redo history
src/utils/storyUndoHistory.ts - Visual story canvas · Renders graph nodes and writes connections as choice targets.
src/components/canvas/StoryCanvas.tsx - Node & choice inspector · Edits selected-node content, choices, conditions, and effects.
src/components/editor/SidebarEditor.tsx - Characters & variables · Manages global entities and variable definitions.
src/components/entities/CharacterManager.tsx - Mini-game authoring · Edits typed mini-game node definitions.
src/components/minigame/MiniGameEditorShell.tsx - Story diagnostics · Reports graph integrity and reference issues.
src/components/editor/StoryDiagnostics.tsx
Story model
- Story contracts · Defines stories, core nodes, blocks, variables, and characters.
src/types/story.ts - Story graph links · Derives node relationships from choice target IDs.
src/utils/nodeGraphLinks.ts
Runtime
- Play state · Isolates interactive execution from editor state.
src/hooks/usePlayState.ts - Execution logic · Evaluates blocks, choices, chat behavior, and entry nodes.
src/utils/storyLogic.ts - Embedded preview
src/components/preview/StoryPreview.tsx - Standalone player · Runs stories at the #/play route.
src/components/player/PlayerPage.tsx
Interchange & delivery
- Versioned import/export · Imports projects and serializes schema-backed exports.
src/utils/importStoryPlayProject.ts - Static deployment · Deploys the Vite app to GitHub Pages; itch.io packaging is optional.
.github/workflows/deploy.yml
Connections 20
- 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