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

130 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-10-21 23:14:22 +05:30
import React, { useCallback, useEffect, useState, useContext } from 'react';
2024-10-24 15:20:01 +05:30
import { Button, Box, LinearProgress, Tooltip } from "@mui/material";
2024-06-24 22:36:52 +05:30
import { GenericModal } from "../atoms/GenericModal";
import { stopRecording } from "../../api/recording";
import { useGlobalInfoStore } from "../../context/globalInfo";
2024-10-21 23:14:22 +05:30
import { AuthContext } from '../../context/auth';
2024-06-24 22:36:52 +05:30
import { useSocketStore } from "../../context/socket";
import { TextField, Typography } from "@mui/material";
import { WarningText } from "../atoms/texts";
import NotificationImportantIcon from "@mui/icons-material/NotificationImportant";
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();
2024-10-21 23:14:22 +05:30
const { state, dispatch } = useContext(AuthContext);
const { user } = state;
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 () => {
2024-10-21 23:14:22 +05:30
if (user) {
2024-10-21 23:14:39 +05:30
const payload = { fileName: recordingName, userId: user.id };
socket?.emit('save', payload);
setWaitingForSave(true);
console.log(`Saving the recording as ${recordingName} for userId ${user.id}`);
2024-10-21 23:14:22 +05:30
} else {
2024-10-21 23:14:39 +05:30
console.error('User not logged in. Cannot save recording.');
2024-10-21 23:14:22 +05:30
}
2024-10-21 23:14:39 +05:30
};
2024-06-24 22:36:52 +05:30
useEffect(() => {
socket?.on('fileSaved', exitRecording);
return () => {
socket?.off('fileSaved', exitRecording);
}
}, [socket, exitRecording]);
return (
<div>
2024-10-20 18:25:03 +05:30
<Button onClick={() => setOpenModal(true)} variant="outlined" sx={{ marginRight: '20px' }} size="small" color="success">
Finish
</Button>
2024-06-24 22:36:52 +05:30
<GenericModal isOpen={openModal} onClose={() => setOpenModal(false)} modalStyle={modalStyle}>
2024-10-29 05:00:02 +05:30
<form onSubmit={handleSaveRecording} style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start' }}>
2024-10-24 15:21:09 +05:30
<Typography variant="h6">Save Robot</Typography>
2024-06-24 22:36:52 +05:30
<TextField
required
2024-10-29 05:00:02 +05:30
sx={{ width: '300px', 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>
2024-10-29 05:00:02 +05:30
<Button color="error" variant="contained" onClick={saveRecording} sx={{ marginTop: '10px' }}>Confirm</Button>
2024-09-10 02:43:44 +05:30
<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>)
2024-10-29 05:00:02 +05:30
: <Button type="submit" variant="contained" sx={{ marginTop: '10px' }}>Save</Button>
2024-09-10 02:43:44 +05:30
}
{waitingForSave &&
<Tooltip title='Optimizing and saving the workflow' placement={"bottom"}>
2024-10-29 05:00:02 +05:30
<Box sx={{ width: '100%', marginTop: '10px' }}>
2024-09-10 02:43:44 +05:30
<LinearProgress />
</Box>
</Tooltip>
2024-06-24 22:36:52 +05:30
}
</form>
</GenericModal>
</div>
);
}
const modalStyle = {
top: '25%',
left: '50%',
transform: 'translate(-50%, -50%)',
2024-10-24 15:31:55 +05:30
width: '30%',
2024-06-24 22:36:52 +05:30
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',
};