@@ -293,17 +293,21 @@ export const useOpeningDrillController = (
293293 )
294294
295295 // Complete current drill and show performance modal
296- const completeDrill = useCallback ( ( ) => {
297- if ( ! currentDrillGame ) return
298-
299- const performanceData = evaluateDrillPerformance ( currentDrillGame )
300- setCurrentPerformanceData ( performanceData )
301- setCompletedDrills ( ( prev ) => [ ...prev , performanceData . drill ] )
302-
303- // Don't remove from remaining drills here - do it in moveToNextDrill
304- // This ensures proper counting for the performance modal
305- setShowPerformanceModal ( true )
306- } , [ currentDrillGame , evaluateDrillPerformance ] )
296+ const completeDrill = useCallback (
297+ ( gameToComplete ?: OpeningDrillGame ) => {
298+ const drillGame = gameToComplete || currentDrillGame
299+ if ( ! drillGame ) return
300+
301+ const performanceData = evaluateDrillPerformance ( drillGame )
302+ setCurrentPerformanceData ( performanceData )
303+ setCompletedDrills ( ( prev ) => [ ...prev , performanceData . drill ] )
304+
305+ // Don't remove from remaining drills here - do it in moveToNextDrill
306+ // This ensures proper counting for the performance modal
307+ setShowPerformanceModal ( true )
308+ } ,
309+ [ currentDrillGame , evaluateDrillPerformance ] ,
310+ )
307311
308312 // Move to next drill
309313 const moveToNextDrill = useCallback ( ( ) => {
@@ -471,7 +475,14 @@ export const useOpeningDrillController = (
471475 setCurrentDrillGame ( loadedGame )
472476 setAnalysisEnabled ( true ) // Auto-enable analysis when loading a completed drill
473477 setContinueAnalyzingMode ( true ) // Allow moves beyond target count
474- setWaitingForMaiaResponse ( false ) // Ensure we're not waiting for Maia
478+
479+ // Check if it's Maia's turn after loading and set waiting flag accordingly
480+ const isMaiaTurn = finalNode
481+ ? new Chess ( finalNode . fen ) . turn ( ) !==
482+ ( completedDrill . selection . playerColor === 'white' ? 'w' : 'b' )
483+ : false
484+
485+ setWaitingForMaiaResponse ( isMaiaTurn ) // Set to true if it's Maia's turn, false otherwise
475486
476487 // Set the controller to the final position after ensuring game state is set
477488 setTimeout ( ( ) => {
@@ -689,7 +700,7 @@ export const useOpeningDrillController = (
689700 ) {
690701 // Delay completion to allow for Maia's response if it's Maia's turn
691702 setTimeout ( ( ) => {
692- completeDrill ( )
703+ completeDrill ( updatedGame )
693704 } , 1500 )
694705 }
695706 }
0 commit comments