1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/**
* 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<ChatState>(() => 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);
}
|