/** * The application store: a zustand wrapper around the pure reducer. * * @example * import { useChatState, dispatch } from "./store"; * dispatch({ type: "connection", status: "online" }); * const state = useChatState(); // inside a component */ import { create } from "zustand"; import { initialState, reduce, type ChatEvent, type ChatState, } from "./reducer"; const store = create(() => initialState); /** React hook returning the current chat state (re-renders on change). */ export const useChatState = store; /** dispatch applies an event to the store through the reducer. */ export function dispatch(event: ChatEvent): void { store.setState((state) => reduce(state, event)); } /** resetStore restores the initial state; meant for tests. */ export function resetStore(): void { store.setState(initialState, true); }