@@ -23,6 +23,7 @@ import {
2323 trackDrillConfigurationCompleted ,
2424} from 'src/lib/analytics'
2525import { MAIA_MODELS_WITH_NAMES } from 'src/constants/common'
26+ import { selectOpeningDrills } from 'src/api/opening'
2627
2728type MobileTab = 'browse' | 'selected'
2829
@@ -1029,41 +1030,70 @@ export const OpeningSelectionModal: React.FC<Props> = ({
10291030 return sequence . sort ( ( ) => Math . random ( ) - 0.5 )
10301031 }
10311032
1032- const handleStartDrilling = ( ) => {
1033+ const handleStartDrilling = async ( ) => {
10331034 if ( selections . length > 0 ) {
10341035 const drillSequence = generateDrillSequence ( selections , drillCount )
1035- const configuration : DrillConfiguration = {
1036- selections,
1037- drillCount,
1038- drillSequence,
1039- }
10401036
1041- // Track drill configuration completion
1042- const uniqueOpenings = new Set ( selections . map ( ( s ) => s . opening . id ) ) . size
1043- const averageTargetMoves =
1044- selections . reduce ( ( sum , s ) => sum + s . targetMoveNumber , 0 ) /
1045- selections . length
1046- const maiaVersionsUsed = [
1047- ...new Set ( selections . map ( ( s ) => s . maiaVersion ) ) ,
1048- ]
1049- const colorDistribution = selections . reduce (
1050- ( acc , s ) => {
1051- acc [ s . playerColor ] ++
1052- return acc
1053- } ,
1054- { white : 0 , black : 0 } ,
1055- )
1037+ try {
1038+ // Prepare API request data
1039+ const openings = selections . map ( ( selection ) => ( {
1040+ opening_fen : selection . variation
1041+ ? selection . variation . fen
1042+ : selection . opening . fen ,
1043+ side_played : selection . playerColor ,
1044+ } ) )
1045+
1046+ // Call the backend API to log opening selections and get session ID
1047+ const response = await selectOpeningDrills ( {
1048+ openings,
1049+ opponent : selectedMaiaVersion . id ,
1050+ num_moves : targetMoveNumber ,
1051+ num_drills : drillCount ,
1052+ } )
1053+
1054+ const configuration : DrillConfiguration = {
1055+ selections,
1056+ drillCount,
1057+ drillSequence,
1058+ sessionId : response . session_id ,
1059+ }
10561060
1057- trackDrillConfigurationCompleted (
1058- selections . length ,
1059- drillCount ,
1060- uniqueOpenings ,
1061- averageTargetMoves ,
1062- maiaVersionsUsed ,
1063- colorDistribution ,
1064- )
1061+ // Track drill configuration completion
1062+ const uniqueOpenings = new Set ( selections . map ( ( s ) => s . opening . id ) ) . size
1063+ const averageTargetMoves =
1064+ selections . reduce ( ( sum , s ) => sum + s . targetMoveNumber , 0 ) /
1065+ selections . length
1066+ const maiaVersionsUsed = [
1067+ ...new Set ( selections . map ( ( s ) => s . maiaVersion ) ) ,
1068+ ]
1069+ const colorDistribution = selections . reduce (
1070+ ( acc , s ) => {
1071+ acc [ s . playerColor ] ++
1072+ return acc
1073+ } ,
1074+ { white : 0 , black : 0 } ,
1075+ )
10651076
1066- onComplete ( configuration )
1077+ trackDrillConfigurationCompleted (
1078+ selections . length ,
1079+ drillCount ,
1080+ uniqueOpenings ,
1081+ averageTargetMoves ,
1082+ maiaVersionsUsed ,
1083+ colorDistribution ,
1084+ )
1085+
1086+ onComplete ( configuration )
1087+ } catch ( error ) {
1088+ console . error ( 'Failed to start drilling session:' , error )
1089+ // Still allow the drill to start even if API call fails
1090+ const configuration : DrillConfiguration = {
1091+ selections,
1092+ drillCount,
1093+ drillSequence,
1094+ }
1095+ onComplete ( configuration )
1096+ }
10671097 }
10681098 }
10691099
0 commit comments