nandi/oripublic Fork 0
f5c963af3c1597c0274ce4a99705dbd92a7d1cba
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

store.ts · 31 lines · 853 BTypeScript Blame HistoryRaw
 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);
}