Skip to content

Commit 50e17e2

Browse files
fix: turing page moves container + tree implementation
1 parent 6e8bbf6 commit 50e17e2

5 files changed

Lines changed: 31 additions & 22 deletions

File tree

src/components/Board/BoardController.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { useCallback, useEffect, useMemo } from 'react'
66
interface Props {
77
orientation: 'white' | 'black'
88
setOrientation: (orientation: 'white' | 'black') => void
9-
currentNode: GameNode
9+
currentNode: GameNode | null
1010
plyCount: number
1111
goToNode: (node: GameNode) => void
1212
goToNextNode: () => void
1313
goToPreviousNode: () => void
1414
goToRootNode: () => void
15-
gameTree: GameTree
15+
gameTree: GameTree | null
1616
setCurrentMove?: (move: [string, string] | null) => void
1717
disableFlip?: boolean
1818
disablePrevious?: boolean
@@ -43,17 +43,11 @@ export const BoardController: React.FC<Props> = ({
4343
}, [orientation, setOrientation])
4444

4545
const hasPrevious = useMemo(() => {
46-
if (currentNode !== undefined) {
47-
return !!currentNode?.parent
48-
}
49-
return false
46+
return !!currentNode?.parent
5047
}, [currentNode])
5148

5249
const hasNext = useMemo(() => {
53-
if (currentNode !== undefined) {
54-
return !!currentNode?.mainChild
55-
}
56-
return false
50+
return !!currentNode?.mainChild
5751
}, [currentNode])
5852

5953
const getFirst = useCallback(() => {
@@ -72,6 +66,8 @@ export const BoardController: React.FC<Props> = ({
7266
}, [goToNextNode, setCurrentMove])
7367

7468
const getLast = useCallback(() => {
69+
if (!currentNode) return
70+
7571
let lastNode = currentNode
7672
while (lastNode?.mainChild) {
7773
lastNode = lastNode.mainChild

src/components/Board/MovesContainer.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const MovesContainer: React.FC<Props> = (props) => {
6565

6666
const mainLineNodes = useMemo(() => {
6767
const fullMainLine =
68-
controller.gameTree.getMainLine() ?? game.tree.getMainLine()
68+
controller.gameTree?.getMainLine() ?? game.tree?.getMainLine() ?? []
6969

7070
if (startFromNode) {
7171
// Find the index of the start node in the full main line
@@ -103,7 +103,7 @@ export const MovesContainer: React.FC<Props> = (props) => {
103103

104104
console.log('MovesContainer mainLineNodes (default):', {
105105
controllerTreeMainLine: controller.gameTree?.getMainLine()?.length || 0,
106-
gameTreeMainLine: game.tree?.getMainLine()?.length || 0,
106+
gameTreeMainLine: game?.tree?.getMainLine()?.length || 0,
107107
finalMainLineLength: fullMainLine?.length || 0,
108108
currentNode: controller.currentNode?.fen,
109109
})
@@ -197,7 +197,7 @@ export const MovesContainer: React.FC<Props> = (props) => {
197197

198198
console.log('MovesContainer final rows:', rows.length)
199199
return rows
200-
}, [game, mainLineNodes, controller.gameTree])
200+
}, [mainLineNodes, startFromNode])
201201

202202
const highlightSet = useMemo(
203203
() => new Set(highlightIndices ?? []),

src/hooks/useTreeController/useTreeController.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { Color, GameTree, GameNode } from 'src/types'
22
import { useState, useMemo, useCallback, useEffect } from 'react'
33

44
export const useTreeController = (
5-
gameTree: GameTree,
5+
gameTree: GameTree | null | undefined,
66
initialOrientation: Color = 'white',
77
) => {
8-
const [currentNode, setCurrentNode] = useState<GameNode>(gameTree.getRoot())
8+
const [currentNode, setCurrentNode] = useState<GameNode | null>(
9+
gameTree?.getRoot() || null
10+
)
911
const [orientation, setOrientation] = useState<Color>(initialOrientation)
1012

1113
useEffect(() => {
12-
setCurrentNode(gameTree.getRoot())
14+
setCurrentNode(gameTree?.getRoot() || null)
1315
}, [gameTree])
1416

1517
const plyCount = useMemo(() => {

src/hooks/useTuringController/useTuringController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const useTuringController = () => {
5454
[turingGames, currentGameId],
5555
)
5656

57-
const controller = useTreeController(game.tree, 'white')
57+
const controller = useTreeController(game?.tree, 'white')
5858

5959
useEffect(() => {
6060
if (controller.tree && game) {

src/pages/turing.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { trackTuringGameStarted } from 'src/lib/analytics'
66
import {
77
WindowSizeContext,
88
TuringControllerContext,
9+
TreeControllerContext,
910
useTour,
1011
} from 'src/contexts'
1112
import {
@@ -188,7 +189,11 @@ const Turing: React.FC<Props> = (props: Props) => {
188189
id="turing-page"
189190
className="relative flex aspect-square w-full max-w-[75vh] flex-shrink-0"
190191
>
191-
<GameBoard game={game} currentNode={controller.currentNode} />
192+
<GameBoard
193+
game={game}
194+
currentNode={controller.currentNode}
195+
orientation={controller.orientation}
196+
/>
192197
</motion.div>
193198
<motion.div
194199
variants={itemVariants}
@@ -243,7 +248,11 @@ const Turing: React.FC<Props> = (props: Props) => {
243248
id="turing-page"
244249
className="relative flex aspect-square h-[100vw] w-screen"
245250
>
246-
<GameBoard game={game} currentNode={controller.currentNode} />
251+
<GameBoard
252+
game={game}
253+
currentNode={controller.currentNode}
254+
orientation={controller.orientation}
255+
/>
247256
</div>
248257
<div className="flex h-auto w-full flex-col gap-1">
249258
<div className="relative bottom-0 h-full flex-1 overflow-auto">
@@ -297,9 +306,11 @@ const Turing: React.FC<Props> = (props: Props) => {
297306
/>
298307
</Head>
299308
<TuringControllerContext.Provider value={controller}>
300-
<AnimatePresence mode="wait">
301-
{isMobile ? mobileLayout : desktopLayout}
302-
</AnimatePresence>
309+
<TreeControllerContext.Provider value={controller}>
310+
<AnimatePresence mode="wait">
311+
{isMobile ? mobileLayout : desktopLayout}
312+
</AnimatePresence>
313+
</TreeControllerContext.Provider>
303314
</TuringControllerContext.Provider>
304315
</>
305316
)

0 commit comments

Comments
 (0)