From 41b99e91b67da8d0962ec754d6dd96bf4a4bbae2 Mon Sep 17 00:00:00 2001 From: Cozma Rares Date: Sat, 5 Aug 2023 12:56:08 +0300 Subject: [PATCH] fix: history scroll --- client/src/components/ChessUI.tsx | 15 ++++++++ client/src/components/Chessboard.tsx | 57 +++++++++++++--------------- client/src/components/History.tsx | 18 +++++++-- client/src/index.css | 5 --- client/src/main.tsx | 9 +++-- client/src/routes/LocalGame.tsx | 26 +++++-------- client/src/routes/OnlineGame.tsx | 6 +-- client/src/utils/InferProps.ts | 31 +++++++++++++++ package.json | 2 +- server/src/engine.ts | 2 +- 10 files changed, 106 insertions(+), 65 deletions(-) create mode 100644 client/src/components/ChessUI.tsx create mode 100644 client/src/utils/InferProps.ts diff --git a/client/src/components/ChessUI.tsx b/client/src/components/ChessUI.tsx new file mode 100644 index 0000000..ae64c47 --- /dev/null +++ b/client/src/components/ChessUI.tsx @@ -0,0 +1,15 @@ +import InferProps from "../utils/InferProps"; +import ChessBoard from "./Chessboard"; +import History from "./History"; + +// TODO: fix border +const ChessUI: React.FC> = ( + props +) => ( +
+ + +
+); + +export default ChessUI; diff --git a/client/src/components/Chessboard.tsx b/client/src/components/Chessboard.tsx index dc353e1..9327e8b 100644 --- a/client/src/components/Chessboard.tsx +++ b/client/src/components/Chessboard.tsx @@ -30,7 +30,6 @@ import Chess, { } from "../../../server/src/engine"; import { MouseEventHandler, useState } from "react"; import Show from "../utils/Show"; -import useWindowSize from "../hooks/useWindowSize"; const PIECES: Record> = { w: { p: wp, n: wn, b: wb, r: wr, q: wq, k: wk }, @@ -42,14 +41,11 @@ const ChessBoard: React.FC<{ blackPerspective?: boolean; disabled?: boolean; }> = ({ chess, makeMove, blackPerspective, disabled }) => { - const { width, height } = useWindowSize(); const [activeTile, setActiveTile] = useState(-1); const [promotionMove, setPromotionMove] = useState< (Pick & { color: Color }) | null >(null); - const gridSize = Math.min(width, height, 800); - const tileProps = new Array(64).fill(null).map((_, i) => ({ tileNumber: i, piece: chess.getPiece(i), @@ -112,34 +108,32 @@ const ChessBoard: React.FC<{ setActiveTile(-1); }; + // FIX: border return ( <> -
+
- {blackPerspective == true ? tiles.reverse() : tiles} + {blackPerspective ? tiles.reverse() : tiles}
- {promotionMove == null ? ( - <> - ) : ( - <> -
-
- {PIECE_PROMOTION.map((p) => ( - sendPromotionMove(p)} - /> - ))} -
- - )}
+ {promotionMove && ( + <> +
+
+ {PIECE_PROMOTION.map((p) => ( + sendPromotionMove(p)} + /> + ))} +
+ + )} ); }; @@ -178,14 +172,15 @@ const Tile: React.FC<{ return (
*]:pointer-events-none " + - (isActive ? activeColor : bgColor) - } + className={[ + "relative aspect-square font-bold text-xl isolate group [&>*]:pointer-events-none outline-blue-300 hover:outline outline-4 -outline-offset-4", + isActive ? activeColor : bgColor, + piece ? "cursor-pointer" : "", + ].join(" ")} data-tile={tileNumber} > - {piece == null ? <> : } - + {piece && } + void; redo?: () => void; }> = ({ chess, newGame, switchSides, undo, redo }) => { + const historyRef = useRef(null); + + useEffect(() => { + const div = historyRef.current as HTMLDivElement; + if (div) div.scrollTop = div.scrollHeight; + }, [chess.getHistory().length]); + const history: ReactNode[] = []; chess.getHistory().forEach(({ san }, idx) => { @@ -21,6 +28,7 @@ const History: React.FC<{ {idk}. ); + history.push( {san} @@ -39,11 +47,13 @@ const History: React.FC<{ return (
History
- {/* FIX: scroll*/} -
+
{history}
-
+
{newGame && (