All files / hooks useRequestPermissions.ts

92.85% Statements 26/28
85.71% Branches 6/7
100% Functions 2/2
92.85% Lines 26/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 361x   1x 3x   3x 3x 3x     3x 3x 3x   3x 2x 2x 2x   2x 2x 2x 3x 1x 1x 1x 1x 3x     3x   3x 3x 3x  
import { useEffect } from "react";
 
export function useRequestPermissions() {
	useEffect(() => {
		// Request Notification permission if not already granted
		if ("Notification" in window && Notification.permission !== "granted") {
			Notification.requestPermission();
		}
 
		// Request Microphone permission if not already granted
		const requestMic = async () => {
			try {
				const nav = navigator as Navigator;
 
				if ("permissions" in nav && "mediaDevices" in nav) {
					const micStatus = await nav.permissions.query({
						name: "microphone" as PermissionName,
					});
 
					if (micStatus.state === "prompt") {
						await nav.mediaDevices.getUserMedia({ audio: true });
					}
				} else if ("mediaDevices" in nav) {
					await (nav as Navigator).mediaDevices.getUserMedia({
						audio: true,
					});
				}
			} catch (err) {
				console.warn("Microphone access denied or unavailable:", err);
			}
		};
 
		requestMic();
	}, []);
}