Skip to content

Commit

Permalink
Fix Camera Capture on Retake and Close (#9180)
Browse files Browse the repository at this point in the history
  • Loading branch information
JavidSumra authored Nov 27, 2024
1 parent e309fcd commit f8c78fd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
13 changes: 11 additions & 2 deletions src/components/Files/AudioCaptureDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Link } from "raviger";
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";

import CareIcon from "@/CAREUI/icons/CareIcon";
Expand All @@ -26,6 +26,7 @@ export default function AudioCaptureDialog(props: AudioCaptureDialogProps) {

const { show, onHide, onCapture, autoRecord = false } = props;
const [status, setStatus] = useState<Status | null>(null);
const mediaStreamRef = useRef<MediaStream | null>(null);
const { t } = useTranslation();

const { audioURL, resetRecording, startRecording, stopRecording } =
Expand All @@ -42,7 +43,8 @@ export default function AudioCaptureDialog(props: AudioCaptureDialogProps) {
const handleStartRecording = () => {
navigator.mediaDevices
.getUserMedia({ audio: true })
.then(() => {
.then((stream) => {
mediaStreamRef.current = stream;
setStatus("RECORDING");
startRecording();
timer.start();
Expand Down Expand Up @@ -105,6 +107,13 @@ export default function AudioCaptureDialog(props: AudioCaptureDialogProps) {
if (autoRecord && show && status === "RECORDING") {
handleStartRecording();
}

return () => {
if (mediaStreamRef.current) {
mediaStreamRef.current.getTracks().forEach((track) => track.stop());
mediaStreamRef.current = null;
}
};
}, [autoRecord, status, show]);

return (
Expand Down
7 changes: 6 additions & 1 deletion src/components/Files/CameraCaptureDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export interface CameraCaptureDialogProps {
show: boolean;
onHide: () => void;
onCapture: (file: File, fileName: string) => void;
onResetCapture: () => void;
}

export default function CameraCaptureDialog(props: CameraCaptureDialogProps) {
const { show, onHide, onCapture } = props;
const { show, onHide, onCapture, onResetCapture } = props;
const isLaptopScreen = useBreakpoints({ lg: true, default: false });

const [cameraFacingMode, setCameraFacingMode] = useState(
Expand Down Expand Up @@ -160,6 +161,7 @@ export default function CameraCaptureDialog(props: CameraCaptureDialogProps) {
<ButtonV2
onClick={() => {
setPreviewImage(null);
onResetCapture();
}}
className="m-2"
>
Expand All @@ -183,6 +185,7 @@ export default function CameraCaptureDialog(props: CameraCaptureDialogProps) {
variant="secondary"
onClick={() => {
setPreviewImage(null);
onResetCapture();
onHide();
}}
className="m-2"
Expand Down Expand Up @@ -221,6 +224,7 @@ export default function CameraCaptureDialog(props: CameraCaptureDialogProps) {
<ButtonV2
onClick={() => {
setPreviewImage(null);
onResetCapture();
}}
>
{t("retake")}
Expand All @@ -242,6 +246,7 @@ export default function CameraCaptureDialog(props: CameraCaptureDialogProps) {
variant="secondary"
onClick={() => {
setPreviewImage(null);
onResetCapture();
onHide();
}}
>
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/useFileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ export default function useFileUpload(
setUploadFileNames([]);
};

const clearFiles = () => {
setFiles([]);
setUploadFileNames([]);
};

const Dialogues = (
<>
<CameraCaptureDialog
Expand All @@ -257,6 +262,7 @@ export default function useFileUpload(
onCapture={(file) => {
setFiles((prev) => [...prev, file]);
}}
onResetCapture={clearFiles}
/>
<AudioCaptureDialog
show={audioModalOpen}
Expand Down Expand Up @@ -309,10 +315,7 @@ export default function useFileUpload(
setFiles((prev) => prev.filter((_, i) => i !== index));
setUploadFileNames((prev) => prev.filter((_, i) => i !== index));
},
clearFiles: () => {
setFiles([]);
setUploadFileNames([]);
},
clearFiles,
uploading,
};
}

0 comments on commit f8c78fd

Please sign in to comment.