Skip to content

Commit 52da219

Browse files
fix: game swapping
1 parent bfeca23 commit 52da219

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

src/hooks/useBroadcastController.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ export const useBroadcastController = (): BroadcastStreamController => {
380380
return
381381
}
382382

383+
// Store the current game ID to preserve selection
384+
const currentGameId = currentGame?.id
385+
383386
let allGamesAfterUpdate: BroadcastGame[] = []
384387

385388
setRoundData((prevRoundData) => {
@@ -398,8 +401,8 @@ export const useBroadcastController = (): BroadcastStreamController => {
398401

399402
// Play sound for new moves only if this is the currently selected game
400403
if (
401-
currentGame &&
402-
game.id === currentGame.id &&
404+
currentGameId &&
405+
game.id === currentGameId &&
403406
existingGameState &&
404407
newLiveGame.moves.length > existingGameState.moves.length
405408
) {
@@ -426,14 +429,14 @@ export const useBroadcastController = (): BroadcastStreamController => {
426429
return newRoundData
427430
})
428431

429-
// Update current game data if it's in the update, but don't switch to a different game
430-
if (currentGame) {
432+
// Preserve game selection - only update current game if it was already selected
433+
if (currentGameId) {
431434
console.log(
432435
'Current game selected:',
433-
currentGame.white + ' vs ' + currentGame.black,
436+
currentGame?.white + ' vs ' + currentGame?.black,
434437
)
435438
const updatedCurrentGame = parseResult.games.find(
436-
(g) => g.id === currentGame.id,
439+
(g) => g.id === currentGameId,
437440
)
438441
if (updatedCurrentGame) {
439442
console.log('Updating current game with new data')
@@ -443,11 +446,11 @@ export const useBroadcastController = (): BroadcastStreamController => {
443446
console.log(
444447
'Current game not in update - keeping selection unchanged',
445448
)
449+
// Keep the current game selection even if it's not in this update
450+
// This prevents auto-switching to the first game
446451
}
447-
// Important: Do NOT change game selection if current game is not in the update
448452
} else if (allGamesAfterUpdate.length > 0) {
449453
// Auto-select first game only if no game is currently selected
450-
// Use the first game from the complete games list, not just the updated games
451454
console.log('No game selected - auto-selecting first game')
452455
console.log(
453456
'Auto-selecting:',

0 commit comments

Comments
 (0)