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

282 lines
11 KiB
TypeScript
Raw Normal View History

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-07-26 22:53:36 +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-06-24 22:42:22 +05:30
2024-08-06 02:31:36 +05:30
export const RightSidePanel = () => {
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 }>({});
2024-08-06 05:05:50 +05:30
const { lastAction, notify } = useGlobalInfoStore();
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();
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
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
}
}, [stopGetText, getTextSettingsObject, socket, browserSteps, confirmedTextSteps]);
2024-08-09 10:32:48 +05:30
const getListSettingsObject = useCallback(() => {
const listStep = browserSteps.find(step => step.type === 'list');
if (!listStep || listStep.type !== 'list' || Object.keys(listStep.fields).length === 0) return null;
const firstFieldKey = Object.keys(listStep.fields)[0];
const firstField = listStep.fields[firstFieldKey];
return {
listSelector: listStep.listSelector,
fields: {
[firstField.label]: {
selector: firstField.selectorObj.selector,
attribute: firstField.selectorObj.attribute || 'innerText'
}
}
};
}, [browserSteps]);
const stopCaptureAndEmitGetListSettings = useCallback(() => {
stopGetList();
const settings = getListSettingsObject();
if (settings) {
socket?.emit('action', { action: 'scrapeList', settings });
} else {
notify('error', 'Unable to create list settings. Make sure you have defined a field for the list.');
}
}, [stopGetList, getListSettingsObject, socket, notify]);
// const handleListFieldChange = (stepId: number, key: 'label' | 'data', value: string) => {
// updateListStepField(stepId, key, value);
// };
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-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-09 10:32:48 +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
{!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>
</>
}
{!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' }}>
{
step.type === 'text' ? (
<>
<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-09 10:32:48 +05:30
step.type === 'screenshot' ? (
2024-08-06 06:13:13 +05:30
<Box display="flex" alignItems="center">
2024-08-06 06:13:24 +05:30
<DocumentScannerIcon sx={{ mr: 1 }} />
<Typography>
{`Take ${step.fullPage ? 'Fullpage' : 'Visible Part'} Screenshot`}
</Typography>
</Box>
2024-08-09 10:32:48 +05:30
) : (
step.type === 'list' && Object.keys(step.fields).length > 0 && (
<>
<TextField
label="List Selector"
value={step.listSelector || ''}
fullWidth
margin="normal"
InputProps={{
startAdornment: (
<InputAdornment position="start">
<EditIcon />
</InputAdornment>
)
}}
/>
{Object.entries(step.fields).map(([key, field]) => (
<React.Fragment key={key}>
<TextField
label="Field Label"
value={field.label || ''}
2024-08-09 19:19:44 +05:30
onChange={() => { }}
2024-08-09 10:32:48 +05:30
fullWidth
margin="normal"
InputProps={{
startAdornment: (
<InputAdornment position="start">
<EditIcon />
</InputAdornment>
)
}}
/>
<TextField
label="Field Selector"
value={field.data || ''}
fullWidth
margin="normal"
InputProps={{
readOnly: true,
startAdornment: (
<InputAdornment position="start">
<EditIcon />
</InputAdornment>
)
}}
/>
</React.Fragment>
))}
</>
)
2024-08-06 05:06:04 +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>
);
};
export const ActionDescription = styled.p`
margin-left: 15px;
2024-08-05 23:17:56 +05:30
`;