@@ -15,6 +15,7 @@ import { ScheduleSettings } from "../components/robot/ScheduleSettings";
1515import { apiUrl } from "../apiConfig" ;
1616import { useNavigate } from 'react-router-dom' ;
1717import { AuthContext } from '../context/auth' ;
18+ import { useSocketStore } from '../context/socket' ;
1819
1920interface MainPageProps {
2021 handleEditRecording : ( id : string , fileName : string ) => void ;
@@ -54,6 +55,8 @@ export const MainPage = ({ handleEditRecording, initialContent }: MainPageProps)
5455 const { state } = useContext ( AuthContext ) ;
5556 const { user } = state ;
5657
58+ const { connectToQueueSocket, disconnectQueueSocket } = useSocketStore ( ) ;
59+
5760 const abortRunHandler = ( runId : string , robotName : string , browserId : string ) => {
5861 notify ( 'info' , t ( 'main_page.notifications.abort_initiated' , { name : robotName } ) ) ;
5962
@@ -138,50 +141,7 @@ export const MainPage = ({ handleEditRecording, initialContent }: MainPageProps)
138141 navigate ( `/runs/${ robotMetaId } /run/${ runId } ` ) ;
139142
140143 if ( queued ) {
141- console . log ( 'Creating queue socket for queued run:' , runId ) ;
142-
143144 setQueuedRuns ( prev => new Set ( [ ...prev , runId ] ) ) ;
144-
145- const queueSocket = io ( `${ apiUrl } /queued-run` , {
146- transports : [ "websocket" ] ,
147- rejectUnauthorized : false ,
148- query : { userId : user ?. id }
149- } ) ;
150-
151- queueSocket . on ( 'connect' , ( ) => {
152- console . log ( 'Queue socket connected for user:' , user ?. id ) ;
153- } ) ;
154-
155- queueSocket . on ( 'connect_error' , ( error ) => {
156- console . log ( 'Queue socket connection error:' , error ) ;
157- } ) ;
158-
159- queueSocket . on ( 'run-completed' , ( completionData ) => {
160- if ( completionData . runId === runId ) {
161- setRunningRecordingName ( '' ) ;
162- setCurrentInterpretationLog ( '' ) ;
163- setRerenderRuns ( true ) ;
164-
165- setQueuedRuns ( prev => {
166- const newSet = new Set ( prev ) ;
167- newSet . delete ( runId ) ;
168- return newSet ;
169- } ) ;
170-
171- const robotName = completionData . robotName || runningRecordingName ;
172-
173- if ( completionData . status === 'success' ) {
174- notify ( 'success' , t ( 'main_page.notifications.interpretation_success' , { name : robotName } ) ) ;
175- } else {
176- notify ( 'error' , t ( 'main_page.notifications.interpretation_failed' , { name : robotName } ) ) ;
177- }
178-
179- queueSocket . disconnect ( ) ;
180- }
181- } ) ;
182-
183- setSockets ( sockets => [ ...sockets , queueSocket ] ) ;
184-
185145 notify ( 'info' , `Run queued: ${ runningRecordingName } ` ) ;
186146 } else {
187147 const socket = io ( `${ apiUrl } /${ browserId } ` , {
@@ -245,6 +205,36 @@ export const MainPage = ({ handleEditRecording, initialContent }: MainPageProps)
245205 return message === 'success' ;
246206 }
247207
208+ useEffect ( ( ) => {
209+ if ( user ?. id ) {
210+ const handleRunCompleted = ( completionData : any ) => {
211+ setRerenderRuns ( true ) ;
212+
213+ if ( queuedRuns . has ( completionData . runId ) ) {
214+ setQueuedRuns ( prev => {
215+ const newSet = new Set ( prev ) ;
216+ newSet . delete ( completionData . runId ) ;
217+ return newSet ;
218+ } ) ;
219+ }
220+
221+ const robotName = completionData . robotName || 'Unknown Robot' ;
222+
223+ if ( completionData . status === 'success' ) {
224+ notify ( 'success' , t ( 'main_page.notifications.interpretation_success' , { name : robotName } ) ) ;
225+ } else {
226+ notify ( 'error' , t ( 'main_page.notifications.interpretation_failed' , { name : robotName } ) ) ;
227+ }
228+ } ;
229+
230+ connectToQueueSocket ( user . id , handleRunCompleted ) ;
231+
232+ return ( ) => {
233+ disconnectQueueSocket ( ) ;
234+ } ;
235+ }
236+ } , [ user ?. id , connectToQueueSocket , disconnectQueueSocket , t , setRerenderRuns , queuedRuns , setQueuedRuns ] ) ;
237+
248238 const DisplayContent = ( ) => {
249239 switch ( content ) {
250240 case 'robots' :
0 commit comments