@@ -4,10 +4,12 @@ import { defaults } from 'chessground/state'
44import { useCallback , useContext } from 'react'
55import Chessground from '@react-chess/chessground'
66import type { DrawBrushes , DrawShape } from 'chessground/draw'
7+ import type { Key } from 'chessground/types'
78
89import { useChessSound } from 'src/hooks'
9- import { BaseGame , Check } from 'src/types'
10+ import { BaseGame , Check , GameNode } from 'src/types'
1011import { GameControllerContext } from 'src/contexts'
12+ import { PlayTreeControllerContext } from 'src/contexts/PlayTreeControllerContext/PlayTreeControllerContext'
1113
1214interface Props {
1315 game : BaseGame
@@ -19,6 +21,7 @@ interface Props {
1921 move : [ string , string ]
2022 check ?: Check
2123 }
24+ currentNode ?: GameNode
2225 shapes ?: DrawShape [ ]
2326 brushes ?: DrawBrushes
2427}
@@ -27,20 +30,29 @@ export const GameBoard: React.FC<Props> = ({
2730 game,
2831 moves,
2932 move,
33+ currentNode,
3034 setCurrentMove,
3135 setCurrentSquare,
3236 shapes,
3337 brushes,
3438} : Props ) => {
3539 const { currentIndex, orientation } = useContext ( GameControllerContext )
40+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41+ const treeCtx : any = useContext ( PlayTreeControllerContext as any )
42+ const boardOrientation : 'white' | 'black' =
43+ treeCtx ?. orientation || orientation
3644 const { playSound } = useChessSound ( )
3745
3846 const after = useCallback (
3947 ( from : string , to : string ) => {
4048 if ( setCurrentMove ) setCurrentMove ( [ from , to ] )
4149 if ( setCurrentSquare ) setCurrentSquare ( null )
4250
43- const currentFen = move ? move . fen : game . moves [ currentIndex ] ?. board
51+ const currentFen = currentNode
52+ ? currentNode . fen
53+ : move
54+ ? move . fen
55+ : game . moves [ currentIndex ] ?. board
4456 if ( currentFen ) {
4557 const chess = new Chess ( currentFen )
4658 const pieceAtDestination = chess . get ( to )
@@ -58,6 +70,7 @@ export const GameBoard: React.FC<Props> = ({
5870 game . moves ,
5971 currentIndex ,
6072 playSound ,
73+ currentNode ,
6174 ] ,
6275 )
6376
@@ -82,12 +95,27 @@ export const GameBoard: React.FC<Props> = ({
8295 autoShapes : shapes || [ ] ,
8396 brushes : { ...defaults ( ) . drawable . brushes , ...brushes } ,
8497 } ,
85- fen : move ? move . fen : game . moves [ currentIndex ] ?. board ,
86- lastMove : move
87- ? move . move
88- : [ ...( ( game . moves [ currentIndex ] ?. lastMove ?? [ ] ) as any ) ] ,
89- check : move ? move . check : game . moves [ currentIndex ] ?. check ,
90- orientation,
98+ fen : currentNode
99+ ? currentNode . fen
100+ : move
101+ ? move . fen
102+ : game . moves [ currentIndex ] ?. board ,
103+ lastMove : currentNode
104+ ? currentNode . move
105+ ? ( [ currentNode . move . slice ( 0 , 2 ) , currentNode . move . slice ( 2 , 4 ) ] as [
106+ Key ,
107+ Key ,
108+ ] )
109+ : [ ]
110+ : move
111+ ? move . move
112+ : [ ...( ( game . moves [ currentIndex ] ?. lastMove ?? [ ] ) as any ) ] ,
113+ check : currentNode
114+ ? currentNode . check
115+ : move
116+ ? move . check
117+ : game . moves [ currentIndex ] ?. check ,
118+ orientation : boardOrientation ,
91119 } }
92120 />
93121 )
0 commit comments