2024-08-06 02:33:39 +05:30
|
|
|
import React, { useState, useCallback } from 'react';
|
|
|
|
|
import { Button, Paper, Box, TextField } from "@mui/material";
|
2024-08-06 06:04:22 +05:30
|
|
|
import EditIcon from '@mui/icons-material/Edit';
|
|
|
|
|
import TextFieldsIcon from '@mui/icons-material/TextFields';
|
2024-08-06 06:13:13 +05:30
|
|
|
import DocumentScannerIcon from '@mui/icons-material/DocumentScanner';
|
2024-06-24 22:42:22 +05:30
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { SimpleBox } from "../atoms/Box";
|
|
|
|
|
import Typography from "@mui/material/Typography";
|
|
|
|
|
import { useGlobalInfoStore } from "../../context/globalInfo";
|
2024-07-24 20:25:22 +05:30
|
|
|
import { useActionContext } from '../../context/browserActions';
|
2024-08-10 04:46:09 +05:30
|
|
|
import { useBrowserSteps, ListStep, TextStep, SelectorObject } from '../../context/browserSteps';
|
2024-07-31 04:47:00 +05:30
|
|
|
import { useSocketStore } from '../../context/socket';
|
2024-08-05 23:17:11 +05:30
|
|
|
import { ScreenshotSettings } from '../../shared/types';
|
2024-08-06 06:04:22 +05:30
|
|
|
import InputAdornment from '@mui/material/InputAdornment';
|
|
|
|
|
|
2024-06-24 22:42:22 +05:30
|
|
|
|
2024-08-06 02:31:36 +05:30
|
|
|
export const RightSidePanel = () => {
|
2024-08-06 02:26:22 +05:30
|
|
|
const [textLabels, setTextLabels] = useState<{ [id: number]: string }>({});
|
2024-07-26 23:24:41 +05:30
|
|
|
const [errors, setErrors] = useState<{ [id: number]: string }>({});
|
2024-08-06 02:30:07 +05:30
|
|
|
const [confirmedTextSteps, setConfirmedTextSteps] = useState<{ [id: number]: boolean }>({});
|
2024-08-06 05:05:50 +05:30
|
|
|
|
2024-08-06 05:16:12 +05:30
|
|
|
const { lastAction, notify } = useGlobalInfoStore();
|
2024-08-08 00:44:29 +05:30
|
|
|
const { getText, startGetText, stopGetText, getScreenshot, startGetScreenshot, stopGetScreenshot, getList, startGetList, stopGetList } = useActionContext();
|
2024-08-06 03:16:09 +05:30
|
|
|
const { browserSteps, updateBrowserTextStepLabel, deleteBrowserStep, addScreenshotStep } = useBrowserSteps();
|
2024-07-31 04:47:00 +05:30
|
|
|
const { socket } = useSocketStore();
|
2024-06-24 22:42:22 +05:30
|
|
|
|
2024-08-06 02:30:07 +05:30
|
|
|
const handleTextLabelChange = (id: number, label: string) => {
|
2024-08-06 02:26:22 +05:30
|
|
|
setTextLabels(prevLabels => ({ ...prevLabels, [id]: label }));
|
2024-08-06 05:05:50 +05:30
|
|
|
if (!label.trim()) {
|
|
|
|
|
setErrors(prevErrors => ({ ...prevErrors, [id]: 'Label cannot be empty' }));
|
|
|
|
|
} else {
|
|
|
|
|
setErrors(prevErrors => ({ ...prevErrors, [id]: '' }));
|
|
|
|
|
}
|
2024-07-26 23:25:03 +05:30
|
|
|
};
|
2024-07-26 23:07:54 +05:30
|
|
|
|
2024-08-06 02:30:07 +05:30
|
|
|
const handleTextStepConfirm = (id: number) => {
|
2024-08-06 02:26:22 +05:30
|
|
|
const label = textLabels[id]?.trim();
|
2024-07-26 23:24:41 +05:30
|
|
|
if (label) {
|
2024-08-06 03:12:07 +05:30
|
|
|
updateBrowserTextStepLabel(id, label);
|
2024-08-06 02:30:07 +05:30
|
|
|
setConfirmedTextSteps(prev => ({ ...prev, [id]: true }));
|
2024-07-26 23:24:41 +05:30
|
|
|
} else {
|
2024-07-26 23:25:03 +05:30
|
|
|
setErrors(prevErrors => ({ ...prevErrors, [id]: 'Label cannot be empty' }));
|
2024-07-26 23:07:54 +05:30
|
|
|
}
|
2024-07-26 23:25:03 +05:30
|
|
|
};
|
2024-07-26 23:07:54 +05:30
|
|
|
|
2024-08-06 02:30:07 +05:30
|
|
|
const handleTextStepDiscard = (id: number) => {
|
2024-07-26 23:07:54 +05:30
|
|
|
deleteBrowserStep(id);
|
2024-08-06 02:26:22 +05:30
|
|
|
setTextLabels(prevLabels => {
|
2024-07-26 23:25:03 +05:30
|
|
|
const { [id]: _, ...rest } = prevLabels;
|
|
|
|
|
return rest;
|
2024-07-26 23:24:41 +05:30
|
|
|
});
|
|
|
|
|
setErrors(prevErrors => {
|
2024-07-26 23:25:03 +05:30
|
|
|
const { [id]: _, ...rest } = prevErrors;
|
|
|
|
|
return rest;
|
2024-07-26 23:24:41 +05:30
|
|
|
});
|
2024-07-26 23:25:03 +05:30
|
|
|
};
|
2024-07-26 23:07:54 +05:30
|
|
|
|
2024-08-06 02:30:07 +05:30
|
|
|
const getTextSettingsObject = useCallback(() => {
|
2024-08-06 05:05:50 +05:30
|
|
|
const settings: Record<string, { selector: string; tag?: string;[key: string]: any }> = {};
|
2024-07-31 04:47:00 +05:30
|
|
|
browserSteps.forEach(step => {
|
2024-08-06 03:25:52 +05:30
|
|
|
if (step.type === 'text' && step.label && step.selectorObj?.selector) {
|
|
|
|
|
settings[step.label] = step.selectorObj;
|
|
|
|
|
}
|
2024-07-31 04:47:00 +05:30
|
|
|
});
|
|
|
|
|
return settings;
|
2024-08-06 03:25:52 +05:30
|
|
|
}, [browserSteps]);
|
2024-08-06 03:12:07 +05:30
|
|
|
|
2024-08-06 05:05:50 +05:30
|
|
|
|
2024-08-06 03:25:52 +05:30
|
|
|
const stopCaptureAndEmitGetTextSettings = useCallback(() => {
|
2024-08-06 05:12:08 +05:30
|
|
|
const hasUnconfirmedTextSteps = browserSteps.some(step => step.type === 'text' && !confirmedTextSteps[step.id]);
|
|
|
|
|
if (hasUnconfirmedTextSteps) {
|
2024-08-06 05:16:12 +05:30
|
|
|
notify('error', 'Please confirm no labels are empty');
|
2024-08-06 05:12:08 +05:30
|
|
|
return;
|
|
|
|
|
}
|
2024-08-06 03:25:52 +05:30
|
|
|
stopGetText();
|
|
|
|
|
const settings = getTextSettingsObject();
|
|
|
|
|
const hasTextSteps = browserSteps.some(step => step.type === 'text');
|
|
|
|
|
if (hasTextSteps) {
|
2024-08-04 20:57:40 +05:30
|
|
|
socket?.emit('action', { action: 'scrapeSchema', settings });
|
2024-08-06 03:25:52 +05:30
|
|
|
}
|
2024-08-06 05:12:08 +05:30
|
|
|
}, [stopGetText, getTextSettingsObject, socket, browserSteps, confirmedTextSteps]);
|
|
|
|
|
|
2024-08-09 10:32:48 +05:30
|
|
|
|
|
|
|
|
const getListSettingsObject = useCallback(() => {
|
2024-08-10 05:06:43 +05:30
|
|
|
const settings: Record<string, { listSelector: string; fields: Record<string, { selector: string; tag?: string;[key: string]: any }> }> = {};
|
2024-08-10 04:46:09 +05:30
|
|
|
|
|
|
|
|
browserSteps.forEach(step => {
|
2024-08-10 05:06:43 +05:30
|
|
|
if (step.type === 'list' && step.listSelector && Object.keys(step.fields).length > 0) {
|
|
|
|
|
const fields: Record<string, { selector: string; tag?: string;[key: string]: any }> = {};
|
|
|
|
|
Object.entries(step.fields).forEach(([label, field]) => {
|
|
|
|
|
if (field.selectorObj?.selector) {
|
|
|
|
|
fields[label] = {
|
|
|
|
|
selector: field.selectorObj.selector,
|
|
|
|
|
tag: field.selectorObj.tag,
|
|
|
|
|
attribute: field.selectorObj.attribute
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
settings[step.listSelector] = {
|
|
|
|
|
listSelector: step.listSelector,
|
|
|
|
|
fields: fields
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-08-10 04:46:09 +05:30
|
|
|
});
|
2024-08-09 10:32:48 +05:30
|
|
|
|
2024-08-10 04:46:09 +05:30
|
|
|
return settings;
|
2024-08-10 05:06:43 +05:30
|
|
|
}, [browserSteps]);
|
2024-08-10 04:46:09 +05:30
|
|
|
|
|
|
|
|
|
2024-08-10 05:04:50 +05:30
|
|
|
const stopCaptureAndEmitGetListSettings = useCallback(() => {
|
2024-08-10 05:06:43 +05:30
|
|
|
stopGetList();
|
|
|
|
|
const settings = getListSettingsObject();
|
|
|
|
|
if (settings) {
|
|
|
|
|
socket?.emit('action', { action: 'scrapeList', settings });
|
2024-08-10 05:04:50 +05:30
|
|
|
} else {
|
2024-08-10 05:06:43 +05:30
|
|
|
notify('error', 'Unable to create list settings. Make sure you have defined a field for the list.');
|
|
|
|
|
}
|
|
|
|
|
}, [stopGetList, getListSettingsObject, socket, notify]);
|
2024-08-09 10:32:48 +05:30
|
|
|
|
|
|
|
|
// const handleListFieldChange = (stepId: number, key: 'label' | 'data', value: string) => {
|
|
|
|
|
// updateListStepField(stepId, key, value);
|
|
|
|
|
// };
|
|
|
|
|
|
2024-08-05 23:17:11 +05:30
|
|
|
const captureScreenshot = (fullPage: boolean) => {
|
|
|
|
|
const screenshotSettings: ScreenshotSettings = {
|
|
|
|
|
fullPage,
|
2024-08-05 23:24:11 +05:30
|
|
|
type: 'png',
|
|
|
|
|
timeout: 30000,
|
|
|
|
|
animations: 'allow',
|
|
|
|
|
caret: 'hide',
|
|
|
|
|
scale: 'device',
|
2024-08-05 23:17:11 +05:30
|
|
|
};
|
2024-08-05 23:24:11 +05:30
|
|
|
socket?.emit('action', { action: 'screenshot', settings: screenshotSettings });
|
2024-08-06 03:16:09 +05:30
|
|
|
addScreenshotStep(fullPage);
|
2024-08-06 05:32:05 +05:30
|
|
|
stopGetScreenshot();
|
2024-08-05 23:02:01 +05:30
|
|
|
};
|
|
|
|
|
|
2024-06-24 22:42:22 +05:30
|
|
|
return (
|
2024-07-31 04:47:00 +05:30
|
|
|
<Paper variant="outlined" sx={{ height: '100%', width: '100%', backgroundColor: 'white', alignItems: "center" }}>
|
2024-06-24 22:42:22 +05:30
|
|
|
<SimpleBox height={60} width='100%' background='lightGray' radius='0%'>
|
2024-07-31 04:47:00 +05:30
|
|
|
<Typography sx={{ padding: '10px' }}>Last action: {` ${lastAction}`}</Typography>
|
2024-06-24 22:42:22 +05:30
|
|
|
</SimpleBox>
|
|
|
|
|
|
2024-07-25 01:56:45 +05:30
|
|
|
<Box display="flex" flexDirection="column" gap={2} style={{ margin: '15px' }}>
|
2024-08-09 19:19:44 +05:30
|
|
|
{!getText && !getScreenshot && !getList && <Button variant="contained" onClick={startGetList}>Capture List</Button>}
|
|
|
|
|
{getList &&
|
2024-08-08 02:19:38 +05:30
|
|
|
<>
|
|
|
|
|
<Box display="flex" justifyContent="space-between" gap={2} style={{ margin: '15px' }}>
|
2024-08-10 05:04:50 +05:30
|
|
|
<Button variant="outlined" onClick={stopCaptureAndEmitGetListSettings}>Confirm</Button>
|
2024-08-08 02:19:38 +05:30
|
|
|
<Button variant="outlined" color="error" onClick={stopGetList}>Discard</Button>
|
|
|
|
|
</Box>
|
|
|
|
|
</>
|
|
|
|
|
}
|
2024-08-08 02:18:39 +05:30
|
|
|
|
2024-08-08 02:14:39 +05:30
|
|
|
{!getText && !getScreenshot && !getList && <Button variant="contained" onClick={startGetText}>Capture Text</Button>}
|
2024-08-06 06:04:22 +05:30
|
|
|
{getText &&
|
|
|
|
|
<>
|
|
|
|
|
<Box display="flex" justifyContent="space-between" gap={2} style={{ margin: '15px' }}>
|
|
|
|
|
<Button variant="outlined" onClick={stopCaptureAndEmitGetTextSettings}>Confirm</Button>
|
|
|
|
|
<Button variant="outlined" color="error" onClick={stopGetText}>Discard</Button>
|
|
|
|
|
</Box>
|
|
|
|
|
</>
|
2024-08-06 05:22:47 +05:30
|
|
|
}
|
|
|
|
|
|
2024-08-08 02:14:39 +05:30
|
|
|
{!getText && !getScreenshot && !getList && <Button variant="contained" onClick={startGetScreenshot}>Capture Screenshot</Button>}
|
2024-08-05 23:02:01 +05:30
|
|
|
{getScreenshot && (
|
|
|
|
|
<Box display="flex" flexDirection="column" gap={2}>
|
2024-08-05 23:24:11 +05:30
|
|
|
<Button variant="contained" onClick={() => captureScreenshot(true)}>Capture Fullpage</Button>
|
|
|
|
|
<Button variant="contained" onClick={() => captureScreenshot(false)}>Capture Visible Part</Button>
|
2024-08-06 05:05:50 +05:30
|
|
|
<Button variant="outlined" color="error" onClick={stopGetScreenshot}>Discard</Button>
|
2024-08-05 23:02:01 +05:30
|
|
|
</Box>
|
|
|
|
|
)}
|
2024-07-25 01:56:31 +05:30
|
|
|
</Box>
|
2024-08-06 02:23:33 +05:30
|
|
|
|
2024-08-06 05:06:04 +05:30
|
|
|
<Box>
|
|
|
|
|
{browserSteps.map(step => (
|
|
|
|
|
<Box key={step.id} sx={{ boxShadow: 5, padding: '10px', margin: '10px', borderRadius: '4px' }}>
|
|
|
|
|
{
|
2024-08-10 07:07:49 +05:30
|
|
|
step.type === 'text' && (
|
2024-08-06 05:06:04 +05:30
|
|
|
<>
|
|
|
|
|
<TextField
|
|
|
|
|
label="Label"
|
|
|
|
|
value={textLabels[step.id] || step.label || ''}
|
|
|
|
|
onChange={(e) => handleTextLabelChange(step.id, e.target.value)}
|
|
|
|
|
fullWidth
|
|
|
|
|
margin="normal"
|
|
|
|
|
error={!!errors[step.id]}
|
|
|
|
|
helperText={errors[step.id]}
|
2024-08-06 06:13:24 +05:30
|
|
|
InputProps={{
|
2024-08-06 06:04:22 +05:30
|
|
|
readOnly: confirmedTextSteps[step.id],
|
|
|
|
|
startAdornment: (
|
|
|
|
|
<InputAdornment position="start">
|
|
|
|
|
<EditIcon />
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
)
|
|
|
|
|
}}
|
2024-08-06 05:06:04 +05:30
|
|
|
/>
|
|
|
|
|
<TextField
|
|
|
|
|
label="Data"
|
|
|
|
|
value={step.data}
|
|
|
|
|
fullWidth
|
|
|
|
|
margin="normal"
|
2024-08-06 06:13:24 +05:30
|
|
|
InputProps={{
|
2024-08-06 06:04:22 +05:30
|
|
|
readOnly: confirmedTextSteps[step.id],
|
|
|
|
|
startAdornment: (
|
|
|
|
|
<InputAdornment position="start">
|
|
|
|
|
<TextFieldsIcon />
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
)
|
|
|
|
|
}}
|
2024-08-06 05:06:04 +05:30
|
|
|
/>
|
|
|
|
|
{!confirmedTextSteps[step.id] && (
|
|
|
|
|
<Box display="flex" justifyContent="space-between" gap={2}>
|
|
|
|
|
<Button variant="contained" onClick={() => handleTextStepConfirm(step.id)} disabled={!textLabels[step.id]?.trim()}>Confirm</Button>
|
|
|
|
|
<Button variant="contained" onClick={() => handleTextStepDiscard(step.id)}>Discard</Button>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2024-08-10 07:07:49 +05:30
|
|
|
)}
|
2024-08-10 07:42:41 +05:30
|
|
|
{step.type === 'screenshot' && (
|
2024-08-10 07:07:49 +05:30
|
|
|
<Box display="flex" alignItems="center">
|
|
|
|
|
<DocumentScannerIcon sx={{ mr: 1 }} />
|
|
|
|
|
<Typography>
|
|
|
|
|
{`Take ${step.fullPage ? 'Fullpage' : 'Visible Part'} Screenshot`}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
2024-08-10 07:42:41 +05:30
|
|
|
)}
|
|
|
|
|
{step.type === 'list' && (
|
2024-08-10 07:07:49 +05:30
|
|
|
<>
|
|
|
|
|
<Typography>List Selected Successfully</Typography>
|
|
|
|
|
{Object.entries(step.fields).map(([key, field]) => (
|
|
|
|
|
<Box key={key}>
|
|
|
|
|
<TextField
|
|
|
|
|
label="Field Label"
|
|
|
|
|
value={field.label || ''}
|
2024-08-10 07:42:41 +05:30
|
|
|
onChange={() => { }}
|
2024-08-10 07:07:49 +05:30
|
|
|
fullWidth
|
|
|
|
|
margin="normal"
|
|
|
|
|
InputProps={{
|
|
|
|
|
startAdornment: (
|
|
|
|
|
<InputAdornment position="start">
|
|
|
|
|
<EditIcon />
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<TextField
|
|
|
|
|
label="Field Data"
|
|
|
|
|
value={field.data || ''}
|
|
|
|
|
fullWidth
|
|
|
|
|
margin="normal"
|
|
|
|
|
InputProps={{
|
|
|
|
|
readOnly: true,
|
|
|
|
|
startAdornment: (
|
|
|
|
|
<InputAdornment position="start">
|
|
|
|
|
<TextFieldsIcon />
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2024-08-06 06:13:24 +05:30
|
|
|
</Box>
|
2024-08-10 07:07:49 +05:30
|
|
|
))}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2024-07-26 23:25:03 +05:30
|
|
|
</Box>
|
2024-08-06 05:06:04 +05:30
|
|
|
))}
|
|
|
|
|
</Box>
|
2024-06-24 22:42:22 +05:30
|
|
|
</Paper>
|
|
|
|
|
);
|
2024-08-10 07:07:49 +05:30
|
|
|
};
|