@@ -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 ( ( ) => {
0 commit comments