From 61d17a6c5699b7220ec6063dbbb03cd9ec224369 Mon Sep 17 00:00:00 2001 From: GuillaumeSD Date: Sat, 6 Apr 2024 03:20:02 +0200 Subject: [PATCH] fix : copyGame throws on empty game --- src/hooks/useChessActions.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/hooks/useChessActions.ts b/src/hooks/useChessActions.ts index 07eb18c..fc21ae6 100644 --- a/src/hooks/useChessActions.ts +++ b/src/hooks/useChessActions.ts @@ -37,7 +37,11 @@ export const useChessActions = (chessAtom: PrimitiveAtom) => { const copyGame = useCallback(() => { const newGame = new Chess(); - newGame.loadPgn(game.pgn()); + if (game.history().length > 0) { + newGame.loadPgn(game.pgn()); + } else { + newGame.load(game.fen()); + } return newGame; }, [game]); @@ -65,13 +69,13 @@ export const useChessActions = (chessAtom: PrimitiveAtom) => { }, [copyGame, setGame]); const goToMove = useCallback( - (moveIdx: number, game: Chess) => { + (moveIdx: number, fullGame: Chess) => { if (moveIdx < 0) return; const newGame = new Chess(); - newGame.loadPgn(game.pgn()); + newGame.loadPgn(fullGame.pgn()); - const movesNb = game.history().length; + const movesNb = fullGame.history().length; if (moveIdx > movesNb) return; let lastMove: Move | null = null;