Skip to content

Commit 9df1ba2

Browse files
Merge pull request #272 from CSSLab/codex/fix-second-game-piece-offset
fix: reset play board state between games
2 parents b49f8b7 + b714164 commit 9df1ba2

4 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/components/Board/GameBoard.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const GameBoard: React.FC<Props> = ({
6969
destinationBadges = [],
7070
}: Props) => {
7171
const { playMoveSound } = useSound()
72+
const boardInstanceKey = game?.id ?? 'board'
7273

7374
const after = useCallback(
7475
(from: string, to: string) => {
@@ -151,6 +152,7 @@ export const GameBoard: React.FC<Props> = ({
151152
return (
152153
<div className="relative h-full w-full">
153154
<Chessground
155+
key={boardInstanceKey}
154156
contained
155157
config={{
156158
movable: {

src/components/Board/GameplayInterface.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import Head from 'next/head'
1212
import { useUnload } from 'src/hooks/useUnload'
1313
import type { DrawShape } from 'chessground/draw'
14-
import { useCallback, useContext, useMemo, useState } from 'react'
14+
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'
1515
import {
1616
AuthContext,
1717
WindowSizeContext,
@@ -57,6 +57,10 @@ export const GameplayInterface: React.FC<React.PropsWithChildren<Props>> = (
5757
[string, string] | null
5858
>(null)
5959

60+
useEffect(() => {
61+
setPromotionFromTo(null)
62+
}, [game.id])
63+
6064
const onPlayerMakeMove = useCallback(
6165
(move: [string, string] | null) => {
6266
if (move) {
@@ -199,6 +203,7 @@ export const GameplayInterface: React.FC<React.PropsWithChildren<Props>> = (
199203
className="relative flex aspect-square w-full max-w-[75vh] flex-shrink-0"
200204
>
201205
<GameBoard
206+
key={game.id}
202207
game={game}
203208
availableMoves={availableMovesMapped}
204209
onPlayerMakeMove={onPlayerMakeMove}
@@ -272,6 +277,7 @@ export const GameplayInterface: React.FC<React.PropsWithChildren<Props>> = (
272277
className="relative mx-auto flex aspect-square w-full max-w-3xl"
273278
>
274279
<GameBoard
280+
key={game.id}
275281
game={game}
276282
availableMoves={availableMovesMapped}
277283
onPlayerMakeMove={onPlayerMakeMove}

src/pages/play/hb.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const PlayHandBrain: React.FC<Props> = ({
3535
)
3636

3737
return (
38-
<PlayControllerContext.Provider value={controller}>
38+
<PlayControllerContext.Provider key={id} value={controller}>
3939
<GameplayInterface boardShapes={controller.boardShapes}>
4040
<HandBrainPlayControls
4141
game={controller.game}

src/pages/play/maia.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const PlayMaia: React.FC<Props> = ({
5353
}, [controller.playerActive, controller.game.termination])
5454

5555
return (
56-
<PlayControllerContext.Provider value={controller}>
56+
<PlayControllerContext.Provider key={id} value={controller}>
5757
<GameplayInterface>
5858
<div id="play-controls">
5959
<PlayControls

0 commit comments

Comments
 (0)