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

129 lines
4.9 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect, useCallback } from 'react';
2024-07-26 09:29:14 +05:30
import { Button, MenuItem, Paper, Box, TextField } from "@mui/material";
2024-06-24 22:42:22 +05:30
import { Dropdown as MuiDropdown } from '../atoms/DropdownMui';
import styled from "styled-components";
import { SimpleBox } from "../atoms/Box";
import Typography from "@mui/material/Typography";
import { useGlobalInfoStore } from "../../context/globalInfo";
import { PairForEdit } from "../../pages/RecordingPage";
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';
2024-06-24 22:42:22 +05:30
interface RightSidePanelProps {
pairForEdit: PairForEdit;
}
2024-07-26 09:29:14 +05:30
export const RightSidePanel = ({ pairForEdit }: RightSidePanelProps) => {
2024-07-26 23:07:54 +05:30
const [labels, setLabels] = useState<{ [id: number]: string }>({});
2024-07-26 23:24:41 +05:30
const [errors, setErrors] = useState<{ [id: number]: string }>({});
2024-07-26 23:25:03 +05:30
const [confirmedSteps, setConfirmedSteps] = useState<{ [id: number]: boolean }>({});
2024-06-24 22:42:22 +05:30
const { lastAction } = useGlobalInfoStore();
2024-07-24 21:37:26 +05:30
const { getText, getScreenshot, startGetText, stopGetText, startGetScreenshot, stopGetScreenshot } = useActionContext();
2024-07-26 23:07:54 +05:30
const { browserSteps, updateBrowserStepLabel, deleteBrowserStep } = useBrowserSteps();
const { socket } = useSocketStore();
2024-06-24 22:42:22 +05:30
2024-07-26 23:07:54 +05:30
const handleLabelChange = (id: number, label: string) => {
setLabels(prevLabels => ({ ...prevLabels, [id]: label }));
2024-07-26 23:24:41 +05:30
if (!label.trim()) {
2024-07-26 23:25:03 +05:30
setErrors(prevErrors => ({ ...prevErrors, [id]: 'Label cannot be empty' }));
2024-07-26 23:24:41 +05:30
} else {
2024-07-26 23:25:03 +05:30
setErrors(prevErrors => ({ ...prevErrors, [id]: '' }));
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-07-26 23:25:03 +05:30
const handleConfirm = (id: number) => {
2024-07-26 23:24:41 +05:30
const label = labels[id]?.trim();
if (label) {
2024-07-26 23:25:03 +05:30
updateBrowserStepLabel(id, label);
setConfirmedSteps(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-07-26 23:25:03 +05:30
const handleDiscard = (id: number) => {
2024-07-26 23:07:54 +05:30
deleteBrowserStep(id);
2024-07-26 23:24:41 +05:30
setLabels(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 createSettingsObject = useCallback(() => {
2024-08-03 21:56:21 +05:30
const settings: Record<string, { selector: string; tag?: string;[key: string]: any }> = {};
browserSteps.forEach(step => {
2024-08-03 21:55:58 +05:30
if (step.label && step.selectorObj && step.selectorObj.selector) {
settings[step.label] = step.selectorObj;
}
});
return settings;
}, [browserSteps]);
2024-08-03 21:55:58 +05:30
const stopCaptureAndEmitSettings = useCallback(() => {
stopGetText();
const settings = createSettingsObject();
if (browserSteps.length > 0) {
socket?.emit('action', { action: 'scrapeSchema', settings });
}
}, [stopGetText, createSettingsObject, socket]);
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 && <Button variant="contained" onClick={stopCaptureAndEmitSettings}>Stop Capture Text</Button>}
{!getText && !getScreenshot && <Button variant="contained" onClick={startGetScreenshot}>Capture Screenshot</Button>}
{getScreenshot && <Button variant="contained" onClick={stopGetScreenshot}>Stop Capture Screenshot</Button>}
2024-07-25 01:56:31 +05:30
</Box>
2024-07-26 09:29:14 +05:30
2024-07-26 22:53:36 +05:30
<Box>
2024-07-26 23:25:03 +05:30
{browserSteps.map(step => (
<Box key={step.id} sx={{ boxShadow: 5, padding: '10px', margin: '10px', borderRadius: '4px' }}>
2024-07-26 23:25:03 +05:30
<TextField
label="Label"
value={labels[step.id] || step.label || ''}
onChange={(e) => handleLabelChange(step.id, e.target.value)}
fullWidth
margin="normal"
error={!!errors[step.id]}
helperText={errors[step.id]}
InputProps={{ readOnly: confirmedSteps[step.id] }}
2024-07-26 23:25:03 +05:30
/>
<TextField
label="Data"
value={step.data}
fullWidth
margin="normal"
InputProps={{ readOnly: confirmedSteps[step.id] }}
/>
2024-07-26 23:25:03 +05:30
{!confirmedSteps[step.id] && (
<Box display="flex" justifyContent="space-between" gap={2}>
<Button variant="contained" onClick={() => handleConfirm(step.id)} disabled={!labels[step.id]?.trim()}>Confirm</Button>
<Button variant="contained" onClick={() => handleDiscard(step.id)}>Discard</Button>
2024-07-26 23:25:03 +05:30
</Box>
)}
</Box>
))}
</Box>
2024-06-24 22:42:22 +05:30
</Paper>
);
};
2024-08-05 22:44:41 +05:30
2024-06-24 22:42:22 +05:30
export const ActionDescription = styled.p`
margin-left: 15px;
`;