Skip to content

Commit 06d59bd

Browse files
feat: migrate play page to use tree structure
1 parent ec26156 commit 06d59bd

15 files changed

Lines changed: 1061 additions & 70 deletions

File tree

src/components/Board/AnalysisBoardController.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { useCallback, useContext, useEffect, useMemo } from 'react'
2-
31
import { useWindowSize } from 'src/hooks'
42
import { FlipIcon } from 'src/components/Icons/icons'
53
import { AnalysisGameControllerContext } from 'src/contexts/'
4+
import { useCallback, useContext, useEffect, useMemo } from 'react'
65

76
export const AnalysisBoardController: React.FC = () => {
87
const { width } = useWindowSize()

src/components/Board/GameBoard.tsx

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { defaults } from 'chessground/state'
44
import { useCallback, useContext } from 'react'
55
import Chessground from '@react-chess/chessground'
66
import type { DrawBrushes, DrawShape } from 'chessground/draw'
7+
import type { Key } from 'chessground/types'
78

89
import { useChessSound } from 'src/hooks'
9-
import { BaseGame, Check } from 'src/types'
10+
import { BaseGame, Check, GameNode } from 'src/types'
1011
import { GameControllerContext } from 'src/contexts'
12+
import { PlayTreeControllerContext } from 'src/contexts/PlayTreeControllerContext/PlayTreeControllerContext'
1113

1214
interface 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
)

src/components/Board/GameClock.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useState, useEffect, useContext } from 'react'
22

33
import { Color } from 'src/types'
44
import { AuthContext, ThemeContext } from 'src/contexts'
5-
import { PlayControllerContext } from 'src/contexts/PlayControllerContext'
5+
import { PlayTreeControllerContext } from 'src/contexts/PlayTreeControllerContext/PlayTreeControllerContext'
66

77
interface Props {
88
player: Color
@@ -15,7 +15,7 @@ export const GameClock: React.FC<Props> = (
1515
const { theme } = useContext(ThemeContext)
1616
const { user } = useContext(AuthContext)
1717
const { player, toPlay, whiteClock, blackClock, lastMoveTime } = useContext(
18-
PlayControllerContext,
18+
PlayTreeControllerContext,
1919
)
2020

2121
const [referenceTime, setReferenceTime] = useState<number>(Date.now())

0 commit comments

Comments
 (0)