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

145 lines
4.9 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-08-24 09:57:47 +05:30
import Radio from '@mui/material/Radio';
import RadioGroup from '@mui/material/RadioGroup';
import FormControlLabel from '@mui/material/FormControlLabel';
import FormControl from '@mui/material/FormControl';
import FormLabel from '@mui/material/FormLabel';
import Highlight from 'react-highlight';
2024-06-24 22:34:40 +05:30
import { useCallback, useEffect, useRef, useState } from "react";
import { useSocketStore } from "../../context/socket";
2024-08-22 06:08:19 +05:30
import { useBrowserDimensionsStore } from "../../context/browserDimensions";
2024-08-23 19:26:37 +05:30
import { useActionContext } from '../../context/browserActions';
2024-06-24 22:34:40 +05:30
export const InterpretationLog = () => {
const [open, setOpen] = useState<boolean>(false);
2024-06-24 22:34:40 +05:30
const [log, setLog] = useState<string>('');
const logEndRef = useRef<HTMLDivElement | null>(null);
2024-08-22 06:08:19 +05:30
const { width } = useBrowserDimensionsStore();
2024-08-23 19:26:37 +05:30
const { getList } = useActionContext();
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;
}
setOpen(newOpen);
2024-06-24 22:34:40 +05:30
};
const { socket } = useSocketStore();
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: string) => {
setLog((prevState) =>
prevState + '\n' + '---------- Serializable output data received ----------' + '\n'
+ JSON.stringify(data, null, 2) + '\n' + '--------------------------------------------------');
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-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-06-24 22:34:40 +05:30
+ `mimetype: ${mimetype}` + '\n' + `data: ${JSON.stringify(data)}` + '\n'
+ '------------------------------------------------');
scrollLogToBottom();
2024-08-22 05:42:42 +05:30
}, [log, scrollLogToBottom]);
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]);
2024-06-24 22:34:40 +05:30
return (
<div>
2024-08-22 07:38:15 +05:30
<button
onClick={toggleDrawer(true)}
style={{
color: 'white',
background: '#3f4853',
border: 'none',
padding: '10px 20px',
width: 1280,
textAlign: 'left'
}}>
Interpretation Log
</button>
<SwipeableDrawer
anchor="bottom"
open={open}
onClose={toggleDrawer(false)}
onOpen={toggleDrawer(true)}
PaperProps={{
2024-08-22 07:37:32 +05:30
sx: { background: '#19171c', color: 'white', padding: '10px', height: 720, width: width - 10, display: 'flex' }
}}
2024-06-24 22:34:40 +05:30
>
<Typography variant="h6" gutterBottom>
Interpretation Log
</Typography>
<div style={{
height: '50vh',
2024-06-24 22:34:40 +05:30
overflowY: 'scroll',
padding: '10px',
background: '#19171c',
2024-06-24 22:34:40 +05:30
}}>
<Highlight className="javascript">
{log}
</Highlight>
{
getList ? (
<>
2024-08-24 09:59:19 +05:30
<p>What is the maximum number of rows you want to extract?</p>
<FormControl>
2024-08-24 09:59:54 +05:30
<FormLabel>Gender</FormLabel>
2024-08-24 09:59:19 +05:30
<RadioGroup
row
aria-labelledby="demo-row-radio-buttons-group-label"
name="row-radio-buttons-group"
>
<FormControlLabel value="female" control={<Radio />} label="Female" />
<FormControlLabel value="male" control={<Radio />} label="Male" />
<FormControlLabel value="other" control={<Radio />} label="Other" />
<FormControlLabel
value="disabled"
disabled
control={<Radio />}
label="other"
/>
</RadioGroup>
</FormControl>
</>
) : null
}
<div style={{ float: "left", clear: "both" }}
ref={logEndRef} />
</div>
</SwipeableDrawer>
2024-06-24 22:34:40 +05:30
</div>
);
}