Skip to content

Commit 22787c7

Browse files
author
Sravan S
authored
feat: Show modal when there is no recoding permission (#605)
Fixes: https://sendbird.atlassian.net/browse/UIKIT-3925
1 parent c8ffe4c commit 22787c7

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/hooks/VoiceRecorder/index.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import React, { createContext, useCallback, useContext, useEffect, useState } from 'react';
2+
3+
import { useLocalization } from '../../lib/LocalizationContext';
4+
import Modal from '../../ui/Modal';
25
import {
36
BROWSER_SUPPORT_MIME_TYPE_LIST,
47
VOICE_MESSAGE_FILE_NAME,
@@ -36,6 +39,25 @@ export const VoiceRecorderProvider = (props: VoiceRecorderProps): React.ReactEle
3639
const { logger, isVoiceMessageEnabled } = config;
3740
const [mediaRecorder, setMediaRecorder] = useState<MediaRecorder>(null);
3841
const [isRecordable, setIsRecordable] = useState<boolean>(false);
42+
const [permissionWarning, setPermissionWarning] = useState<boolean>(false);
43+
const { stringSet } = useLocalization();
44+
45+
const checkPermission = () => {
46+
try {
47+
// Type '"microphone"' is not assignable to type 'PermissionName'.ts(2322)
48+
// this is typescript issue
49+
// https://github.com/microsoft/TypeScript/issues/33923
50+
// @ts-expect-error
51+
navigator.permissions.query({ name: 'microphone' }).then((result) => {
52+
if (result.state === 'denied') {
53+
logger.warning('VoiceRecorder: Permission denied.');
54+
setPermissionWarning(true);
55+
}
56+
});
57+
} catch (error) {
58+
logger.warning('VoiceRecorder: Failed to check permission.', error);
59+
}
60+
};
3961

4062
const browserSupportMimeType = BROWSER_SUPPORT_MIME_TYPE_LIST.find((mimeType) => MediaRecorder.isTypeSupported(mimeType)) ?? '';
4163
if (!browserSupportMimeType) {
@@ -62,6 +84,7 @@ export const VoiceRecorderProvider = (props: VoiceRecorderProps): React.ReactEle
6284
stop();
6385
logger.info('VoiceRecorder: Previous mediaRecorder is stopped.');
6486
}
87+
checkPermission();
6588
navigator?.mediaDevices?.getUserMedia?.({ audio: true })
6689
.then((stream) => {
6790
logger.info('VoiceRecorder: Succeeded getting media stream.', stream);
@@ -114,6 +137,16 @@ export const VoiceRecorderProvider = (props: VoiceRecorderProps): React.ReactEle
114137
isRecordable,
115138
}}>
116139
{children}
140+
{
141+
permissionWarning && (
142+
<Modal
143+
hideFooter
144+
onCancel={() => setPermissionWarning(false)}
145+
>
146+
<>{stringSet.VOICE_RECORDING_PERMISSION_DENIED}</>
147+
</Modal>
148+
)
149+
}
117150
</Context.Provider>
118151
);
119152
};

src/ui/Label/stringSet.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ const getStringSet = (lang = 'en') => {
190190
MENTION_COUNT__OVER_LIMIT: 'You can mention up to %d times at a time.',
191191
UI__FILE_VIEWER__UNSUPPORT: 'Unsupported message',
192192
// Feature - Voice Message
193+
VOICE_RECORDING_PERMISSION_DENIED: `You cannot record the voice since
194+
voice recording is not permitted in your device system setting`,
193195
VOICE_MESSAGE: 'Voice Message',
194196
},
195197
};

0 commit comments

Comments
 (0)