feat : allow custom starting fen
This commit is contained in:
@@ -14,9 +14,12 @@ export const useChessActions = (chessAtom: PrimitiveAtom<Chess>) => {
|
|||||||
[setGame]
|
[setGame]
|
||||||
);
|
);
|
||||||
|
|
||||||
const reset = useCallback(() => {
|
const reset = useCallback(
|
||||||
setGame(new Chess());
|
(fen?: string) => {
|
||||||
}, [setGame]);
|
setGame(new Chess(fen));
|
||||||
|
},
|
||||||
|
[setGame]
|
||||||
|
);
|
||||||
|
|
||||||
const copyGame = useCallback(() => {
|
const copyGame = useCallback(() => {
|
||||||
const newGame = new Chess();
|
const newGame = new Chess();
|
||||||
|
|||||||
@@ -196,3 +196,11 @@ const getPieceValue = (piece: PieceSymbol): number => {
|
|||||||
return 0;
|
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 { useAtomValue, useSetAtom } from "jotai";
|
||||||
import { Chess } from "chess.js";
|
import { Chess } from "chess.js";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
|
import { getStartingFen } from "@/lib/chess";
|
||||||
|
|
||||||
export default function LoadGame() {
|
export default function LoadGame() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -25,7 +26,7 @@ export default function LoadGame() {
|
|||||||
|
|
||||||
const resetAndSetGamePgn = useCallback(
|
const resetAndSetGamePgn = useCallback(
|
||||||
(pgn: string) => {
|
(pgn: string) => {
|
||||||
resetBoard();
|
resetBoard(getStartingFen(pgn));
|
||||||
setEval(undefined);
|
setEval(undefined);
|
||||||
setBoardOrientation(true);
|
setBoardOrientation(true);
|
||||||
setGamePgn(pgn);
|
setGamePgn(pgn);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import NextMoveButton from "./nextMoveButton";
|
|||||||
import GoToLastPositionButton from "./goToLastPositionButton";
|
import GoToLastPositionButton from "./goToLastPositionButton";
|
||||||
import SaveButton from "./saveButton";
|
import SaveButton from "./saveButton";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
import { getStartingFen } from "@/lib/chess";
|
||||||
|
|
||||||
export default function ReviewPanelToolBar() {
|
export default function ReviewPanelToolBar() {
|
||||||
const board = useAtomValue(boardAtom);
|
const board = useAtomValue(boardAtom);
|
||||||
@@ -38,7 +39,7 @@ export default function ReviewPanelToolBar() {
|
|||||||
<Tooltip title="Reset board">
|
<Tooltip title="Reset board">
|
||||||
<Grid>
|
<Grid>
|
||||||
<IconButton
|
<IconButton
|
||||||
onClick={() => resetBoard()}
|
onClick={() => resetBoard(getStartingFen(board.pgn()))}
|
||||||
disabled={boardHistory.length === 0}
|
disabled={boardHistory.length === 0}
|
||||||
>
|
>
|
||||||
<Icon icon="ri:skip-back-line" />
|
<Icon icon="ri:skip-back-line" />
|
||||||
|
|||||||
Reference in New Issue
Block a user