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

165 lines
6.6 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-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-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 } = 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]);
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' }}>
{!getText && !getScreenshot && <Button variant="contained" onClick={startGetText}>Capture Text</Button>}
{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 && <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]}
InputProps={{ readOnly: confirmedTextSteps[step.id] }}
/>
<TextField
label="Data"
value={step.data}
fullWidth
margin="normal"
InputProps={{ readOnly: confirmedTextSteps[step.id] }}
/>
{!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>
)}
</>
) : (
step.type === 'screenshot' && (
<Typography>{`Take ${step.fullPage ? 'Fullpage' : 'Visible Part'} Screenshot`}</Typography>
)
)
}
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
`;