@@ -171,54 +171,82 @@ interface Credentials {
171171 [ key : string ] : CredentialInfo ;
172172}
173173
174- function updateTypeActionsInWorkflow ( workflow : any [ ] , credentials : Credentials ) {
174+ function handleWorkflowActions ( workflow : any [ ] , credentials : Credentials ) {
175175 return workflow . map ( step => {
176176 if ( ! step . what ) return step ;
177177
178- const indicesToRemove = new Set < number > ( ) ;
179- step . what . forEach ( ( action : any , index : number ) => {
180- if ( ! action . action || ! action . args ?. [ 0 ] ) return ;
178+ const newWhat : any [ ] = [ ] ;
179+ const processedSelectors = new Set < string > ( ) ;
180+
181+ for ( let i = 0 ; i < step . what . length ; i ++ ) {
182+ const action = step . what [ i ] ;
181183
182- if ( ( action . action === 'type' || action . action === 'press' ) && credentials [ action . args [ 0 ] ] ) {
183- indicesToRemove . add ( index ) ;
184-
185- if ( step . what [ index + 1 ] ?. action === 'waitForLoadState' ) {
186- indicesToRemove . add ( index + 1 ) ;
187- }
184+ if ( ! action ?. action || ! action ?. args ?. [ 0 ] ) {
185+ newWhat . push ( action ) ;
186+ continue ;
188187 }
189- } ) ;
190188
191- const filteredWhat = step . what . filter ( ( _ : any , index : number ) => ! indicesToRemove . has ( index ) ) ;
189+ const selector = action . args [ 0 ] ;
190+ const credential = credentials [ selector ] ;
192191
193- Object . entries ( credentials ) . forEach ( ( [ selector , credentialInfo ] ) => {
194- const clickIndex = filteredWhat . findIndex ( ( action : any ) =>
195- action . action === 'click' && action . args ?. [ 0 ] === selector
196- ) ;
192+ if ( ! credential ) {
193+ newWhat . push ( action ) ;
194+ continue ;
195+ }
197196
198- if ( clickIndex !== - 1 ) {
199- const chars = credentialInfo . value . split ( '' ) ;
197+ if ( action . action === 'click' ) {
198+ newWhat . push ( action ) ;
200199
201- chars . forEach ( ( char , i ) => {
202- filteredWhat . splice ( clickIndex + 1 + ( i * 2 ) , 0 , {
200+ if ( ! processedSelectors . has ( selector ) &&
201+ i + 1 < step . what . length &&
202+ ( step . what [ i + 1 ] . action === 'type' || step . what [ i + 1 ] . action === 'press' ) ) {
203+
204+ newWhat . push ( {
203205 action : 'type' ,
204- args : [
205- selector ,
206- encrypt ( char ) ,
207- credentialInfo . type
208- ]
206+ args : [ selector , encrypt ( credential . value ) , credential . type ]
209207 } ) ;
210208
211- filteredWhat . splice ( clickIndex + 2 + ( i * 2 ) , 0 , {
209+ newWhat . push ( {
212210 action : 'waitForLoadState' ,
213211 args : [ 'networkidle' ]
214212 } ) ;
213+
214+ processedSelectors . add ( selector ) ;
215+
216+ while ( i + 1 < step . what . length &&
217+ ( step . what [ i + 1 ] . action === 'type' ||
218+ step . what [ i + 1 ] . action === 'press' ||
219+ step . what [ i + 1 ] . action === 'waitForLoadState' ) ) {
220+ i ++ ;
221+ }
222+ }
223+ } else if ( ( action . action === 'type' || action . action === 'press' ) &&
224+ ! processedSelectors . has ( selector ) ) {
225+ newWhat . push ( {
226+ action : 'type' ,
227+ args : [ selector , encrypt ( credential . value ) , credential . type ]
215228 } ) ;
229+
230+ newWhat . push ( {
231+ action : 'waitForLoadState' ,
232+ args : [ 'networkidle' ]
233+ } ) ;
234+
235+ processedSelectors . add ( selector ) ;
236+
237+ // Skip subsequent type/press/waitForLoadState actions for this selector
238+ while ( i + 1 < step . what . length &&
239+ ( step . what [ i + 1 ] . action === 'type' ||
240+ step . what [ i + 1 ] . action === 'press' ||
241+ step . what [ i + 1 ] . action === 'waitForLoadState' ) ) {
242+ i ++ ;
243+ }
216244 }
217- } ) ;
245+ }
218246
219247 return {
220248 ...step ,
221- what : filteredWhat
249+ what : newWhat
222250 } ;
223251 } ) ;
224252}
@@ -251,7 +279,7 @@ router.put('/recordings/:id', requireSignIn, async (req: AuthenticatedRequest, r
251279 let workflow = [ ...robot . recording . workflow ] ; // Create a copy of the workflow
252280
253281 if ( credentials ) {
254- workflow = updateTypeActionsInWorkflow ( workflow , credentials ) ;
282+ workflow = handleWorkflowActions ( workflow , credentials ) ;
255283 }
256284
257285 // Update the limit
@@ -289,9 +317,23 @@ router.put('/recordings/:id', requireSignIn, async (req: AuthenticatedRequest, r
289317 }
290318 }
291319
292- robot . set ( 'recording' , { ...robot . recording , workflow } ) ;
320+ const updates : any = {
321+ recording : {
322+ ...robot . recording ,
323+ workflow
324+ }
325+ } ;
293326
294- await robot . save ( ) ;
327+ if ( name ) {
328+ updates . recording_meta = {
329+ ...robot . recording_meta ,
330+ name
331+ } ;
332+ }
333+
334+ await Robot . update ( updates , {
335+ where : { 'recording_meta.id' : id }
336+ } ) ;
295337
296338 const updatedRobot = await Robot . findOne ( { where : { 'recording_meta.id' : id } } ) ;
297339
0 commit comments