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

136 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-06-24 22:36:52 +05:30
import React, { useCallback, useEffect, useState } from 'react';
import { IconButton, Button, Box, LinearProgress, Tooltip } from "@mui/material";
import { GenericModal } from "../atoms/GenericModal";
import { stopRecording } from "../../api/recording";
import { useGlobalInfoStore } from "../../context/globalInfo";
import { useSocketStore } from "../../context/socket";
import { TextField, Typography } from "@mui/material";
import { WarningText } from "../atoms/texts";
import NotificationImportantIcon from "@mui/icons-material/NotificationImportant";
import FlagIcon from '@mui/icons-material/Flag';
2024-10-10 22:17:56 +05:30
import { DoneAll } from '@mui/icons-material'
2024-09-10 02:47:23 +05:30
import { useNavigate } from 'react-router-dom';
2024-06-24 22:36:52 +05:30
interface SaveRecordingProps {
fileName: string;
}
2024-09-10 02:43:44 +05:30
export const SaveRecording = ({ fileName }: SaveRecordingProps) => {
2024-06-24 22:36:52 +05:30
const [openModal, setOpenModal] = useState<boolean>(false);
const [needConfirm, setNeedConfirm] = useState<boolean>(false);
const [recordingName, setRecordingName] = useState<string>(fileName);
const [waitingForSave, setWaitingForSave] = useState<boolean>(false);
2024-09-10 02:43:44 +05:30
const { browserId, setBrowserId, notify, recordings } = useGlobalInfoStore();
2024-06-24 22:36:52 +05:30
const { socket } = useSocketStore();
const navigate = useNavigate();
2024-06-24 22:36:52 +05:30
const handleChangeOfTitle = (event: React.ChangeEvent<HTMLInputElement>) => {
const { value } = event.target;
if (needConfirm) {
setNeedConfirm(false);
}
setRecordingName(value);
}
const handleSaveRecording = async (event: React.SyntheticEvent) => {
event.preventDefault();
if (recordings.includes(recordingName)) {
if (needConfirm) { return; }
setNeedConfirm(true);
} else {
await saveRecording();
}
};
2024-09-10 02:43:44 +05:30
const exitRecording = useCallback(async () => {
2024-06-24 22:36:52 +05:30
notify('success', 'Recording saved successfully');
if (browserId) {
await stopRecording(browserId);
}
setBrowserId(null);
navigate('/');
2024-06-24 22:36:52 +05:30
}, [setBrowserId, browserId, notify]);
// notifies backed to save the recording in progress,
// releases resources and changes the view for main page by clearing the global browserId
const saveRecording = async () => {
socket?.emit('save', recordingName)
setWaitingForSave(true);
}
useEffect(() => {
socket?.on('fileSaved', exitRecording);
return () => {
socket?.off('fileSaved', exitRecording);
}
}, [socket, exitRecording]);
return (
<div>
2024-10-10 22:17:56 +05:30
<IconButton sx={{
width: '140px',
background: 'green',
color: 'white',
'&:hover': { background: 'green', color: 'white' },
padding: '13px',
2024-06-24 22:36:52 +05:30
marginRight: '10px',
2024-10-10 22:17:56 +05:30
borderRadius: '5px',
fontFamily: '"Roboto","Helvetica","Arial",sans-serif',
2024-10-11 18:29:30 +05:30
fontWeight: '500',
2024-10-10 22:17:56 +05:30
fontSize: '0.875rem',
lineHeight: '1.75',
letterSpacing: '0.02857em',
2024-06-24 22:36:52 +05:30
}} onClick={() => setOpenModal(true)}>
2024-10-10 22:17:56 +05:30
<DoneAll sx={{ marginRight: '5px' }} /> Finish
</IconButton>
2024-06-24 22:36:52 +05:30
<GenericModal isOpen={openModal} onClose={() => setOpenModal(false)} modalStyle={modalStyle}>
2024-10-10 22:22:50 +05:30
<form onSubmit={handleSaveRecording} style={{ paddingTop: '20px', display: 'flex', flexDirection: 'column' }} >
<Typography variant="h5">Save the robot as</Typography>
2024-06-24 22:36:52 +05:30
<TextField
required
2024-09-10 02:43:44 +05:30
sx={{ width: '250px', paddingBottom: '10px', margin: '15px 0px' }}
2024-06-24 22:36:52 +05:30
onChange={handleChangeOfTitle}
id="title"
2024-10-10 22:22:50 +05:30
label="Robot Name"
2024-06-24 22:36:52 +05:30
variant="outlined"
defaultValue={recordingName ? recordingName : null}
/>
2024-09-10 02:43:44 +05:30
{needConfirm
?
(<React.Fragment>
<Button color="error" variant="contained" onClick={saveRecording}>Confirm</Button>
<WarningText>
<NotificationImportantIcon color="warning" />
2024-10-10 22:22:50 +05:30
Robot with this name already exists, please confirm the Robot's overwrite.
2024-09-10 02:43:44 +05:30
</WarningText>
</React.Fragment>)
: <Button type="submit" variant="contained">Save</Button>
}
{waitingForSave &&
<Tooltip title='Optimizing and saving the workflow' placement={"bottom"}>
<Box sx={{ width: '100%' }}>
<LinearProgress />
</Box>
</Tooltip>
2024-06-24 22:36:52 +05:30
}
</form>
</GenericModal>
</div>
);
}
const modalStyle = {
top: '25%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: '20%',
backgroundColor: 'background.paper',
p: 4,
2024-09-10 02:43:44 +05:30
height: 'fit-content',
display: 'block',
2024-06-24 22:36:52 +05:30
padding: '20px',
};