fix : play board optimization
This commit is contained in:
24
src/hooks/useGameData.ts
Normal file
24
src/hooks/useGameData.ts
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user