Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

useGameState.ts

47 lines1.1 KB

1import { useGameStateContext } from "../contexts/game-state-context"
2​
3/**
4 * Hook for accessing game state and placing edges.
5 *
6 * Provides:
7 * - gameState: The current GameState instance
8 * - boxBitmap: Shared 1px-per-box bitmap for rendering
9 * - pendingEdge: Edge ID of an optimistically placed edge (or null)
10 * - placeEdge: Function to place an edge
11 * - isLoading: Whether initial data is loading
12 * - error: Current error message (or null)
13 * - isConnected: Whether the Realtime subscription is live
14 * - version: Counter that increments on state changes (for re-render triggers)
15 * - isGameComplete: Whether the game has ended
16 */
17export function useGameState() {
18 const {
19 gameState,
20 boxBitmap,
21 pendingEdge,
22 placeEdge,
23 isLoading,
24 isReadyToRender,
25 notifyCanvasRendered,
26 error,
27 isConnected,
28 version,
29 recentEvents,
30 isGameComplete,
31 } = useGameStateContext()
32​
33 return {
34 gameState,
35 boxBitmap,
36 pendingEdge,
37 placeEdge,
38 isLoading,
39 isReadyToRender,
40 notifyCanvasRendered,
41 error,
42 isConnected,
43 version,
44 recentEvents,
45 isGameComplete,
46 }