set and clear game state using reducers - removed the need for global state management

This commit is contained in:
2026-06-01 11:22:56 +02:00
parent a69911d457
commit 38abf30ecc
5 changed files with 198 additions and 36 deletions
+38
View File
@@ -0,0 +1,38 @@
interface IDiagrams {
ply: number;
fen: string;
}
export interface IGameState {
pgn: string;
headers: string;
diagrams: IDiagrams[];
diagramClock: boolean;
}
type GameAction =
| { type: "SET_GAME"; payload: { pgn: string; headers: "" } }
| { type: "CLEAR_GAME" };
export const initialGameState = {
pgn: "",
headers: "",
diagrams: [],
diagramClock: false,
};
export const gameReducer = (state: IGameState, action: GameAction) => {
switch (action.type) {
case "SET_GAME":
return {
pgn: action.payload.pgn,
headers: "",
diagrams: [],
diagramClock: false,
};
case "CLEAR_GAME":
return initialGameState;
default:
return state;
}
};