Skip to main content

useScene

The useScene hook is the main state store for Pascal’s scene graph. It’s built with Zustand and includes middleware for undo/redo (temporal) and localStorage persistence.

Import

State Structure

SceneState

Record<AnyNodeId, AnyNode>
Flat dictionary of all nodes in the scene, keyed by node ID.
AnyNodeId[]
Array of root-level node IDs (typically contains a single SiteNode ID).
Set<AnyNodeId>
Set of node IDs that have been modified and need revalidation by systems (wall physics, item collision, etc.).

Methods

Scene Management

loadScene

Loads the default scene if none exists. Creates a hierarchy: Site → Building → Level.

clearScene

Clears all nodes and resets to default scene.

setScene

Replaces entire scene with new nodes.
Record<AnyNodeId, AnyNode>
required
Complete node dictionary
AnyNodeId[]
required
Root node IDs for the new scene

Dirty Node Tracking

markDirty

Marks a node as dirty, indicating it needs revalidation.
AnyNodeId
required
Node ID to mark dirty

clearDirty

Removes a node from the dirty set after revalidation.
AnyNodeId
required
Node ID to clear

Node Operations

createNode

Creates a single node and optionally adds it to a parent’s children array.
AnyNode
required
The node to create (must have a unique id)
AnyNodeId
Optional parent node ID. If provided, the node will be added to the parent’s children array.

createNodes

Batch create multiple nodes in a single operation.
{ node: AnyNode; parentId?: AnyNodeId }[]
required
Array of node creation operations

updateNode

Updates a single node with partial data.
AnyNodeId
required
ID of the node to update
Partial<AnyNode>
required
Partial node data to merge with existing node

updateNodes

Batch update multiple nodes in a single operation.
{ id: AnyNodeId; data: Partial<AnyNode> }[]
required
Array of update operations

deleteNode

Deletes a single node and its descendants.
AnyNodeId
required
ID of the node to delete

deleteNodes

Batch delete multiple nodes.
AnyNodeId[]
required
Array of node IDs to delete

Middleware

Temporal (Undo/Redo)

Provided by zundo. Access undo/redo state via:
Configuration:
  • Partialized state: Only nodes and rootNodeIds are tracked in history
  • Limit: Last 50 actions
  • Auto re-validation: After undo/redo, all nodes are marked dirty to trigger system revalidation

Persist (LocalStorage)

Provided by zustand/middleware/persist. Configuration:
  • Storage key: editor-storage
  • Version: 1
  • Partialized state: Only nodes (excluding transient nodes) and rootNodeIds are persisted
  • Transient nodes: Nodes with metadata.isTransient === true are excluded from persistence

Usage Examples

Basic Usage

Creating Nodes

Batch Operations

Updating Nodes

Undo/Redo

Dirty Node System

Type Definitions

See Also