Files
parcer/src/components/organisms/RightSidePanel.tsx

425 lines
17 KiB
TypeScript
Raw Normal View History

2024-09-04 05:38:24 +05:30
import React, { useState, useCallback } from 'react';
2024-08-06 02:33:39 +05:30
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 { SimpleBox } from "../atoms/Box";
import Typography from "@mui/material/Typography";
import { useGlobalInfoStore } from "../../context/globalInfo";
2024-09-08 13:52:01 +05:30
import { PaginationType, useActionContext, LimitType } from '../../context/browserActions';
2024-09-04 05:38:48 +05:30
import { useBrowserSteps } from '../../context/browserSteps';
import { useSocketStore } from '../../context/socket';
import { ScreenshotSettings } from '../../shared/types';
2024-08-06 06:04:22 +05:30
import InputAdornment from '@mui/material/InputAdornment';
2024-09-07 08:38:00 +05:30
import { SidePanelHeader } from '../molecules/SidePanelHeader';
2024-09-08 12:19:11 +05:30
import FormControlLabel from '@mui/material/FormControlLabel';
import FormControl from '@mui/material/FormControl';
import FormLabel from '@mui/material/FormLabel';
import Radio from '@mui/material/Radio';
import RadioGroup from '@mui/material/RadioGroup';
2024-08-06 06:04:22 +05:30
2024-08-11 08:46:48 +05:30
// TODO:
// 1. Handle field label update
// 2. Handle field deletion | confirmation
// 3. Add description for each browser step
// 4. Handle non custom action steps
2024-06-24 22:42:22 +05:30
2024-09-08 07:39:09 +05:30
interface RightSidePanelProps {
onFinishCapture: () => void;
}
export const RightSidePanel: React.FC<RightSidePanelProps> = ({ onFinishCapture }) => {
const [textLabels, setTextLabels] = useState<{ [id: number]: string }>({});
2024-07-26 23:24:41 +05:30
const [errors, setErrors] = useState<{ [id: number]: string }>({});
const [confirmedTextSteps, setConfirmedTextSteps] = useState<{ [id: number]: boolean }>({});
const [showPaginationOptions, setShowPaginationOptions] = useState(false);
2024-09-08 12:19:11 +05:30
const [showLimitOptions, setShowLimitOptions] = useState(false);
const [selectedLimit, setSelectedLimit] = useState<string>('10');
2024-09-08 13:52:01 +05:30
const [captureStage, setCaptureStage] = useState<'initial' | 'pagination' | 'limit' | 'complete'>('initial');
2024-09-01 22:42:34 +05:30
const { lastAction, notify } = useGlobalInfoStore();
2024-09-08 13:52:01 +05:30
const { getText, startGetText, stopGetText, getScreenshot, startGetScreenshot, stopGetScreenshot, paginationMode, getList, startGetList, stopGetList, startPaginationMode, stopPaginationMode, paginationType, updatePaginationType, limitMode, limitType, customLimit, updateLimitType, updateCustomLimit, stopLimitMode, startLimitMode } = useActionContext();
2024-08-06 03:16:09 +05:30
const { browserSteps, updateBrowserTextStepLabel, deleteBrowserStep, addScreenshotStep } = useBrowserSteps();
const { socket } = useSocketStore();
2024-06-24 22:42:22 +05:30
const handleTextLabelChange = (id: number, label: string) => {
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-09-08 12:19:11 +05:30
const handleLimitChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setSelectedLimit(event.target.value);
};
const handleTextStepConfirm = (id: number) => {
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);
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
const handleTextStepDiscard = (id: number) => {
2024-07-26 23:07:54 +05:30
deleteBrowserStep(id);
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
const getTextSettingsObject = useCallback(() => {
2024-08-06 05:05:50 +05:30
const settings: Record<string, { selector: string; tag?: string;[key: string]: any }> = {};
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;
}
});
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(() => {
const hasUnconfirmedTextSteps = browserSteps.some(step => step.type === 'text' && !confirmedTextSteps[step.id]);
if (hasUnconfirmedTextSteps) {
notify('error', 'Please confirm no labels are empty');
return;
}
2024-08-06 03:25:52 +05:30
stopGetText();
const settings = getTextSettingsObject();
const hasTextSteps = browserSteps.some(step => step.type === 'text');
if (hasTextSteps) {
socket?.emit('action', { action: 'scrapeSchema', settings });
2024-08-06 03:25:52 +05:30
}
onFinishCapture();
}, [stopGetText, getTextSettingsObject, socket, browserSteps, confirmedTextSteps]);
2024-08-09 10:32:48 +05:30
const getListSettingsObject = useCallback(() => {
2024-08-31 03:32:43 +05:30
let settings: {
listSelector?: string;
2024-08-31 22:45:51 +05:30
fields?: Record<string, { selector: string; tag?: string;[key: string]: any }>;
2024-09-08 12:19:11 +05:30
pagination?: { type: string; selector?: string };
limit?: number;
2024-08-31 03:31:20 +05:30
} = {};
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,
2024-09-08 12:19:11 +05:30
attribute: field.selectorObj.attribute,
2024-08-10 05:06:43 +05:30
};
}
});
settings = {
2024-08-10 05:06:43 +05:30
listSelector: step.listSelector,
2024-08-31 03:33:34 +05:30
fields: fields,
pagination: { type: paginationType, selector: step.pagination?.selector },
2024-09-08 12:19:11 +05:30
limit: parseInt(selectedLimit === 'custom' ? customLimit : selectedLimit),
2024-08-10 21:25:17 +05:30
};
2024-08-10 05:06:43 +05:30
}
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-09-08 12:19:11 +05:30
}, [browserSteps, paginationType, selectedLimit, customLimit]);
2024-08-10 04:46:09 +05:30
const resetListState = useCallback(() => {
2024-09-04 05:34:56 +05:30
setShowPaginationOptions(false);
2024-09-08 13:52:01 +05:30
updatePaginationType(''); // Reset pagination type
setShowLimitOptions(false); // Reset limit options
setSelectedLimit('10'); // Reset limit value to default
updateLimitType(''); // Reset limit type
updateCustomLimit(''); // Reset custom limit value
}, [updatePaginationType, updateLimitType, updateCustomLimit]);
2024-09-04 05:35:16 +05:30
const handleStopGetList = useCallback(() => {
2024-09-04 05:34:56 +05:30
stopGetList();
resetListState();
2024-09-04 05:35:16 +05:30
}, [stopGetList, resetListState]);
2024-08-10 05:04:50 +05:30
const stopCaptureAndEmitGetListSettings = useCallback(() => {
2024-08-10 05:06:43 +05:30
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.');
}
2024-09-04 05:34:56 +05:30
handleStopGetList();
onFinishCapture();
2024-09-04 05:34:56 +05:30
}, [stopGetList, getListSettingsObject, socket, notify, handleStopGetList]);
2024-08-09 10:32:48 +05:30
const handleConfirmListCapture = useCallback(() => {
2024-09-08 13:52:01 +05:30
switch (captureStage) {
case 'initial':
// Start pagination mode
startPaginationMode();
setShowPaginationOptions(true);
setCaptureStage('pagination');
break;
case 'pagination':
if (!paginationType) {
notify('error', 'Please select a pagination type.');
return;
}
const settings = getListSettingsObject();
const paginationSelector = settings.pagination?.selector;
if (['clickNext', 'clickLoadMore'].includes(paginationType) && !paginationSelector) {
notify('error', 'Please select the pagination element first.');
return;
}
// Close pagination mode and start limit mode
stopPaginationMode();
setShowPaginationOptions(false);
startLimitMode();
setShowLimitOptions(true);
setCaptureStage('limit');
break;
case 'limit':
if (!limitType || (limitType === 'custom' && !customLimit)) {
notify('error', 'Please select a limit or enter a custom limit.');
return;
}
const limit = limitType === 'custom' ? parseInt(customLimit) : parseInt(limitType);
if (isNaN(limit) || limit <= 0) {
notify('error', 'Please enter a valid limit.');
return;
}
// Close limit mode and emit settings
stopLimitMode();
setShowLimitOptions(false);
stopCaptureAndEmitGetListSettings();
setCaptureStage('complete');
break;
case 'complete':
// Reset the capture process
setCaptureStage('initial');
break;
}
2024-09-08 13:52:01 +05:30
}, [captureStage, paginationType, limitType, customLimit, startPaginationMode, stopPaginationMode, startLimitMode, stopLimitMode, notify, stopCaptureAndEmitGetListSettings, getListSettingsObject]);
const handlePaginationSettingSelect = (option: PaginationType) => {
updatePaginationType(option);
if (['clickNext', 'clickLoadMore'].includes(option)) {
2024-09-03 22:26:40 +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:24:11 +05:30
socket?.emit('action', { action: 'screenshot', settings: screenshotSettings });
2024-08-06 03:16:09 +05:30
addScreenshotStep(fullPage);
stopGetScreenshot();
};
2024-06-24 22:42:22 +05:30
return (
<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%'>
<Typography sx={{ padding: '10px' }}>Last action: {` ${lastAction}`}</Typography>
2024-06-24 22:42:22 +05:30
</SimpleBox>
2024-09-07 08:38:00 +05:30
<SidePanelHeader />
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>}
2024-09-08 13:52:01 +05:30
{getList && (
2024-08-08 02:19:38 +05:30
<>
<Box display="flex" justifyContent="space-between" gap={2} style={{ margin: '15px' }}>
2024-09-08 13:52:01 +05:30
<Button variant="outlined" onClick={handleConfirmListCapture}>
{captureStage === 'initial' ? 'Start Capture' :
captureStage === 'pagination' ? 'Confirm Pagination' :
captureStage === 'limit' ? 'Confirm Limit' : 'Finish Capture'}
</Button>
<Button variant="outlined" color="error" onClick={handleStopGetList}>Discard</Button>
2024-08-08 02:19:38 +05:30
</Box>
</>
2024-09-08 13:52:01 +05:30
)}
2024-08-08 02:18:39 +05:30
2024-08-31 01:19:34 +05:30
{showPaginationOptions && (
2024-08-31 01:19:24 +05:30
<Box display="flex" flexDirection="column" gap={2} style={{ margin: '15px' }}>
2024-08-31 01:28:13 +05:30
<Typography>How can we find the next list item on the page?</Typography>
<Button variant={paginationType === 'clickNext' ? "contained" : "outlined"} onClick={() => handlePaginationSettingSelect('clickNext')}>Click on next to navigate to the next page</Button>
<Button variant={paginationType === 'clickLoadMore' ? "contained" : "outlined"} onClick={() => handlePaginationSettingSelect('clickLoadMore')}>Click on load more to load more items</Button>
<Button variant={paginationType === 'scrollDown' ? "contained" : "outlined"} onClick={() => handlePaginationSettingSelect('scrollDown')}>Scroll down to load more items</Button>
<Button variant={paginationType === 'scrollUp' ? "contained" : "outlined"} onClick={() => handlePaginationSettingSelect('scrollUp')}>Scroll up to load more items</Button>
<Button variant={paginationType === 'none' ? "contained" : "outlined"} onClick={() => handlePaginationSettingSelect('none')}>No more items to load</Button>
2024-08-31 01:19:24 +05:30
</Box>
)}
2024-09-03 22:26:40 +05:30
2024-09-08 13:52:01 +05:30
{showLimitOptions && (
2024-09-08 12:23:34 +05:30
<FormControl>
<FormLabel>
<h4>What is the maximum number of rows you want to extract?</h4>
</FormLabel>
<RadioGroup row value={limitType} onChange={(e) => updateLimitType(e.target.value as LimitType)} sx={{ width: '500px' }}>
<FormControlLabel value="10" control={<Radio />} label="10" />
<FormControlLabel value="100" control={<Radio />} label="100" />
<FormControlLabel value="custom" control={<Radio />} label="Custom" />
{limitType === 'custom' && (
<TextField
type="number"
value={customLimit}
onChange={(e) => updateCustomLimit(e.target.value)}
placeholder="Enter number"
sx={{
marginLeft: '10px',
marginTop: '-3px',
'& input': {
padding: '10px',
},
}}
/>
)}
</RadioGroup>
</FormControl>
)}
2024-09-08 12:19:11 +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>
2024-08-06 06:04:22 +05:30
</Box>
</>
}
{!getText && !getScreenshot && !getList && <Button variant="contained" onClick={startGetScreenshot}>Capture Screenshot</Button>}
{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>
</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
};