Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Removed unnecessary rendering of microphone permission warning info. #7404

23 changes: 22 additions & 1 deletion src/Components/Patient/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,26 @@ export const FileUpload = (props: FileUploadProps) => {
{ name: "Unarchived Files", value: "UNARCHIVED" },
{ name: "Archived Files", value: "ARCHIVED" },
]);
const [isMicPermission, setIsMicPermission] = useState(true);

useEffect(() => {
const checkMicPermission = async () => {
try {
const permissions = await navigator.permissions.query({
name: "microphone" as PermissionName,
});
setIsMicPermission(permissions.state === "granted");
} catch (error) {
setIsMicPermission(false);
}
};

checkMicPermission();

return () => {
setIsMicPermission(true);
};
}, []);

const { data: patient } = useQuery(routes.getPatient, {
pathParams: { id: patientId },
Expand Down Expand Up @@ -1510,8 +1530,9 @@ export const FileUpload = (props: FileUploadProps) => {
confirmAudioBlobExists={confirmAudioBlobExists}
reset={resetRecording}
setResetRecording={setResetRecording}
handleSetMicPermission={setIsMicPermission}
/>
{!audioBlobExists && (
{!audioBlobExists && !isMicPermission && (
<span className="text-sm font-medium text-warning-500">
<CareIcon
icon="l-exclamation-triangle"
Expand Down
11 changes: 8 additions & 3 deletions src/Utils/VoiceRecorder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import { useTranslation } from "react-i18next";

export const VoiceRecorder = (props: any) => {
const { t } = useTranslation();
const { createAudioBlob, confirmAudioBlobExists, reset, setResetRecording } =
props;
const {
createAudioBlob,
confirmAudioBlobExists,
reset,
setResetRecording,
handleSetMicPermission,
} = props;
const [
audioURL,
isRecording,
startRecording,
stopRecording,
newBlob,
resetRecording,
] = useRecorder();
] = useRecorder(handleSetMicPermission);
const [time, setTime] = useState(0);
createAudioBlob(newBlob);
useEffect(() => {
Expand Down
19 changes: 14 additions & 5 deletions src/Utils/useRecorder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from "react";
import { Error } from "./Notifications";

const useRecorder = () => {
const useRecorder = (handleMicPermission) => {
const [audioURL, setAudioURL] = useState("");
const [isRecording, setIsRecording] = useState(false);
const [recorder, setRecorder] = useState(null);
Expand All @@ -11,10 +11,19 @@ const useRecorder = () => {
// Lazily obtain recorder first time we're recording.
if (recorder === null) {
if (isRecording) {
requestRecorder().then(setRecorder, () => {
Error({ msg: "Please grant microphone permission to record audio." });
setIsRecording(false);
});
requestRecorder().then(
(fetchedRecorder) => {
setRecorder(fetchedRecorder);
handleMicPermission(true);
},
() => {
Error({
msg: "Please grant microphone permission to record audio.",
});
setIsRecording(false);
handleMicPermission(false);
}
);
}
return;
}
Expand Down
Loading