mirror of
https://github.com/revoltchat/revite.git
synced 2024-11-29 02:10:59 -05:00
fix(settings): close microphone access navigating away from page (#219)
Co-authored-by: Paul Makles <paulmakles@gmail.com>
This commit is contained in:
parent
d5c8749b80
commit
3ef3f84877
1 changed files with 16 additions and 5 deletions
|
@ -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;
|
||||||
|
|
Loading…
Reference in a new issue