Files
parcer/src/components/molecules/InterpretationLog.tsx

221 lines
8.3 KiB
TypeScript
Raw Normal View History

2024-06-24 22:34:40 +05:30
import * as React from 'react';
import SwipeableDrawer from '@mui/material/SwipeableDrawer';
2024-06-24 22:34:40 +05:30
import Typography from '@mui/material/Typography';
2024-10-19 09:41:49 +05:30
import { Button, TextField, Grid } from '@mui/material';
2024-06-24 22:34:40 +05:30
import { useCallback, useEffect, useRef, useState } from "react";
import { useSocketStore } from "../../context/socket";
2024-10-24 00:57:39 +05:30
import { Buffer } from 'buffer';
2024-08-22 06:08:19 +05:30
import { useBrowserDimensionsStore } from "../../context/browserDimensions";
2024-08-25 22:46:52 +05:30
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow';
2024-08-25 22:49:07 +05:30
import Paper from '@mui/material/Paper';
2024-08-26 19:57:30 +05:30
import StorageIcon from '@mui/icons-material/Storage';
2024-10-24 01:04:55 +05:30
import ArrowUpwardIcon from '@mui/icons-material/ArrowUpward';
import { SidePanelHeader } from './SidePanelHeader';
import { useGlobalInfoStore } from '../../context/globalInfo';
2024-08-25 22:49:07 +05:30
2024-09-08 07:50:59 +05:30
interface InterpretationLogProps {
isOpen: boolean;
setIsOpen: (isOpen: boolean) => void;
}
2024-09-27 23:39:30 +05:30
export const InterpretationLog: React.FC<InterpretationLogProps> = ({ isOpen, setIsOpen }) => {
2024-06-24 22:34:40 +05:30
const [log, setLog] = useState<string>('');
2024-08-28 23:50:50 +05:30
const [customValue, setCustomValue] = useState('');
2024-09-08 07:50:59 +05:30
const [tableData, setTableData] = useState<any[]>([]);
2024-10-24 00:57:39 +05:30
const [binaryData, setBinaryData] = useState<string | null>(null);
2024-06-24 22:34:40 +05:30
const logEndRef = useRef<HTMLDivElement | null>(null);
2024-08-22 06:08:19 +05:30
const { width } = useBrowserDimensionsStore();
const { socket } = useSocketStore();
const { currentWorkflowActionsState } = useGlobalInfoStore();
2024-08-22 06:08:19 +05:30
const toggleDrawer = (newOpen: boolean) => (event: React.KeyboardEvent | React.MouseEvent) => {
if (
event.type === 'keydown' &&
((event as React.KeyboardEvent).key === 'Tab' ||
(event as React.KeyboardEvent).key === 'Shift')
) {
return;
}
2024-09-08 07:50:59 +05:30
setIsOpen(newOpen);
2024-06-24 22:34:40 +05:30
};
const scrollLogToBottom = () => {
if (logEndRef.current) {
logEndRef.current.scrollIntoView({ behavior: "smooth" });
2024-06-24 22:34:40 +05:30
}
};
2024-06-24 22:34:40 +05:30
const handleLog = useCallback((msg: string, date: boolean = true) => {
2024-08-21 21:15:59 +05:30
if (!date) {
2024-06-24 22:34:40 +05:30
setLog((prevState) => prevState + '\n' + msg);
} else {
setLog((prevState) => prevState + '\n' + `[${new Date().toLocaleString()}] ` + msg);
}
scrollLogToBottom();
2024-08-22 05:42:42 +05:30
}, [log, scrollLogToBottom]);
2024-06-24 22:34:40 +05:30
const handleSerializableCallback = useCallback((data: any) => {
2024-06-24 22:34:40 +05:30
setLog((prevState) =>
prevState + '\n' + '---------- Serializable output data received ----------' + '\n'
+ JSON.stringify(data, null, 2) + '\n' + '--------------------------------------------------');
2024-09-27 23:39:30 +05:30
if (Array.isArray(data)) {
setTableData(data);
}
2024-09-27 23:39:30 +05:30
2024-06-24 22:34:40 +05:30
scrollLogToBottom();
2024-08-22 05:42:42 +05:30
}, [log, scrollLogToBottom]);
2024-06-24 22:34:40 +05:30
2024-08-21 21:15:59 +05:30
const handleBinaryCallback = useCallback(({ data, mimetype }: any) => {
2024-10-24 00:57:39 +05:30
const base64String = Buffer.from(data).toString('base64');
const imageSrc = `data:${mimetype};base64,${base64String}`;
2024-10-24 01:00:04 +05:30
2024-06-24 22:34:40 +05:30
setLog((prevState) =>
2024-08-21 21:15:59 +05:30
prevState + '\n' + '---------- Binary output data received ----------' + '\n'
2024-10-24 00:57:39 +05:30
+ `mimetype: ${mimetype}` + '\n' + 'Image is rendered below:' + '\n'
2024-06-24 22:34:40 +05:30
+ '------------------------------------------------');
2024-10-24 01:00:04 +05:30
2024-10-24 00:57:39 +05:30
setBinaryData(imageSrc);
2024-06-24 22:34:40 +05:30
scrollLogToBottom();
2024-08-22 05:42:42 +05:30
}, [log, scrollLogToBottom]);
2024-10-24 01:00:04 +05:30
2024-06-24 22:34:40 +05:30
2024-08-29 23:02:26 +05:30
const handleCustomValueChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setCustomValue(event.target.value);
};
2024-08-28 23:56:30 +05:30
2024-06-24 22:34:40 +05:30
useEffect(() => {
socket?.on('log', handleLog);
socket?.on('serializableCallback', handleSerializableCallback);
socket?.on('binaryCallback', handleBinaryCallback);
return () => {
socket?.off('log', handleLog);
socket?.off('serializableCallback', handleSerializableCallback);
socket?.off('binaryCallback', handleBinaryCallback);
};
}, [socket, handleLog, handleSerializableCallback, handleBinaryCallback]);
2024-06-24 22:34:40 +05:30
// Extract columns dynamically from the first item of tableData
const columns = tableData.length > 0 ? Object.keys(tableData[0]) : [];
2024-09-27 23:39:30 +05:30
const { hasScrapeListAction, hasScreenshotAction, hasScrapeSchemaAction } = currentWorkflowActionsState
useEffect(() => {
if (hasScrapeListAction || hasScrapeSchemaAction || hasScreenshotAction) {
setIsOpen(true);
}
}, [hasScrapeListAction, hasScrapeSchemaAction, hasScreenshotAction, setIsOpen]);
2024-06-24 22:34:40 +05:30
return (
2024-10-21 15:21:12 +05:30
<Grid container>
<Grid item xs={12} md={9} lg={9}>
<Button
onClick={toggleDrawer(true)}
variant="contained"
color="primary"
sx={{
marginTop: '10px',
color: 'white',
position: 'absolute',
background: '#ff00c3',
border: 'none',
padding: '10px 20px',
width: '900px',
overflow: 'hidden',
textAlign: 'left',
justifyContent: 'flex-start',
'&:hover': {
backgroundColor: '#ff00c3',
},
}}
>
2024-10-24 01:04:55 +05:30
<ArrowUpwardIcon fontSize="inherit" sx={{ marginRight: '10px'}} /> Output Data Preview
2024-10-21 15:21:12 +05:30
</Button>
<SwipeableDrawer
anchor="bottom"
open={isOpen}
onClose={toggleDrawer(false)}
onOpen={toggleDrawer(true)}
PaperProps={{
sx: {
background: 'white',
color: 'black',
padding: '10px',
height: 500,
width: width - 10,
display: 'flex',
},
}}
>
<Typography variant="h6" gutterBottom>
<StorageIcon /> Output Data Preview
</Typography>
<div
style={{
height: '50vh',
overflow: 'none',
padding: '10px',
2024-10-19 14:47:54 +05:30
}}
>
2024-10-24 00:57:39 +05:30
{
2024-10-24 01:00:04 +05:30
binaryData ? (
<div style={{ marginBottom: '20px' }}>
<Typography variant="body1" gutterBottom>Screenshot</Typography>
<img src={binaryData} alt="Binary Output" style={{ maxWidth: '100%' }} />
</div>
) : tableData.length > 0 ? (
<>
<TableContainer component={Paper}>
<Table sx={{ minWidth: 650 }} stickyHeader aria-label="output data table">
<TableHead>
<TableRow>
2024-10-21 15:57:51 +05:30
{columns.map((column) => (
2024-10-24 01:00:04 +05:30
<TableCell key={column}>{column}</TableCell>
2024-10-21 15:57:51 +05:30
))}
</TableRow>
2024-10-24 01:00:04 +05:30
</TableHead>
<TableBody>
{tableData.slice(0, Math.min(5, tableData.length)).map((row, index) => (
<TableRow key={index}>
{columns.map((column) => (
<TableCell key={column}>{row[column]}</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
<span style={{ marginLeft: '15px', marginTop: '10px', fontSize: '12px' }}>Additional rows of data will be extracted once you finish recording. </span>
</>
) : (
<Grid container justifyContent="center" alignItems="center" style={{ height: '100%' }}>
<Grid item>
{hasScrapeListAction || hasScrapeSchemaAction || hasScreenshotAction ? (
<>
<Typography variant="h6" gutterBottom align="left">
You've successfully trained the robot to perform actions! Click on the button below to get a preview of the data your robot will extract.
</Typography>
<SidePanelHeader />
</>
) : (
2024-10-19 19:48:44 +05:30
<Typography variant="h6" gutterBottom align="left">
2024-10-24 01:00:04 +05:30
It looks like you have not selected anything for extraction yet. Once you do, the robot will show a preview of your selections here.
</Typography>
2024-10-24 01:00:04 +05:30
)}
</Grid>
2024-10-19 19:38:25 +05:30
</Grid>
2024-10-24 01:00:04 +05:30
)}
2024-10-21 15:21:12 +05:30
<div style={{ float: 'left', clear: 'both' }} ref={logEndRef} />
</div>
</SwipeableDrawer>
2024-10-19 14:47:54 +05:30
</Grid>
2024-10-21 15:21:12 +05:30
</Grid>
);
2024-10-11 23:12:32 +05:30
}