diff --git a/src/hooks/usePermission.ts b/src/hooks/usePermission.ts index c8a4ef8..21e2e70 100644 --- a/src/hooks/usePermission.ts +++ b/src/hooks/usePermission.ts @@ -19,7 +19,7 @@ export function usePermissions() { useEffect(() => { let isMounted = true; - const initPermissions = async () => { + const checkAll = async () => { const [gps, camera] = await Promise.all([ checkGpsPermission(), checkCameraPermission(), @@ -30,35 +30,58 @@ export function usePermissions() { } }; - initPermissions(); + checkAll(); + + // 1. Listen for standard Permissions API change events (Works best in Chrome/Firefox) + let cleanupGpsStatus: (() => void) | undefined; + let cleanupCamStatus: (() => void) | undefined; if (typeof window !== 'undefined' && 'permissions' in navigator) { - const registerListener = async ( - permissionName: PermissionName, - setter: (val: PermissionStateResult) => void - ) => { - try { - const status = await navigator.permissions.query({ name: permissionName }); - const handleChange = () => setter(status.state); - status.addEventListener('change', handleChange); - return () => status.removeEventListener('change', handleChange); - } catch { - return () => {}; - } - }; + navigator.permissions.query({ name: 'geolocation' }) + .then((status) => { + const onChange = () => isMounted && setGpsStatus(status.state); + status.addEventListener('change', onChange); + cleanupGpsStatus = () => status.removeEventListener('change', onChange); + }) + .catch(() => {}); - const cleanupGps = registerListener('geolocation', (val) => isMounted && setGpsStatus(val)); - const cleanupCamera = registerListener('camera' as PermissionName, (val) => isMounted && setCameraStatus(val)); - - return () => { - isMounted = false; - cleanupGps.then((cleanup) => cleanup()); - cleanupCamera.then((cleanup) => cleanup()); - }; + navigator.permissions.query({ name: 'camera' as PermissionName }) + .then((status) => { + const onChange = () => isMounted && setCameraStatus(status.state); + status.addEventListener('change', onChange); + cleanupCamStatus = () => status.removeEventListener('change', onChange); + }) + .catch(() => {}); } + const handleDeviceChange = () => { + if (isMounted) checkAll(); + }; + + if (typeof window !== 'undefined' && navigator.mediaDevices) { + navigator.mediaDevices.addEventListener('devicechange', handleDeviceChange); + } + + const handleFocusOrVisibility = () => { + if (document.visibilityState === 'visible' && isMounted) { + checkAll(); + } + }; + + window.addEventListener('focus', handleFocusOrVisibility); + document.addEventListener('visibilitychange', handleFocusOrVisibility); + return () => { isMounted = false; + if (cleanupGpsStatus) cleanupGpsStatus(); + if (cleanupCamStatus) cleanupCamStatus(); + + if (typeof window !== 'undefined' && navigator.mediaDevices) { + navigator.mediaDevices.removeEventListener('devicechange', handleDeviceChange); + } + + window.removeEventListener('focus', handleFocusOrVisibility); + document.removeEventListener('visibilitychange', handleFocusOrVisibility); }; }, []);