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-12-20 22:28:31 +05:30
|
|
|
import { useTranslation } from 'react-i18next';
|
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-12-20 22:28:31 +05:30
|
|
|
const { t } = useTranslation();
|
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;
|
2024-09-10 02:46:54 +05:30
|
|
|
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-12-20 22:28:31 +05:30
|
|
|
notify('success', t('save_recording.notifications.save_success'));
|
2024-06-24 22:36:52 +05:30
|
|
|
if (browserId) {
|
|
|
|
|
await stopRecording(browserId);
|
|
|
|
|
}
|
|
|
|
|
setBrowserId(null);
|
2024-09-10 02:46:54 +05:30
|
|
|
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-12-20 22:28:31 +05:30
|
|
|
console.error(t('save_recording.notifications.user_not_logged'));
|
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>
|
2025-01-08 13:14:41 +05:30
|
|
|
{/* <Button onClick={() => setOpenModal(true)} variant='contained' sx={{ marginRight: '20px',backgroundColor: '#ff00c3',color: 'white' }} size="small" color="success">
|
|
|
|
|
Finish */}
|
2025-01-08 12:50:46 +05:30
|
|
|
|
2025-01-08 16:42:57 +05:30
|
|
|
<Button
|
|
|
|
|
onClick={() => setOpenModal(true)}
|
|
|
|
|
variant="outlined"
|
2025-01-08 16:50:15 +05:30
|
|
|
color="success"
|
2025-01-08 16:42:57 +05:30
|
|
|
sx={{
|
|
|
|
|
marginRight: '20px',
|
|
|
|
|
color: '#00c853 !important',
|
2025-01-08 16:43:19 +05:30
|
|
|
borderColor: '#00c853 !important',
|
2025-01-08 22:08:10 +05:30
|
|
|
backgroundColor: 'whitesmoke !important',
|
2025-01-08 16:42:57 +05:30
|
|
|
}}
|
|
|
|
|
size="small"
|
|
|
|
|
>
|
2024-12-20 22:28:31 +05:30
|
|
|
{t('right_panel.buttons.finish')}
|
2024-10-20 03:29:33 +05:30
|
|
|
</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-12-20 22:28:31 +05:30
|
|
|
<Typography variant="h6">{t('save_recording.title')}</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-12-20 22:28:31 +05:30
|
|
|
label={t('save_recording.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-12-20 22:28:31 +05:30
|
|
|
<Button color="error" variant="contained" onClick={saveRecording} sx={{ marginTop: '10px' }}>
|
2024-12-20 22:35:59 +05:30
|
|
|
{t('save_recording.buttons.confirm')}
|
2024-12-20 22:28:31 +05:30
|
|
|
</Button>
|
2024-09-10 02:43:44 +05:30
|
|
|
<WarningText>
|
|
|
|
|
<NotificationImportantIcon color="warning" />
|
2025-01-03 20:22:30 +05:30
|
|
|
{t('save_recording.errors.exists_warning')}
|
2024-09-10 02:43:44 +05:30
|
|
|
</WarningText>
|
|
|
|
|
</React.Fragment>)
|
2024-12-20 22:28:31 +05:30
|
|
|
: <Button type="submit" variant="contained" sx={{ marginTop: '10px' }}>
|
2024-12-20 22:35:59 +05:30
|
|
|
{t('save_recording.buttons.save')}
|
2024-12-20 22:28:31 +05:30
|
|
|
</Button>
|
2024-09-10 02:43:44 +05:30
|
|
|
}
|
|
|
|
|
{waitingForSave &&
|
2024-12-20 22:28:31 +05:30
|
|
|
<Tooltip title={t('save_recording.tooltips.optimizing')} 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',
|
|
|
|
|
};
|