From e47ce83ebee6358afca8612d2ef70f920122248f Mon Sep 17 00:00:00 2001 From: GuillaumeSD Date: Thu, 7 Mar 2024 22:01:33 +0100 Subject: [PATCH] fix : square renderer performance issue --- src/hooks/useSquareRenderer.tsx | 73 ------------------- src/sections/analysis/board/index.tsx | 8 +- .../analysis/board/squareRenderer.tsx | 65 +++++++++++++++++ 3 files changed, 67 insertions(+), 79 deletions(-) delete mode 100644 src/hooks/useSquareRenderer.tsx create mode 100644 src/sections/analysis/board/squareRenderer.tsx diff --git a/src/hooks/useSquareRenderer.tsx b/src/hooks/useSquareRenderer.tsx deleted file mode 100644 index bec282d..0000000 --- a/src/hooks/useSquareRenderer.tsx +++ /dev/null @@ -1,73 +0,0 @@ -import { showPlayerMoveIconAtom } from "@/sections/analysis/states"; -import { MoveClassification } from "@/types/enums"; -import { CurrentPosition } from "@/types/eval"; -import { useAtomValue } from "jotai"; -import Image from "next/image"; -import { CSSProperties, forwardRef, useMemo } from "react"; -import { CustomSquareProps } from "react-chessboard/dist/chessboard/types"; - -export const useSquareRenderer = (position: CurrentPosition) => { - const showPlayerMoveIcon = useAtomValue(showPlayerMoveIconAtom); - - const CustomSquareRenderer = useMemo(() => { - const fromSquare = position.lastMove?.from; - const toSquare = position.lastMove?.to; - const moveClassification = position?.eval?.moveClassification; - - if (!showPlayerMoveIcon || !moveClassification || !fromSquare || !toSquare) - return undefined; - - const squareRenderer = forwardRef( - (props, ref) => { - const { children, square, style } = props; - - const customSquareStyle: CSSProperties | undefined = - fromSquare === square || toSquare === square - ? { - position: "absolute", - width: "100%", - height: "100%", - backgroundColor: moveClassificationColors[moveClassification], - opacity: 0.5, - } - : undefined; - - return ( -
- {children} - {customSquareStyle &&
} - {square === toSquare && ( - move-icon - )} -
- ); - } - ); - - squareRenderer.displayName = "CustomSquareRenderer"; - - return squareRenderer; - }, [showPlayerMoveIcon, position]); - - return CustomSquareRenderer; -}; - -export const moveClassificationColors: Record = { - [MoveClassification.Best]: "#3aab18", - [MoveClassification.Book]: "#d5a47d", - [MoveClassification.Excellent]: "#3aab18", - [MoveClassification.Good]: "#81b64c", - [MoveClassification.Inaccuracy]: "#f7c631", - [MoveClassification.Mistake]: "#ffa459", - [MoveClassification.Blunder]: "#fa412d", -}; diff --git a/src/sections/analysis/board/index.tsx b/src/sections/analysis/board/index.tsx index 47488b8..742966c 100644 --- a/src/sections/analysis/board/index.tsx +++ b/src/sections/analysis/board/index.tsx @@ -14,10 +14,7 @@ import PlayerInfo from "./playerInfo"; import EvaluationBar from "./evaluationBar"; import { useScreenSize } from "@/hooks/useScreenSize"; import { MoveClassification } from "@/types/enums"; -import { - moveClassificationColors, - useSquareRenderer, -} from "@/hooks/useSquareRenderer"; +import SquareRenderer, { moveClassificationColors } from "./squareRenderer"; export default function Board() { const boardRef = useRef(null); @@ -27,7 +24,6 @@ export default function Board() { const showBestMoveArrow = useAtomValue(showBestMoveArrowAtom); const { makeMove: makeBoardMove } = useChessActions(boardAtom); const position = useAtomValue(currentPositionAtom); - const squareRenderer = useSquareRenderer(position); const onPieceDrop = ( source: Square, @@ -108,7 +104,7 @@ export default function Board() { borderRadius: "5px", boxShadow: "0 2px 10px rgba(0, 0, 0, 0.5)", }} - customSquare={squareRenderer} + customSquare={SquareRenderer} /> diff --git a/src/sections/analysis/board/squareRenderer.tsx b/src/sections/analysis/board/squareRenderer.tsx new file mode 100644 index 0000000..48b57f4 --- /dev/null +++ b/src/sections/analysis/board/squareRenderer.tsx @@ -0,0 +1,65 @@ +import { currentPositionAtom, showPlayerMoveIconAtom } from "../states"; +import { MoveClassification } from "@/types/enums"; +import { useAtomValue } from "jotai"; +import Image from "next/image"; +import { CSSProperties, forwardRef } from "react"; +import { CustomSquareProps } from "react-chessboard/dist/chessboard/types"; + +const SquareRenderer = forwardRef( + (props, ref) => { + const { children, square, style } = props; + const showPlayerMoveIcon = useAtomValue(showPlayerMoveIconAtom); + const position = useAtomValue(currentPositionAtom); + + const fromSquare = position.lastMove?.from; + const toSquare = position.lastMove?.to; + const moveClassification = position?.eval?.moveClassification; + + const showPlayerMove = moveClassification && showPlayerMoveIcon; + + const customSquareStyle: CSSProperties | undefined = + showPlayerMove && (fromSquare === square || toSquare === square) + ? { + position: "absolute", + width: "100%", + height: "100%", + backgroundColor: moveClassificationColors[moveClassification], + opacity: 0.5, + } + : undefined; + + return ( +
+ {children} + {customSquareStyle &&
} + {showPlayerMove && square === toSquare && ( + move-icon + )} +
+ ); + } +); + +SquareRenderer.displayName = "CustomSquareRenderer"; + +export default SquareRenderer; + +export const moveClassificationColors: Record = { + [MoveClassification.Best]: "#3aab18", + [MoveClassification.Book]: "#d5a47d", + [MoveClassification.Excellent]: "#3aab18", + [MoveClassification.Good]: "#81b64c", + [MoveClassification.Inaccuracy]: "#f7c631", + [MoveClassification.Mistake]: "#ffa459", + [MoveClassification.Blunder]: "#fa412d", +};