Skip to content

Commit

Permalink
fix: Improved permissions checks performance
Browse files Browse the repository at this point in the history
  • Loading branch information
richiemcilroy committed Mar 18, 2024
1 parent 792d4c1 commit d3856eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion apps/desktop/src-tauri/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>com.apple.security.device.usb</key>
<true/>
<key>com.apple.security.device.camera</key>
<string>This app requires access to the camera for showing camera preview.</string>
<true/>
<key>com.apple.security.device.microphone</key>
<true/>
<key>com.apple.security.device.audio-input</key>
Expand Down
58 changes: 30 additions & 28 deletions apps/desktop/src/components/windows/Permissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,45 @@ export const Permissions = () => {
};

const checkCameraAccess = async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
// Assuming access is granted if the above line doesn't throw an error
await savePermissions("camera", true);
setPermissions((prev) => ({
...prev,
camera: true,
}));
// Stop using the camera after checking access
stream.getTracks().forEach((track) => track.stop());
} catch (error) {
console.log("Camera access denied");
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: true,
});
await savePermissions("camera", true);
setPermissions((prev) => ({
...prev,
camera: true,
}));
stream.getTracks().forEach((track) => track.stop());
} catch (error) {
console.log("Camera access denied");
}
}
};

const checkMicrophoneAccess = async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
// Assuming access is granted if the above line doesn't throw an error
await savePermissions("microphone", true);
setPermissions((prev) => ({
...prev,
microphone: true,
}));
// Stop using the microphone after checking access
stream.getTracks().forEach((track) => track.stop());
} catch (error) {
console.log("Microphone access denied");
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
try {
const stream = await navigator.mediaDevices.getUserMedia({
audio: true,
});
await savePermissions("microphone", true);
setPermissions((prev) => ({
...prev,
microphone: true,
}));
stream.getTracks().forEach((track) => track.stop());
} catch (error) {
console.log("Microphone access denied");
}
}
};

useEffect(() => {
const checkPermissions = async () => {
if (!permissions.screen) {
checkScreenCapture();
await checkScreenCapture();
}
if (!permissions.camera) {
await checkCameraAccess();
Expand All @@ -73,9 +77,7 @@ export const Permissions = () => {
}
};

const checkPermissionInterval = setInterval(checkPermissions, 1000);

return () => clearInterval(checkPermissionInterval);
checkPermissions();
}, [permissions]);

const handlePermissionOpened = (permission: string) => {
Expand Down

1 comment on commit d3856eb

@vercel
Copy link

@vercel vercel bot commented on d3856eb Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.