Skip to content

Commit 1601e17

Browse files
fix: build errors
1 parent 40ac58c commit 1601e17

10 files changed

Lines changed: 70 additions & 67 deletions

File tree

src/components/Analysis/AnalysisSidebar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export const AnalysisSidebar: React.FC<Props> = ({
144144
],
145145
}
146146
}
147-
currentNode={controller.currentNode}
147+
currentNode={controller.currentNode ?? undefined}
148148
/>
149149
</div>
150150
<div className="flex h-full w-full bg-background-1">
@@ -254,7 +254,7 @@ export const AnalysisSidebar: React.FC<Props> = ({
254254
],
255255
}
256256
}
257-
currentNode={controller.currentNode}
257+
currentNode={controller.currentNode ?? undefined}
258258
/>
259259
</div>
260260
<div className="flex h-full w-auto min-w-[40%] max-w-[40%] bg-background-1 p-3">

src/components/Analysis/BroadcastAnalysis.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,15 @@ export const BroadcastAnalysis: React.FC<Props> = ({
8686
if (analysisController.currentNode.mainChild?.move === moveString) {
8787
analysisController.goToNode(analysisController.currentNode.mainChild)
8888
} else {
89-
const newVariation = game.tree.addVariation(
90-
analysisController.currentNode,
91-
newFen,
92-
moveString,
93-
san,
94-
analysisController.currentMaiaModel,
95-
)
89+
const newVariation = game.tree
90+
.getLastMainlineNode()
91+
.addChild(
92+
newFen,
93+
moveString,
94+
san,
95+
false,
96+
analysisController.currentMaiaModel,
97+
)
9698
analysisController.goToNode(newVariation)
9799
}
98100
}
@@ -289,7 +291,6 @@ export const BroadcastAnalysis: React.FC<Props> = ({
289291
<MovesContainer
290292
game={game}
291293
termination={game.termination}
292-
type="analysis"
293294
showAnnotations={true}
294295
disableKeyboardNavigation={false}
295296
disableMoveClicking={false}
@@ -494,7 +495,6 @@ export const BroadcastAnalysis: React.FC<Props> = ({
494495
<MovesContainer
495496
game={game}
496497
termination={game.termination}
497-
type="analysis"
498498
showAnnotations={true}
499499
disableKeyboardNavigation={false}
500500
disableMoveClicking={false}

src/components/Board/BoardController.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const BoardController: React.FC<Props> = ({
6767

6868
const getLast = useCallback(() => {
6969
if (!currentNode) return
70-
70+
7171
let lastNode = currentNode
7272
while (lastNode?.mainChild) {
7373
lastNode = lastNode.mainChild

src/components/Turing/TuringGames.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import { useContext } from 'react'
33
import { TuringControllerContext } from 'src/contexts'
44

55
export const TuringGames: React.FC = () => {
6-
const { gameIds, setCurrentGameId, games } = useContext(
6+
const { gameIds, setCurrentIndex, games } = useContext(
77
TuringControllerContext,
88
)
99
return (
1010
<div className="flex flex-row flex-wrap items-start justify-start gap-1 overflow-y-auto">
11-
{gameIds.map((id) => {
12-
const game = games[id]
11+
{gameIds.map((id, index) => {
12+
const game = games[index]
1313
return (
1414
<button
1515
key={id}
16-
onClick={() => setCurrentGameId(id)}
16+
onClick={() => setCurrentIndex(index)}
1717
className={`${!game.result ? 'bg-button-secondary' : game.result?.correct ? 'bg-engine-4' : 'bg-human-4'} h-10 w-10 cursor-pointer rounded-sm`}
1818
>
1919
{game.result?.ratingDiff ? (

src/components/Turing/TuringLog.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useContext } from 'react'
22
import { TuringControllerContext } from 'src/contexts'
33

44
export const TuringLog: React.FC = () => {
5-
const { games, gameIds, setCurrentGameId, currentGameId } = useContext(
5+
const { games, gameIds, setCurrentIndex, currentIndex } = useContext(
66
TuringControllerContext,
77
)
88

@@ -18,8 +18,8 @@ export const TuringLog: React.FC = () => {
1818
</div>
1919
) : (
2020
gameIds.map((gameId, index) => {
21-
const game = games[gameId]
22-
const isCurrentGame = gameId === currentGameId
21+
const game = games[index]
22+
const isCurrentGame = index === currentIndex
2323
const getStatusInfo = () => {
2424
if (game.result?.correct === true) {
2525
return {
@@ -48,7 +48,7 @@ export const TuringLog: React.FC = () => {
4848
return (
4949
<button
5050
key={gameId}
51-
onClick={() => setCurrentGameId(gameId)}
51+
onClick={() => setCurrentIndex(index)}
5252
className={`group flex w-full cursor-pointer items-center gap-2 border-b border-white/5 px-3 py-2 text-left transition-colors ${
5353
isCurrentGame
5454
? 'bg-background-2 font-medium'

src/contexts/TuringControllerContext.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import { GameTree, Color, BaseTreeControllerContext } from 'src/types'
66

77
export interface ITuringControllerContext extends BaseTreeControllerContext {
88
game?: TuringGame
9-
games: { [id: string]: TuringGame }
9+
games: TuringGame[]
1010
loading: boolean
1111
gameIds: string[]
1212
stats: AllStats
1313

1414
getNewGame: () => Promise<void>
15-
currentGameId: string
16-
setCurrentGameId: (id: string) => void
15+
currentIndex: number
16+
setCurrentIndex: (index: number) => void
1717
submitGuess: (
1818
guess: Color,
1919
comment?: string,
@@ -40,8 +40,8 @@ const defaultContext: ITuringControllerContext = {
4040
goToRootNode: () => {
4141
/* no-op */
4242
},
43-
currentGameId: '',
44-
setCurrentGameId: () => {
43+
currentIndex: 0,
44+
setCurrentIndex: () => {
4545
/* no-op */
4646
},
4747
plyCount: 0,
@@ -50,7 +50,7 @@ const defaultContext: ITuringControllerContext = {
5050
/* no-op */
5151
},
5252
game: undefined,
53-
games: {},
53+
games: [],
5454
loading: false,
5555
gameIds: [],
5656
stats: {

src/hooks/useAnalysisController/useAnalysisController.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export const useAnalysisController = (
4040
const [analysisState, setAnalysisState] = useState(0)
4141
const inProgressAnalyses = useMemo(() => new Set<string>(), [])
4242

43-
// Use auto-save hook
4443
const autoSaveController = useAutoSave({
4544
game,
4645
gameTree: controller.tree,

src/hooks/useTreeController/useTreeController.ts

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

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

1311
useEffect(() => {
14-
setCurrentNode(gameTree?.getRoot() || null)
12+
setCurrentNode(gameTree.getRoot())
1513
}, [gameTree])
1614

1715
const plyCount = useMemo(() => {
18-
if (!gameTree) return 0
1916
return gameTree.getMainLine().length
2017
}, [gameTree])
2118

src/hooks/useTuringController/useTuringController.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ const statsLoader = async () => {
2222

2323
export const useTuringController = () => {
2424
const router = useRouter()
25-
const [turingGames, setTuringGames] = useState<{ [id: string]: TuringGame }>(
26-
{},
27-
)
25+
const [turingGames, setTuringGames] = useState<TuringGame[]>([])
2826

2927
const [loading, setLoading] = useState(false)
30-
const [currentGameId, setCurrentGameId] = useState<string>('')
28+
const [currentIndex, setCurrentIndex] = useState(0)
3129
const [stats, incrementStats, updateRating] = useStats(statsLoader)
3230

3331
const getNewGame = useCallback(async () => {
@@ -41,20 +39,20 @@ export const useTuringController = () => {
4139
}
4240

4341
setLoading(false)
44-
setTuringGames({ ...turingGames, [game.id]: game })
45-
setCurrentGameId(game.id)
42+
setCurrentIndex(turingGames.length)
43+
setTuringGames(turingGames.concat([game]))
4644
}, [turingGames, router])
4745

4846
useEffect(() => {
49-
if (Object.keys(turingGames).length === 0) getNewGame()
50-
}, [getNewGame, turingGames])
47+
if (turingGames.length === 0 && !loading) getNewGame()
48+
}, [getNewGame, turingGames.length, loading])
5149

5250
const game = useMemo(
53-
() => turingGames[currentGameId ?? ''],
54-
[turingGames, currentGameId],
51+
() => turingGames[currentIndex],
52+
[turingGames, currentIndex],
5553
)
5654

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

5957
useEffect(() => {
6058
if (controller.tree && game) {
@@ -63,26 +61,32 @@ export const useTuringController = () => {
6361
}
6462
}, [game])
6563

66-
const gameIds = useMemo(() => Object.keys(turingGames), [turingGames])
64+
const gameIds = useMemo(() => turingGames.map((g) => g.id), [turingGames])
6765

6866
const submitGuess = useCallback(
6967
async (guess: Color, comment = '', rating?: number) => {
7068
if (game && !game.result) {
7169
const result = await submitTuringGuess(game.id, guess, comment)
72-
setTuringGames({
73-
...turingGames,
74-
[game.id]: {
75-
...game,
76-
result: { ...result, ratingDiff: result.turingElo - (rating || 0) },
77-
},
78-
})
70+
setTuringGames(
71+
turingGames.map((g, index) =>
72+
index === currentIndex
73+
? {
74+
...g,
75+
result: {
76+
...result,
77+
ratingDiff: result.turingElo - (rating || 0),
78+
},
79+
}
80+
: g,
81+
),
82+
)
7983
commentController[1]('')
8084

8185
updateRating(result.turingElo)
8286
incrementStats(1, result.correct ? 1 : 0)
8387
}
8488
},
85-
[game, incrementStats, turingGames, updateRating],
89+
[game, incrementStats, turingGames, updateRating, currentIndex],
8690
)
8791

8892
const commentController = useState('')
@@ -104,8 +108,8 @@ export const useTuringController = () => {
104108
getNewGame,
105109
loading,
106110
gameIds,
107-
currentGameId,
108-
setCurrentGameId,
111+
currentIndex,
112+
setCurrentIndex,
109113
submitGuess,
110114
commentController,
111115
stats,

src/pages/turing.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ const TuringPage: NextPage = () => {
4646
}
4747
}, [controller.game, controller.stats?.rating])
4848

49+
if (controller.loading || !controller.game) {
50+
return (
51+
<DelayedLoading isLoading={true}>
52+
<div></div>
53+
</DelayedLoading>
54+
)
55+
}
56+
4957
return (
5058
<TuringControllerContext.Provider value={controller}>
51-
<DelayedLoading isLoading={!controller.game}>
52-
{controller.game && (
53-
<Turing game={controller.game} stats={controller.stats} />
54-
)}
55-
</DelayedLoading>
59+
<Turing game={controller.game} stats={controller.stats} />
5660
</TuringControllerContext.Provider>
5761
)
5862
}
@@ -66,7 +70,6 @@ const Turing: React.FC<Props> = (props: Props) => {
6670
const { game, stats } = props
6771

6872
const { isMobile } = useContext(WindowSizeContext)
69-
7073
const controller = useContext(TuringControllerContext)
7174

7275
const containerVariants = {
@@ -189,9 +192,9 @@ const Turing: React.FC<Props> = (props: Props) => {
189192
id="turing-page"
190193
className="relative flex aspect-square w-full max-w-[75vh] flex-shrink-0"
191194
>
192-
<GameBoard
193-
game={game}
194-
currentNode={controller.currentNode}
195+
<GameBoard
196+
game={game}
197+
currentNode={controller.currentNode}
195198
orientation={controller.orientation}
196199
/>
197200
</motion.div>
@@ -248,9 +251,9 @@ const Turing: React.FC<Props> = (props: Props) => {
248251
id="turing-page"
249252
className="relative flex aspect-square h-[100vw] w-screen"
250253
>
251-
<GameBoard
252-
game={game}
253-
currentNode={controller.currentNode}
254+
<GameBoard
255+
game={game}
256+
currentNode={controller.currentNode}
254257
orientation={controller.orientation}
255258
/>
256259
</div>

0 commit comments

Comments
 (0)