feat : allow custom starting fen

This commit is contained in:
GuillaumeSD
2024-03-17 22:13:54 +01:00
parent 72826d03b2
commit d916899731
4 changed files with 18 additions and 5 deletions

View File

@@ -14,9 +14,12 @@ export const useChessActions = (chessAtom: PrimitiveAtom<Chess>) => {
[setGame]
);
const reset = useCallback(() => {
setGame(new Chess());
}, [setGame]);
const reset = useCallback(
(fen?: string) => {
setGame(new Chess(fen));
},
[setGame]
);
const copyGame = useCallback(() => {
const newGame = new Chess();

View File

@@ -196,3 +196,11 @@ const getPieceValue = (piece: PieceSymbol): number => {
return 0;
}
};
export const getStartingFen = (pgn: string): string => {
const game = new Chess();
game.loadPgn(pgn);
const history = game.history({ verbose: true });
return history[0].before;
};

View File

@@ -12,6 +12,7 @@ import { useGameDatabase } from "@/hooks/useGameDatabase";
import { useAtomValue, useSetAtom } from "jotai";
import { Chess } from "chess.js";
import { useRouter } from "next/router";
import { getStartingFen } from "@/lib/chess";
export default function LoadGame() {
const router = useRouter();
@@ -25,7 +26,7 @@ export default function LoadGame() {
const resetAndSetGamePgn = useCallback(
(pgn: string) => {
resetBoard();
resetBoard(getStartingFen(pgn));
setEval(undefined);
setBoardOrientation(true);
setGamePgn(pgn);

View File

@@ -8,6 +8,7 @@ import NextMoveButton from "./nextMoveButton";
import GoToLastPositionButton from "./goToLastPositionButton";
import SaveButton from "./saveButton";
import { useEffect } from "react";
import { getStartingFen } from "@/lib/chess";
export default function ReviewPanelToolBar() {
const board = useAtomValue(boardAtom);
@@ -38,7 +39,7 @@ export default function ReviewPanelToolBar() {
<Tooltip title="Reset board">
<Grid>
<IconButton
onClick={() => resetBoard()}
onClick={() => resetBoard(getStartingFen(board.pgn()))}
disabled={boardHistory.length === 0}
>
<Icon icon="ri:skip-back-line" />