From 3ae46888ac29eff28b44166158c41328afe1e0c5 Mon Sep 17 00:00:00 2001 From: Gabriel Burbach Date: Fri, 9 Feb 2024 21:56:07 -0600 Subject: [PATCH] Removed defaults from chessboard-wrapper --- src/client/chessboard-wrapper.tsx | 24 ++++++++---------------- src/client/setup/setup-base.tsx | 7 ++++++- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/client/chessboard-wrapper.tsx b/src/client/chessboard-wrapper.tsx index fd31b2c6..e950d4ea 100644 --- a/src/client/chessboard-wrapper.tsx +++ b/src/client/chessboard-wrapper.tsx @@ -24,6 +24,8 @@ function computeChessboardTransform( return { left: xShift, top: yShift, height, width }; } +const CLICK_STYLE = { backgroundColor: "red" }; + interface ChessboardWrapperProps { chess: Chess; isWhite: boolean; @@ -31,9 +33,7 @@ interface ChessboardWrapperProps { } export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { - const { onMove } = props; - let isWhite = props.isWhite ?? true; - const chess = props.chess ?? new Chess(DEFAULT_POSITION); + const { chess, isWhite, onMove } = props; // Chessboard does not like 0 default height and width for some reason const [transform, setTransform] = useState({ @@ -58,7 +58,10 @@ export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { legalSquares = chess .moves({ square: lastClickedSquare, verbose: true }) .map((move) => move.to); - addLegalMoveStyles(customSquareStyles, legalSquares); + + legalSquares.forEach((square) => { + customSquareStyles[square] = CLICK_STYLE; + }); } /** @@ -102,7 +105,7 @@ export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { } }} isDraggablePiece={({ sourceSquare }) => - chess.get(sourceSquare).color == + chess.get(sourceSquare).color === (isWhite ? "w" : "b") } arePremovesAllowed={true} @@ -113,14 +116,3 @@ export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { ); } - -const CLICK_STYLE = { backgroundColor: "red" }; - -function addLegalMoveStyles( - customSquareStyles: { [square: string]: Object }, - legalSquares: string[], -): void { - legalSquares.forEach((square) => { - customSquareStyles[square] = CLICK_STYLE; - }); -} diff --git a/src/client/setup/setup-base.tsx b/src/client/setup/setup-base.tsx index ed22c59f..c3f7e177 100644 --- a/src/client/setup/setup-base.tsx +++ b/src/client/setup/setup-base.tsx @@ -2,6 +2,7 @@ import { Dialog, DialogBody, DialogFooter } from "@blueprintjs/core"; import { Outlet } from "react-router-dom"; import { ChessboardWrapper } from "../chessboard-wrapper"; import { PropsWithChildren, ReactNode } from "react"; +import { Chess, DEFAULT_POSITION, Square } from "chess.js"; interface SetupBaseProps extends PropsWithChildren { actions?: ReactNode; @@ -11,7 +12,11 @@ export function SetupBase(props: SetupBaseProps): JSX.Element { return ( <> - + {}} + />