From 64ef2532e2f698bda1335a44c305edd6df47a41e Mon Sep 17 00:00:00 2001 From: Rohit Date: Wed, 22 Jan 2025 00:39:52 +0530 Subject: [PATCH] feat: extract and set initial credentials --- src/components/robot/RobotEdit.tsx | 42 +++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/src/components/robot/RobotEdit.tsx b/src/components/robot/RobotEdit.tsx index f56f0869..d987e2c0 100644 --- a/src/components/robot/RobotEdit.tsx +++ b/src/components/robot/RobotEdit.tsx @@ -93,11 +93,7 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin const selectors = findCredentialSelectors(robot.recording.workflow); setCredentialSelectors(selectors); - // Initialize credentials state - const initialCredentials: Record = {}; - selectors.forEach(selector => { - initialCredentials[selector] = ''; - }); + const initialCredentials = extractInitialCredentials(robot.recording.workflow); setCredentials(initialCredentials); } }, [robot]); @@ -108,7 +104,7 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin workflow?.forEach(step => { step.what?.forEach(action => { if ( - (action.action === 'type' || action.action === 'press') && + (action.action === 'type') && action.args && action.args[0] && typeof action.args[0] === 'string' @@ -121,9 +117,43 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin return Array.from(selectors); }; + const extractInitialCredentials = (workflow: any[]): Record => { + const credentials: Record = {}; + + const isPrintableCharacter = (char: string): boolean => { + return char.length === 1 && !!char.match(/^[\x20-\x7E]$/); + }; + + workflow.forEach(step => { + if (!step.what) return; + + step.what.forEach((action: any) => { + if ( + (action.action === 'type' || action.action === 'press') && + action.args?.length >= 2 && + typeof action.args[1] === 'string' + ) { + let currentSelector: string = action.args[0]; + let character: string = action.args[1]; + + if (!credentials.hasOwnProperty(currentSelector)) { + credentials[currentSelector] = ''; + } + + if (isPrintableCharacter(character)) { + credentials[currentSelector] += character; + } + } + }); + }); + + return credentials; + }; + const getRobot = async () => { if (recordingId) { const robot = await getStoredRecording(recordingId); + console.log('Robot:', robot); setRobot(robot); } else { notify('error', t('robot_edit.notifications.update_failed'));