feat : allow custom starting fen
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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" />
|
||||
|
||||
Reference in New Issue
Block a user