From c9d2c33677b32219ab8311f882690c216c3d0db5 Mon Sep 17 00:00:00 2001 From: C0ffeeCode Date: Thu, 6 Jan 2022 15:58:15 +0100 Subject: [PATCH] Improve code quality --- .../room/AttachmentUis/VoiceMailRecorder.jsx | 28 ++++++------------- .../room/AttachmentUis/VoiceMailRecorder.scss | 1 - src/app/organisms/room/RoomViewInput.jsx | 4 +-- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/app/organisms/room/AttachmentUis/VoiceMailRecorder.jsx b/src/app/organisms/room/AttachmentUis/VoiceMailRecorder.jsx index dd0cedf0..3b5e5acb 100644 --- a/src/app/organisms/room/AttachmentUis/VoiceMailRecorder.jsx +++ b/src/app/organisms/room/AttachmentUis/VoiceMailRecorder.jsx @@ -15,7 +15,6 @@ let timer; let _stream; let _mediaRecorder; -const audioChunks = []; async function init() { if (_mediaRecorder) return; @@ -23,18 +22,11 @@ async function init() { timer = new Timer(); _stream = null; _mediaRecorder = null; - audioChunks.length = 0; _stream = await navigator.mediaDevices.getUserMedia({ audio: true }); _mediaRecorder = new MediaRecorder(_stream); - // Remove previous recording - audioChunks.length = 0; - - _mediaRecorder.ondataavailable = (event) => { - audioChunks.push(event.data); - }; _mediaRecorder.onerror = (error) => { console.log(error); _mediaRecorder.stop(); @@ -54,16 +46,12 @@ function startOrResumeRec() { if (_mediaRecorder.state === 'paused') { _mediaRecorder.resume(); } else if (_mediaRecorder.state === 'inactive') { - audioChunks.length = 0; _mediaRecorder.start(); } timer.resume(); } async function restartRec() { - // TODO: BUG: If recording is paused before restarting - // it will cause the "Recording paused" not to be edited - // Needed, otherwise the browser indicator would remain after closing UI if (_mediaRecorder.state !== 'inactive') _mediaRecorder.stop(); _stream.getTracks().forEach((track) => track.stop()); @@ -95,13 +83,11 @@ function VoiceMailRecorder({ fnHowToSubmit }) { _mediaRecorder.onresume = () => setState('Recording...'); } - function stopAndSubmit(skipSubmission) { - if (_mediaRecorder && _mediaRecorder.state !== 'inactive') _mediaRecorder.stop(); - - _stream.getTracks().forEach((track) => track.stop()); - + function stopAndSubmit(skipSubmission = false) { if (!skipSubmission) { - setTimeout(() => { + _mediaRecorder.ondataavailable = (event) => { + const audioChunks = []; + audioChunks.push(event.data); _mediaRecorder = null; _stream = null; @@ -110,8 +96,12 @@ function VoiceMailRecorder({ fnHowToSubmit }) { const audioFile = new File([audioBlob], 'voicemail.webm', opts); fnHowToSubmit(audioFile); - }, 300); // TODO: Fix this hack, stop does not mean dataavailable but its going to happen soon + }; } + + // Stop recording, remove browser indicator + if (_mediaRecorder && _mediaRecorder.state !== 'inactive') _mediaRecorder.stop(); + _stream.getTracks().forEach((track) => track.stop()); } useEffect(() => { diff --git a/src/app/organisms/room/AttachmentUis/VoiceMailRecorder.scss b/src/app/organisms/room/AttachmentUis/VoiceMailRecorder.scss index 3f24e584..f30369bc 100644 --- a/src/app/organisms/room/AttachmentUis/VoiceMailRecorder.scss +++ b/src/app/organisms/room/AttachmentUis/VoiceMailRecorder.scss @@ -5,7 +5,6 @@ div { width: 150px; max-width: 100%; - // max-width: ; } } diff --git a/src/app/organisms/room/RoomViewInput.jsx b/src/app/organisms/room/RoomViewInput.jsx index 4d1d2f7e..36a37e55 100644 --- a/src/app/organisms/room/RoomViewInput.jsx +++ b/src/app/organisms/room/RoomViewInput.jsx @@ -297,14 +297,14 @@ function RoomViewInput({ const handleAttachmentTypeSelectorReturn = (ret) => { switch (ret) { case attachmentUiFrameTypes.none: - setAttachmentOrUi(attachmentUiFrameTypes.none); + setAttachmentOrUi(ret); roomsInput.cancelAttachment(roomId); break; case attachmentUiFrameTypes.file: uploadInputRef.current.click(); break; case attachmentUiFrameTypes.voiceMailRecorder: - setAttachmentOrUi(attachmentUiFrameTypes.voiceMailRecorder); + setAttachmentOrUi(ret); break; default: console.log('unhandled attachment type selector return');