fix(settings): close microphone access navigating away from page (#219)

Co-authored-by: Paul Makles <paulmakles@gmail.com>
This commit is contained in:
David Epely 2021-10-31 22:52:20 +01:00 committed by GitHub
parent d5c8749b80
commit 3ef3f84877
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,11 +29,11 @@ export function Component() {
const askOrGetPermission = async () => { const askOrGetPermission = async () => {
try { try {
const mediaStream = await navigator.mediaDevices.getUserMedia( const result = await navigator.mediaDevices.getUserMedia(
constraints, constraints,
); );
setMediaStream(mediaStream); setMediaStream(result);
} catch (err) { } catch (err) {
// The user has blocked the permission // The user has blocked the permission
setError(err as DOMException); setError(err as DOMException);
@ -42,17 +42,28 @@ export function Component() {
try { try {
const { state } = await navigator.permissions.query({ const { state } = await navigator.permissions.query({
// eslint-disable-next-line // eslint-disable-next-line
// @ts-ignore // @ts-ignore: very few browsers accept this `PermissionName`, but it has been drafted in https://www.w3.org/TR/permissions/#powerful-features-registry
name: "microphone", name: "microphone",
}); });
setPermission(state); setPermission(state);
} catch (err) { } catch (err) {
// the browser might not support `query` functionnality // the browser might not support `query` functionnality or `PermissionName`
setError(err as DOMException); // nothing to do
} }
}; };
useEffect(() => {
return () => {
if (mediaStream) {
// close microphone access on unmount
mediaStream.getTracks().forEach(track => {
track.stop()
})
}
}
}, [mediaStream]);
useEffect(() => { useEffect(() => {
if (!mediaStream) { if (!mediaStream) {
return; return;