Skip to content

Commit 03bf112

Browse files
committed
Add forwardRef with-computer game board
1 parent cdeda1b commit 03bf112

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

client/app/_components/Cell.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useCallback, useEffect } from "react";
1+
import { useState, useCallback, useEffect, forwardRef } from "react";
22
import { IMove } from "@/_types";
33
import { EGamer } from "@/_enums";
44
import Stone from "./Stone";
@@ -12,7 +12,13 @@ interface Props {
1212
onClick: (stone: IMove) => void
1313
}
1414

15-
export default function Cell({ stone, hasHint, activeGamer, clickedHint, onClick }: Props) {
15+
export default forwardRef<HTMLDivElement, Props>(function Cell({
16+
stone,
17+
hasHint,
18+
activeGamer,
19+
clickedHint,
20+
onClick
21+
}, ref) {
1622
const [ isHintClicked, setIsHintClicked ] = useState(false);
1723

1824
const handleClick = useCallback(() => {
@@ -31,11 +37,12 @@ export default function Cell({ stone, hasHint, activeGamer, clickedHint, onClick
3137

3238
return (
3339
<div onClick={handleClick}
40+
ref={ref}
3441
className="relative bg-gradient-to-r from-[#038947] via-[#03A454] to-[#04D46C] grid place-items-center rounded w-6 h-6 min-[300px]:w-8 min-[300px]:h-8 min-[320px]:w-[2.25rem] min-[320px]:h-[2.25rem] min-[400px]:w-10 min-[400px]:h-10 min-[480px]:w-12 min-[480px]:h-12 min-[600px]:w-16 min-[600px]:h-16 min-[720px]:w-[72px] min-[720px]:h-[72px] group">
3542

3643
{stone.gamer && <Stone gamer={stone.gamer}/>}
3744

3845
{( clickedHint ? isHintClicked : hasHint) && <Hint isHintClicked={isHintClicked} activeGamer={activeGamer}/>}
3946
</div>
4047
)
41-
}
48+
})

client/app/_components/with-computer/Board.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ export default function Board() {
2020
const { game, board, updateGame, updateBoard } = usePlayWithComputerStore();
2121
const { onOpen } = useGameResultModal()
2222

23-
/**
24-
* This state will be used to hint animation
25-
*/
26-
const [ isHintClicked, setIsHintClicked ] = useState<boolean>(false)
23+
24+
const [ clickedHint, setClickedHint ] = useState<IMove | null>(null);
2725
const cellRefs = useRef<(HTMLDivElement | null)[]>([]);
2826
const activeMoveOrder = useDeepCompareMemoize<IActiveGamerData>(getActiveGamerData(game as IGame));
2927
const opponent: EGamer = activeMoveOrder.gamer.color == EGamer.BLACK ? EGamer.WHITE : EGamer.BLACK;
@@ -41,7 +39,7 @@ export default function Board() {
4139
}, [ hints ]);
4240

4341
const handleHint = useCallback(async (move: IMove) => {
44-
if (isHintClicked) return;
42+
if (clickedHint) return;
4543
if (!game?.isGameStarted) {
4644
return toast.info("Game has not started yet!");
4745
}
@@ -60,7 +58,7 @@ export default function Board() {
6058
gamer: activeMoveOrder.gamer.color,
6159
})))
6260
];
63-
setIsHintClicked(true);
61+
setClickedHint(move);
6462

6563
setTimeout(() => {
6664
updateBoard(board.map(cell => {
@@ -74,7 +72,7 @@ export default function Board() {
7472
});
7573
}, 500);// set timeout is for hint animation
7674

77-
}, [ game, board, activeMoveOrder, isHintClicked ]);
75+
}, [ game, board, activeMoveOrder, clickedHint ]);
7876

7977
const handleGameFinish = useCallback(() => {
8078
const winnerGamer: string | null = getWinnerGamer(game as IGame, board);
@@ -142,9 +140,9 @@ export default function Board() {
142140
}
143141
}, [ isComputerTurn, board, game?.isGameStarted ]);
144142

145-
// set to initial state when component unmount
143+
// set to initial state when active move order change
146144
useEffect(() => {
147-
setIsHintClicked(false);
145+
setClickedHint(null);
148146
}, [ activeMoveOrder ]);
149147

150148

@@ -158,12 +156,13 @@ export default function Board() {
158156

159157
return (
160158
<section
161-
className={`${isHintClicked ? 'pointer-events-none' : ''} relative grid grid-cols-8 grid-rows-8 gap-0.5 sm:gap-1 bg-white`}>
159+
className="relative grid grid-cols-8 grid-rows-8 gap-0.5 sm:gap-1 bg-white">
162160
{board.map((cell, index) => <Cell key={`${cell.row}${cell.col}`}
163161
onClick={handleHint}
164162
hasHint={hints.includes(index)}
165163
stone={cell}
166164
activeGamer={activeMoveOrder.gamer.color}
165+
clickedHint={clickedHint}
167166
ref={(element: HTMLDivElement) => {
168167
cellRefs.current[index] = element
169168
}}/>)}

0 commit comments

Comments
 (0)