()
| 3 | import { useEffect, useRef } from 'react'; |
| 4 | |
| 5 | export function useWakeLock() { |
| 6 | const wakeLockRef = useRef<WakeLockSentinel | null>(null); |
| 7 | |
| 8 | useEffect(() => { |
| 9 | const requestWakeLock = async () => { |
| 10 | try { |
| 11 | if ('wakeLock' in navigator) { |
| 12 | wakeLockRef.current = await navigator.wakeLock.request('screen'); |
| 13 | console.log('Wake Lock activated'); |
| 14 | |
| 15 | wakeLockRef.current.addEventListener('release', () => { |
| 16 | console.log('Wake Lock released'); |
| 17 | }); |
| 18 | } |
| 19 | } catch (err) { |
| 20 | console.error('Wake Lock request failed:', err); |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | const handleVisibilityChange = () => { |
| 25 | if (wakeLockRef.current && document.visibilityState === 'visible') { |
| 26 | requestWakeLock(); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | requestWakeLock(); |
| 31 | document.addEventListener('visibilitychange', handleVisibilityChange); |
| 32 | |
| 33 | return () => { |
| 34 | document.removeEventListener('visibilitychange', handleVisibilityChange); |
| 35 | |
| 36 | if (wakeLockRef.current) { |
| 37 | wakeLockRef.current.release().catch((err) => { |
| 38 | console.error('Wake Lock release failed:', err); |
| 39 | }); |
| 40 | wakeLockRef.current = null; |
| 41 | } |
| 42 | }; |
| 43 | }, []); |
| 44 | } |
no test coverage detected