refactor(web): evaluate Zustand for WebSocket and game state management #7
Labels
No labels
debt
feature
feature
infra
multiplayer
multiplayer
security
ux
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: forgejo-lila/lila#7
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Background
Currently, WebSocket connection state and multiplayer game state are managed
via React Context scoped to the
/multiplayerlayout route. This is the rightcall for the initial slice — one connection, one game screen, simple state.
As the app grows, context will start showing its limits. This issue tracks
when and whether to introduce Zustand as a state manager.
Triggers — revisit this when any of the following land
1. Reconnection handling slice
Reconnection requires game state (current question, scores, submitted answers,
lobby code) to survive component unmounts during the reconnect cycle. React
state and context are destroyed on unmount. Zustand store persists. This is
probably the strongest trigger.
2. Game state shared across many components
When the game screen grows beyond a single route component into multiple
independent components (ScoreBar, PlayerList, Timer, QuestionCard) each
needing their own slice of game state, prop drilling and context re-renders
become painful. Zustand's per-selector subscriptions solve this cleanly.
3. WebSocket messages updating state outside
/multiplayerIf notifications, challenges, or any WS-driven UI needs to appear outside
the multiplayer subtree (e.g. "you've been challenged" toast on the home
page), context scoped to
/multiplayercan't reach it. Zustand as a globalstore can.
4. Optimistic updates
Chat, reactions, or any fire-and-forget interaction that needs immediate UI
feedback before server confirmation. Zustand handles optimistic update
patterns cleanly.
What to evaluate at that point
WsContextprovider with a Zustand store.from local component state into Zustand slices.
ws-client.tsabstraction — it doesn't change, only where itsstate lives changes.
zustand/middlewaredevtools for debugging game state transitions.What NOT to do prematurely
— that stays local.
Related
apps/web/src/lib/ws-client.tsapps/web/src/routes/multiplayer.tsx(layout route + WsProvider)apps/web/src/routes/multiplayer/game.$code.tsx