fix : play board optimization

This commit is contained in:
GuillaumeSD
2024-03-20 00:56:40 +01:00
parent 438ec60107
commit aca8910bab
7 changed files with 45 additions and 10 deletions

24
src/hooks/useGameData.ts Normal file
View File

@@ -0,0 +1,24 @@
import { Chess, Move } from "chess.js";
import { PrimitiveAtom, useAtom, useAtomValue } from "jotai";
import { useEffect } from "react";
export interface GameData {
history: Move[];
lastMove: Move | undefined;
}
export const useGameData = (
gameAtom: PrimitiveAtom<Chess>,
gameDataAtom: PrimitiveAtom<GameData>
) => {
const game = useAtomValue(gameAtom);
const [gameData, setGameData] = useAtom(gameDataAtom);
useEffect(() => {
const history = game.history({ verbose: true });
const lastMove = history.at(-1);
setGameData({ history, lastMove });
}, [game]); // eslint-disable-line react-hooks/exhaustive-deps
return gameData;
};