feat: use Swipeable Drawer instead of Accordian

This commit is contained in:
karishmas6
2024-08-22 05:37:25 +05:30
parent 6b8cd3c67e
commit c656b5782a

View File

@@ -1,30 +1,34 @@
import * as React from 'react'; import * as React from 'react';
import Accordion from '@mui/material/Accordion'; import SwipeableDrawer from '@mui/material/SwipeableDrawer';
import AccordionDetails from '@mui/material/AccordionDetails';
import AccordionSummary from '@mui/material/AccordionSummary';
import Typography from '@mui/material/Typography'; import Typography from '@mui/material/Typography';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import Highlight from 'react-highlight';
import Highlight from 'react-highlight'
import { useCallback, useEffect, useRef, useState } from "react"; import { useCallback, useEffect, useRef, useState } from "react";
import { useSocketStore } from "../../context/socket"; import { useSocketStore } from "../../context/socket";
export const InterpretationLog = () => { export const InterpretationLog = () => {
const [expanded, setExpanded] = useState<boolean>(false); const [open, setOpen] = useState<boolean>(false);
const [log, setLog] = useState<string>(''); const [log, setLog] = useState<string>('');
const logEndRef = useRef<HTMLDivElement | null>(null); const logEndRef = useRef<HTMLDivElement | null>(null);
const handleChange = (isExpanded: boolean) => (event: React.SyntheticEvent) => { const toggleDrawer = (newOpen: boolean) => (event: React.KeyboardEvent | React.MouseEvent) => {
setExpanded(isExpanded); if (
event.type === 'keydown' &&
((event as React.KeyboardEvent).key === 'Tab' ||
(event as React.KeyboardEvent).key === 'Shift')
) {
return;
}
setOpen(newOpen);
}; };
const { socket } = useSocketStore(); const { socket } = useSocketStore();
const scrollLogToBottom = () => { const scrollLogToBottom = () => {
if (logEndRef.current) { if (logEndRef.current) {
logEndRef.current.scrollIntoView({ behavior: "smooth" }) logEndRef.current.scrollIntoView({ behavior: "smooth" });
} }
} };
const handleLog = useCallback((msg: string, date: boolean = true) => { const handleLog = useCallback((msg: string, date: boolean = true) => {
if (!date) { if (!date) {
@@ -33,14 +37,14 @@ export const InterpretationLog = () => {
setLog((prevState) => prevState + '\n' + `[${new Date().toLocaleString()}] ` + msg); setLog((prevState) => prevState + '\n' + `[${new Date().toLocaleString()}] ` + msg);
} }
scrollLogToBottom(); scrollLogToBottom();
}, [log, scrollLogToBottom]) }, [scrollLogToBottom]);
const handleSerializableCallback = useCallback((data: string) => { const handleSerializableCallback = useCallback((data: string) => {
setLog((prevState) => setLog((prevState) =>
prevState + '\n' + '---------- Serializable output data received ----------' + '\n' prevState + '\n' + '---------- Serializable output data received ----------' + '\n'
+ JSON.stringify(data, null, 2) + '\n' + '--------------------------------------------------'); + JSON.stringify(data, null, 2) + '\n' + '--------------------------------------------------');
scrollLogToBottom(); scrollLogToBottom();
}, [log, scrollLogToBottom]) }, [scrollLogToBottom]);
const handleBinaryCallback = useCallback(({ data, mimetype }: any) => { const handleBinaryCallback = useCallback(({ data, mimetype }: any) => {
setLog((prevState) => setLog((prevState) =>
@@ -48,7 +52,7 @@ export const InterpretationLog = () => {
+ `mimetype: ${mimetype}` + '\n' + `data: ${JSON.stringify(data)}` + '\n' + `mimetype: ${mimetype}` + '\n' + `data: ${JSON.stringify(data)}` + '\n'
+ '------------------------------------------------'); + '------------------------------------------------');
scrollLogToBottom(); scrollLogToBottom();
}, [log, scrollLogToBottom]) }, [scrollLogToBottom]);
useEffect(() => { useEffect(() => {
socket?.on('log', handleLog); socket?.on('log', handleLog);
@@ -58,41 +62,39 @@ export const InterpretationLog = () => {
socket?.off('log', handleLog); socket?.off('log', handleLog);
socket?.off('serializableCallback', handleSerializableCallback); socket?.off('serializableCallback', handleSerializableCallback);
socket?.off('binaryCallback', handleBinaryCallback); socket?.off('binaryCallback', handleBinaryCallback);
} };
}, [socket, handleLog]) }, [socket, handleLog]);
return ( return (
<div> <div>
<Accordion <button onClick={toggleDrawer(true)} style={{ color: 'white', background: '#3f4853', border: 'none', padding: '10px 20px' }}>
expanded={expanded} Interpretation Log
onChange={handleChange(!expanded)} </button>
style={{ background: '#3f4853', color: 'white', borderRadius: '0px' }} <SwipeableDrawer
anchor="bottom"
open={open}
onClose={toggleDrawer(false)}
onOpen={toggleDrawer(true)}
PaperProps={{
sx: { background: '#19171c', color: 'white', padding: '10px' }
}}
> >
<AccordionSummary <Typography variant="h6" gutterBottom>
expandIcon={<ExpandMoreIcon sx={{ color: 'white' }} />} Interpretation Log
aria-controls="panel1bh-content" </Typography>
id="panel1bh-header" <div style={{
> height: '50vh',
<Typography sx={{ width: '33%', flexShrink: 0 }}>
Interpretation Log
</Typography>
</AccordionSummary>
<AccordionDetails sx={{
background: '#19171c',
overflowY: 'scroll', overflowY: 'scroll',
width: '100%', padding: '10px',
aspectRatio: '4/1', background: '#19171c',
boxSizing: 'border-box',
}}> }}>
<div> <Highlight className="javascript">
<Highlight className="javascript"> {log}
{log} </Highlight>
</Highlight> <div style={{ float: "left", clear: "both" }}
<div style={{ float: "left", clear: "both" }} ref={logEndRef} />
ref={logEndRef} /> </div>
</div> </SwipeableDrawer>
</AccordionDetails>
</Accordion>
</div> </div>
); );
} }