Merge pull request #579 from getmaxun/multiple-limit
feat: support for editing multiple capture list limits
This commit is contained in:
@@ -254,11 +254,11 @@ function handleWorkflowActions(workflow: any[], credentials: Credentials) {
|
|||||||
router.put('/recordings/:id', requireSignIn, async (req: AuthenticatedRequest, res) => {
|
router.put('/recordings/:id', requireSignIn, async (req: AuthenticatedRequest, res) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const { name, limit, credentials, targetUrl } = req.body;
|
const { name, limits, credentials, targetUrl } = req.body;
|
||||||
|
|
||||||
// Validate input
|
// Validate input
|
||||||
if (!name && limit === undefined && !targetUrl) {
|
if (!name && !limits && !credentials && !targetUrl) {
|
||||||
return res.status(400).json({ error: 'Either "name", "limit" or "target_url" must be provided.' });
|
return res.status(400).json({ error: 'Either "name", "limits", "credentials" or "target_url" must be provided.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the robot by ID
|
// Fetch the robot by ID
|
||||||
@@ -274,22 +274,26 @@ router.put('/recordings/:id', requireSignIn, async (req: AuthenticatedRequest, r
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (targetUrl) {
|
if (targetUrl) {
|
||||||
const updatedWorkflow = robot.recording.workflow.map((step) => {
|
const updatedWorkflow = [...robot.recording.workflow];
|
||||||
if (step.where?.url && step.where.url !== "about:blank") {
|
|
||||||
step.where.url = targetUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
step.what.forEach((action) => {
|
for (let i = updatedWorkflow.length - 1; i >= 0; i--) {
|
||||||
|
const step = updatedWorkflow[i];
|
||||||
|
for (let j = 0; j < step.what.length; j++) {
|
||||||
|
const action = step.what[j];
|
||||||
if (action.action === "goto" && action.args?.length) {
|
if (action.action === "goto" && action.args?.length) {
|
||||||
|
|
||||||
action.args[0] = targetUrl;
|
action.args[0] = targetUrl;
|
||||||
|
if (step.where?.url && step.where.url !== "about:blank") {
|
||||||
|
step.where.url = targetUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
robot.set('recording', { ...robot.recording, workflow: updatedWorkflow });
|
||||||
|
robot.changed('recording', true);
|
||||||
|
i = -1;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
}
|
||||||
return step;
|
|
||||||
});
|
|
||||||
|
|
||||||
robot.set('recording', { ...robot.recording, workflow: updatedWorkflow });
|
|
||||||
robot.changed('recording', true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await robot.save();
|
await robot.save();
|
||||||
@@ -300,38 +304,20 @@ router.put('/recordings/:id', requireSignIn, async (req: AuthenticatedRequest, r
|
|||||||
workflow = handleWorkflowActions(workflow, credentials);
|
workflow = handleWorkflowActions(workflow, credentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the limit
|
if (limits && Array.isArray(limits) && limits.length > 0) {
|
||||||
if (limit !== undefined) {
|
for (const limitInfo of limits) {
|
||||||
// Ensure the workflow structure is valid before updating
|
const { pairIndex, actionIndex, argIndex, limit } = limitInfo;
|
||||||
if (
|
|
||||||
workflow.length > 0 &&
|
const pair = workflow[pairIndex];
|
||||||
workflow[0]?.what?.[0]
|
if (!pair || !pair.what) continue;
|
||||||
) {
|
|
||||||
// Create a new workflow object with the updated limit
|
const action = pair.what[actionIndex];
|
||||||
workflow = workflow.map((step, index) => {
|
if (!action || !action.args) continue;
|
||||||
if (index === 0) { // Assuming you want to update the first step
|
|
||||||
return {
|
const arg = action.args[argIndex];
|
||||||
...step,
|
if (!arg || typeof arg !== 'object') continue;
|
||||||
what: step.what.map((action, actionIndex) => {
|
|
||||||
if (actionIndex === 0) { // Assuming the first action needs updating
|
(arg as { limit: number }).limit = limit;
|
||||||
return {
|
|
||||||
...action,
|
|
||||||
args: (action.args ?? []).map((arg, argIndex) => {
|
|
||||||
if (argIndex === 0) { // Assuming the first argument needs updating
|
|
||||||
return { ...arg, limit };
|
|
||||||
}
|
|
||||||
return arg;
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return action;
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return step;
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return res.status(400).json({ error: 'Invalid workflow structure for updating limit.' });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,12 @@ export const getStoredRecordings = async (): Promise<string[] | null> => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateRecording = async (id: string, data: { name?: string; limit?: number, credentials?: Credentials, targetUrl?: string }): Promise<boolean> => {
|
export const updateRecording = async (id: string, data: {
|
||||||
|
name?: string;
|
||||||
|
limits?: Array<{pairIndex: number, actionIndex: number, argIndex: number, limit: number}>;
|
||||||
|
credentials?: Credentials;
|
||||||
|
targetUrl?: string
|
||||||
|
}): Promise<boolean> => {
|
||||||
try {
|
try {
|
||||||
const response = await axios.put(`${apiUrl}/storage/recordings/${id}`, data);
|
const response = await axios.put(`${apiUrl}/storage/recordings/${id}`, data);
|
||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
|
|||||||
@@ -73,6 +73,13 @@ interface GroupedCredentials {
|
|||||||
others: string[];
|
others: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ScrapeListLimit {
|
||||||
|
pairIndex: number;
|
||||||
|
actionIndex: number;
|
||||||
|
argIndex: number;
|
||||||
|
currentLimit: number;
|
||||||
|
}
|
||||||
|
|
||||||
export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettings }: RobotSettingsProps) => {
|
export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettings }: RobotSettingsProps) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [credentials, setCredentials] = useState<Credentials>({});
|
const [credentials, setCredentials] = useState<Credentials>({});
|
||||||
@@ -85,6 +92,7 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin
|
|||||||
others: []
|
others: []
|
||||||
});
|
});
|
||||||
const [showPasswords, setShowPasswords] = useState<CredentialVisibility>({});
|
const [showPasswords, setShowPasswords] = useState<CredentialVisibility>({});
|
||||||
|
const [scrapeListLimits, setScrapeListLimits] = useState<ScrapeListLimit[]>([]);
|
||||||
|
|
||||||
const isEmailPattern = (value: string): boolean => {
|
const isEmailPattern = (value: string): boolean => {
|
||||||
return value.includes('@');
|
return value.includes('@');
|
||||||
@@ -120,9 +128,36 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin
|
|||||||
const extractedCredentials = extractInitialCredentials(robot.recording.workflow);
|
const extractedCredentials = extractInitialCredentials(robot.recording.workflow);
|
||||||
setCredentials(extractedCredentials);
|
setCredentials(extractedCredentials);
|
||||||
setCredentialGroups(groupCredentialsByType(extractedCredentials));
|
setCredentialGroups(groupCredentialsByType(extractedCredentials));
|
||||||
|
|
||||||
|
findScrapeListLimits(robot.recording.workflow);
|
||||||
}
|
}
|
||||||
}, [robot]);
|
}, [robot]);
|
||||||
|
|
||||||
|
const findScrapeListLimits = (workflow: WhereWhatPair[]) => {
|
||||||
|
const limits: ScrapeListLimit[] = [];
|
||||||
|
|
||||||
|
workflow.forEach((pair, pairIndex) => {
|
||||||
|
if (!pair.what) return;
|
||||||
|
|
||||||
|
pair.what.forEach((action, actionIndex) => {
|
||||||
|
if (action.action === 'scrapeList' && action.args && action.args.length > 0) {
|
||||||
|
// Check if first argument has a limit property
|
||||||
|
const arg = action.args[0];
|
||||||
|
if (arg && typeof arg === 'object' && 'limit' in arg) {
|
||||||
|
limits.push({
|
||||||
|
pairIndex,
|
||||||
|
actionIndex,
|
||||||
|
argIndex: 0,
|
||||||
|
currentLimit: arg.limit
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
setScrapeListLimits(limits);
|
||||||
|
};
|
||||||
|
|
||||||
function extractInitialCredentials(workflow: any[]): Credentials {
|
function extractInitialCredentials(workflow: any[]): Credentials {
|
||||||
const credentials: Credentials = {};
|
const credentials: Credentials = {};
|
||||||
|
|
||||||
@@ -285,20 +320,30 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin
|
|||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLimitChange = (newLimit: number) => {
|
const handleLimitChange = (pairIndex: number, actionIndex: number, argIndex: number, newLimit: number) => {
|
||||||
setRobot((prev) => {
|
setRobot((prev) => {
|
||||||
if (!prev) return prev;
|
if (!prev) return prev;
|
||||||
|
|
||||||
const updatedWorkflow = [...prev.recording.workflow];
|
const updatedWorkflow = [...prev.recording.workflow];
|
||||||
if (
|
if (
|
||||||
updatedWorkflow.length > 0 &&
|
updatedWorkflow.length > pairIndex &&
|
||||||
updatedWorkflow[0]?.what &&
|
updatedWorkflow[pairIndex]?.what &&
|
||||||
updatedWorkflow[0].what.length > 0 &&
|
updatedWorkflow[pairIndex].what.length > actionIndex &&
|
||||||
updatedWorkflow[0].what[0].args &&
|
updatedWorkflow[pairIndex].what[actionIndex].args &&
|
||||||
updatedWorkflow[0].what[0].args.length > 0 &&
|
updatedWorkflow[pairIndex].what[actionIndex].args.length > argIndex
|
||||||
updatedWorkflow[0].what[0].args[0]
|
|
||||||
) {
|
) {
|
||||||
updatedWorkflow[0].what[0].args[0].limit = newLimit;
|
updatedWorkflow[pairIndex].what[actionIndex].args[argIndex].limit = newLimit;
|
||||||
|
|
||||||
|
setScrapeListLimits(prev => {
|
||||||
|
return prev.map(item => {
|
||||||
|
if (item.pairIndex === pairIndex &&
|
||||||
|
item.actionIndex === actionIndex &&
|
||||||
|
item.argIndex === argIndex) {
|
||||||
|
return { ...item, currentLimit: newLimit };
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return { ...prev, recording: { ...prev.recording, workflow: updatedWorkflow } };
|
return { ...prev, recording: { ...prev.recording, workflow: updatedWorkflow } };
|
||||||
@@ -358,9 +403,6 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* <Typography variant="h6" style={{ marginBottom: '20px' }}>
|
|
||||||
{headerText}
|
|
||||||
</Typography> */}
|
|
||||||
{selectors.map((selector, index) => {
|
{selectors.map((selector, index) => {
|
||||||
const isVisible = showPasswords[selector];
|
const isVisible = showPasswords[selector];
|
||||||
|
|
||||||
@@ -393,6 +435,40 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const renderScrapeListLimitFields = () => {
|
||||||
|
if (scrapeListLimits.length === 0) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Typography variant="body1" style={{ marginBottom: '20px' }}>
|
||||||
|
{t('List Limits')}
|
||||||
|
</Typography>
|
||||||
|
|
||||||
|
{scrapeListLimits.map((limitInfo, index) => (
|
||||||
|
<TextField
|
||||||
|
key={`limit-${limitInfo.pairIndex}-${limitInfo.actionIndex}`}
|
||||||
|
label={`${t('List Limit')} ${index + 1}`}
|
||||||
|
type="number"
|
||||||
|
value={limitInfo.currentLimit || ''}
|
||||||
|
onChange={(e) => {
|
||||||
|
const value = parseInt(e.target.value, 10);
|
||||||
|
if (value >= 1) {
|
||||||
|
handleLimitChange(
|
||||||
|
limitInfo.pairIndex,
|
||||||
|
limitInfo.actionIndex,
|
||||||
|
limitInfo.argIndex,
|
||||||
|
value
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
inputProps={{ min: 1 }}
|
||||||
|
style={{ marginBottom: '20px' }}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
if (!robot) return;
|
if (!robot) return;
|
||||||
|
|
||||||
@@ -412,7 +488,12 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin
|
|||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
name: robot.recording_meta.name,
|
name: robot.recording_meta.name,
|
||||||
limit: robot.recording.workflow[0]?.what[0]?.args?.[0]?.limit,
|
limits: scrapeListLimits.map(limit => ({
|
||||||
|
pairIndex: limit.pairIndex,
|
||||||
|
actionIndex: limit.actionIndex,
|
||||||
|
argIndex: limit.argIndex,
|
||||||
|
limit: limit.currentLimit
|
||||||
|
})),
|
||||||
credentials: credentialsForPayload,
|
credentials: credentialsForPayload,
|
||||||
targetUrl: targetUrl,
|
targetUrl: targetUrl,
|
||||||
};
|
};
|
||||||
@@ -468,21 +549,7 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin
|
|||||||
style={{ marginBottom: '20px' }}
|
style={{ marginBottom: '20px' }}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{robot.recording.workflow?.[0]?.what?.[0]?.args?.[0]?.limit !== undefined && (
|
{renderScrapeListLimitFields()}
|
||||||
<TextField
|
|
||||||
label={t('robot_edit.robot_limit')}
|
|
||||||
type="number"
|
|
||||||
value={robot.recording.workflow[0].what[0].args[0].limit || ''}
|
|
||||||
onChange={(e) => {
|
|
||||||
const value = parseInt(e.target.value, 10);
|
|
||||||
if (value >= 1) {
|
|
||||||
handleLimitChange(value);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
inputProps={{ min: 1 }}
|
|
||||||
style={{ marginBottom: '20px' }}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{(Object.keys(credentials).length > 0) && (
|
{(Object.keys(credentials).length > 0) && (
|
||||||
<>
|
<>
|
||||||
|
|||||||
Reference in New Issue
Block a user