generated from codespaces-examples/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.js
26 lines (24 loc) · 875 Bytes
/
history.js
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
import { createContext } from "react";
import { applyMove } from "./move";
export function reduceHistory(oldHistory, action) {
const history = {
boards: [...oldHistory.boards],
currentIndex: oldHistory.currentIndex,
};
if (action.move) {
history.boards.splice(history.currentIndex + 1);
history.boards.push({ move: action.move, board: applyMove(history.boards[history.currentIndex].board, action.move) });
history.currentIndex++;
} else if (action.select != undefined) {
history.currentIndex = action.select;
} else if (action.newGame) {
history.boards = [{ board: action.newGame, move: undefined }];
history.currentIndex = 0;
}
console.log(action);
console.log(history);
return history;
}
export const HistoryContext = createContext({
boards: [], currentIndex: -1
});