Skip to content

Commit adc6c15

Browse files
fix: game switching bug
1 parent 52da219 commit adc6c15

3 files changed

Lines changed: 32 additions & 15 deletions

File tree

src/hooks/useBroadcastController.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export const useBroadcastController = (): BroadcastStreamController => {
4545
const currentRoundId = useRef<string | null>(null)
4646
const gameStates = useRef<Map<string, LiveGame>>(new Map())
4747
const lastPGNData = useRef<string>('')
48+
const currentGameRef = useRef<BroadcastGame | null>(null)
49+
50+
// Keep ref in sync with state
51+
useEffect(() => {
52+
currentGameRef.current = currentGame
53+
}, [currentGame])
4854

4955
const loadBroadcasts = useCallback(async () => {
5056
try {
@@ -102,7 +108,7 @@ export const useBroadcastController = (): BroadcastStreamController => {
102108
sections.push({
103109
title: 'Community Live Broadcasts',
104110
broadcasts: unofficialActive,
105-
type: 'community-active',
111+
type: 'unofficial-active',
106112
})
107113
}
108114

@@ -114,7 +120,7 @@ export const useBroadcastController = (): BroadcastStreamController => {
114120
sections.push({
115121
title: 'Upcoming Community Broadcasts',
116122
broadcasts: unofficialUpcoming,
117-
type: 'community-upcoming',
123+
type: 'unofficial-upcoming',
118124
})
119125
}
120126

@@ -223,6 +229,7 @@ export const useBroadcastController = (): BroadcastStreamController => {
223229
})
224230

225231
currentRoundId.current = null
232+
setCurrentGame(null)
226233
gameStates.current.clear()
227234
lastPGNData.current = ''
228235
}, [])
@@ -381,7 +388,13 @@ export const useBroadcastController = (): BroadcastStreamController => {
381388
}
382389

383390
// Store the current game ID to preserve selection
384-
const currentGameId = currentGame?.id
391+
const currentGameId = currentGameRef.current?.id
392+
console.log(
393+
'handlePGNUpdate - currentGameId:',
394+
currentGameId,
395+
'currentGame:',
396+
currentGameRef.current?.white + ' vs ' + currentGameRef.current?.black,
397+
)
385398

386399
let allGamesAfterUpdate: BroadcastGame[] = []
387400

@@ -433,7 +446,9 @@ export const useBroadcastController = (): BroadcastStreamController => {
433446
if (currentGameId) {
434447
console.log(
435448
'Current game selected:',
436-
currentGame?.white + ' vs ' + currentGame?.black,
449+
currentGameRef.current?.white +
450+
' vs ' +
451+
currentGameRef.current?.black,
437452
)
438453
const updatedCurrentGame = parseResult.games.find(
439454
(g) => g.id === currentGameId,
@@ -449,14 +464,23 @@ export const useBroadcastController = (): BroadcastStreamController => {
449464
// Keep the current game selection even if it's not in this update
450465
// This prevents auto-switching to the first game
451466
}
452-
} else if (allGamesAfterUpdate.length > 0) {
453-
// Auto-select first game only if no game is currently selected
467+
} else if (!currentGameRef.current && allGamesAfterUpdate.length > 0) {
468+
// Auto-select first game only if no game is currently selected at all
454469
console.log('No game selected - auto-selecting first game')
455470
console.log(
456471
'Auto-selecting:',
457472
allGamesAfterUpdate[0].white + ' vs ' + allGamesAfterUpdate[0].black,
458473
)
459474
setCurrentGame(allGamesAfterUpdate[0])
475+
} else {
476+
console.log(
477+
'No action taken - currentGameId:',
478+
currentGameId,
479+
'currentGame exists:',
480+
!!currentGameRef.current,
481+
'allGamesAfterUpdate.length:',
482+
allGamesAfterUpdate.length,
483+
)
460484
}
461485

462486
// Update broadcast state
@@ -469,7 +493,7 @@ export const useBroadcastController = (): BroadcastStreamController => {
469493
error: null,
470494
}))
471495
},
472-
[currentBroadcast, currentGame, createLiveGameFromBroadcastGame],
496+
[currentBroadcast, createLiveGameFromBroadcastGame],
473497
)
474498

475499
const handleStreamComplete = useCallback(() => {

src/pages/broadcast/[broadcastId]/[roundId].tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,6 @@ const BroadcastAnalysisPage: NextPage = () => {
144144
lastNode.parent === analysisController.currentNode ||
145145
lastNode === analysisController.currentNode
146146

147-
console.log('Auto-follow check:', {
148-
isAtLatestPosition,
149-
currentNodeId: analysisController.currentNode.id,
150-
lastNodeParentId: lastNode.parent?.id,
151-
lastNodeId: lastNode.id,
152-
})
153-
154147
if (isAtLatestPosition) {
155148
console.log('Auto-following to new move')
156149
analysisController.setCurrentNode(lastNode)

src/pages/broadcast/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ const BroadcastsPage: NextPage = () => {
182182
<h2 className="flex items-center gap-2 text-xl font-semibold text-primary">
183183
{section.title}
184184
{(section.type === 'official-active' ||
185-
section.type === 'community-active') && (
185+
section.type === 'unofficial-active') && (
186186
<div className="flex items-center gap-1">
187187
<div className="h-2 w-2 animate-pulse rounded-full bg-red-500"></div>
188188
<span className="text-xs font-medium text-red-400">

0 commit comments

Comments
 (0)