40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import { useEffect, useRef } from 'react';
|
|
|
|
export const useScreenWakeLock = (lock: boolean) => {
|
|
const wakeLockRef = useRef<WakeLockSentinel | null>(null);
|
|
|
|
useEffect(() => {
|
|
const requestWakeLock = async () => {
|
|
try {
|
|
if ('wakeLock' in navigator) {
|
|
wakeLockRef.current = await navigator.wakeLock.request('screen');
|
|
|
|
wakeLockRef.current.addEventListener('release', () => {
|
|
wakeLockRef.current = null;
|
|
});
|
|
|
|
console.log('Wake lock acquired');
|
|
}
|
|
} catch (err) {
|
|
console.error('Failed to acquire wake lock:', err);
|
|
}
|
|
};
|
|
|
|
if (lock) {
|
|
requestWakeLock();
|
|
} else if (wakeLockRef.current) {
|
|
wakeLockRef.current.release();
|
|
wakeLockRef.current = null;
|
|
console.log('Wake lock released');
|
|
}
|
|
|
|
return () => {
|
|
if (wakeLockRef.current) {
|
|
wakeLockRef.current.release();
|
|
wakeLockRef.current = null;
|
|
console.log('Wake lock released');
|
|
}
|
|
};
|
|
}, [lock]);
|
|
};
|