Skip to content

Commit

Permalink
fix: Removed unnecessary rendering of microphone permission warning i…
Browse files Browse the repository at this point in the history
…nfo. (#7404)

* fixed unnecessary placement of mic permission info

* added type void

* mic warning to render when mic permission is not enabled

* fixed auto mic permission popup

* Update src/Components/Patient/FileUpload.tsx

Suggested changes applied 1

Co-authored-by: Rithvik Nishad <[email protected]>

* Update src/Components/Patient/FileUpload.tsx

Suggested changes applied 2

Co-authored-by: Rithvik Nishad <[email protected]>

* Update src/Components/Patient/FileUpload.tsx

Suggested changes applied 3

Co-authored-by: Rithvik Nishad <[email protected]>

* fixed lint errors
  • Loading branch information
Spiral-Memory authored Apr 9, 2024
1 parent 34648e9 commit d615772
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
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

0 comments on commit d615772

Please sign in to comment.