@@ -53,6 +53,7 @@ export const MainPage = ({ handleEditRecording, initialContent }: MainPageProps)
5353 if ( response ) {
5454 notify ( 'success' , t ( 'main_page.notifications.abort_success' , { name : runningRecordingName } ) ) ;
5555 await stopRecording ( ids . browserId ) ;
56+ localStorage . removeItem ( 'runningRobot' ) ;
5657 } else {
5758 notify ( 'error' , t ( 'main_page.notifications.abort_failed' , { name : runningRecordingName } ) ) ;
5859 }
@@ -91,20 +92,29 @@ export const MainPage = ({ handleEditRecording, initialContent }: MainPageProps)
9192 const handleRunRecording = useCallback ( ( settings : RunSettings ) => {
9293 createRunForStoredRecording ( runningRecordingId , settings ) . then ( ( { browserId, runId } : CreateRunResponse ) => {
9394 setIds ( { browserId, runId } ) ;
95+
96+ localStorage . setItem ( 'runningRobot' , JSON . stringify ( {
97+ browserId,
98+ runId,
99+ recordingName : runningRecordingName
100+ } ) ) ;
101+
94102 const socket =
95103 io ( `${ apiUrl } /${ browserId } ` , {
96104 transports : [ "websocket" ] ,
97105 rejectUnauthorized : false
98106 } ) ;
99107 setSockets ( sockets => [ ...sockets , socket ] ) ;
108+
100109 socket . on ( 'debugMessage' , debugMessageHandler ) ;
101-
102110 socket . on ( 'run-completed' , ( status ) => {
103111 if ( status === 'success' ) {
104112 notify ( 'success' , t ( 'main_page.notifications.interpretation_success' , { name : runningRecordingName } ) ) ;
105113 } else {
106114 notify ( 'error' , t ( 'main_page.notifications.interpretation_failed' , { name : runningRecordingName } ) ) ;
107115 }
116+
117+ localStorage . removeItem ( 'runningRobot' ) ;
108118 setRunningRecordingName ( '' ) ;
109119 setCurrentInterpretationLog ( '' ) ;
110120 setRerenderRuns ( true ) ;
@@ -121,7 +131,52 @@ export const MainPage = ({ handleEditRecording, initialContent }: MainPageProps)
121131 socket . off ( 'debugMessage' , debugMessageHandler ) ;
122132 socket . off ( 'run-completed' ) ;
123133 }
124- } , [ runningRecordingName , sockets , ids , debugMessageHandler ] )
134+ } , [ runningRecordingName , sockets , ids , notify , debugMessageHandler ] )
135+
136+ useEffect ( ( ) => {
137+ const storedRobotInfo = localStorage . getItem ( 'runningRobot' ) ;
138+
139+ if ( storedRobotInfo ) {
140+ try {
141+ const { browserId, runId, recordingName } = JSON . parse ( storedRobotInfo ) ;
142+
143+ setIds ( { browserId, runId } ) ;
144+ setRunningRecordingName ( recordingName ) ;
145+ setContent ( 'runs' ) ;
146+
147+ const socket = io ( `${ apiUrl } /${ browserId } ` , {
148+ transports : [ "websocket" ] ,
149+ rejectUnauthorized : false
150+ } ) ;
151+
152+ socket . on ( 'debugMessage' , debugMessageHandler ) ;
153+ socket . on ( 'run-completed' , ( status ) => {
154+ if ( status === 'success' ) {
155+ notify ( 'success' , t ( 'main_page.notifications.interpretation_success' , { name : recordingName } ) ) ;
156+ } else {
157+ notify ( 'error' , t ( 'main_page.notifications.interpretation_failed' , { name : recordingName } ) ) ;
158+ }
159+
160+ localStorage . removeItem ( 'runningRobot' ) ;
161+ setRunningRecordingName ( '' ) ;
162+ setCurrentInterpretationLog ( '' ) ;
163+ setRerenderRuns ( true ) ;
164+ } ) ;
165+
166+ setSockets ( prevSockets => [ ...prevSockets , socket ] ) ;
167+ } catch ( error ) {
168+ console . error ( 'Error restoring robot state:' , error ) ;
169+ localStorage . removeItem ( 'runningRobot' ) ;
170+ }
171+ }
172+
173+ return ( ) => {
174+ sockets . forEach ( socket => {
175+ socket . off ( 'debugMessage' , debugMessageHandler ) ;
176+ socket . off ( 'run-completed' ) ;
177+ } ) ;
178+ } ;
179+ } , [ ] ) ;
125180
126181 const handleScheduleRecording = ( settings : ScheduleSettings ) => {
127182 scheduleStoredRecording ( runningRecordingId , settings )
0 commit comments