diff --git a/client/src/hooks/use-wake-lock.ts b/client/src/hooks/use-wake-lock.ts new file mode 100644 index 00000000..e2912734 --- /dev/null +++ b/client/src/hooks/use-wake-lock.ts @@ -0,0 +1,112 @@ +import { useCallback, useEffect, useRef, useState } from "react"; + +/** + * A custom hook to manage a screen wake lock. + * + * @returns An object with an `isLocked` boolean and `request`/`release` functions. + */ +export const useWakeLock = () => { + const wakeLock = useRef(null); + const shouldHoldLock = useRef(false); + const requestInFlight = useRef(false); + const wakeLockSupported = useRef(null); + const [isLocked, setIsLocked] = useState(false); + + const request = useCallback(async () => { + if (wakeLockSupported.current === false) { + return; + } + + if (wakeLockSupported.current === null) { + wakeLockSupported.current = "wakeLock" in navigator; + if (!wakeLockSupported.current) { + console.warn("Screen Wake Lock API not supported"); + return; + } + } + + shouldHoldLock.current = true; + + if (wakeLock.current || requestInFlight.current) { + return; + } + + requestInFlight.current = true; + + try { + const sentinel = await navigator.wakeLock.request("screen"); + + // If release() was called while the request was in flight, + // release the new sentinel immediately and do nothing. + if (!shouldHoldLock.current) { + void sentinel.release(); + return; + } + + wakeLock.current = sentinel; + setIsLocked(true); + + sentinel.addEventListener( + "release", + () => { + if (wakeLock.current === sentinel) { + wakeLock.current = null; + setIsLocked(false); + } + }, + { once: true } + ); + } catch (err) { + console.error("Wake Lock request failed:", err); + setIsLocked(false); + } finally { + requestInFlight.current = false; + } + }, []); + + const release = useCallback(async () => { + shouldHoldLock.current = false; + + if (!wakeLock.current) { + setIsLocked(false); + return; + } + + try { + await wakeLock.current.release(); + } catch { + // ignore (it may have already been released by the browser) + } finally { + wakeLock.current = null; + setIsLocked(false); + } + }, []); + + useEffect(() => { + const handleVisibilityChange = () => { + if ( + shouldHoldLock.current && + !isLocked && + document.visibilityState === "visible" + ) { + void request(); + } + }; + + document.addEventListener("visibilitychange", handleVisibilityChange); + document.addEventListener("fullscreenchange", handleVisibilityChange); + + return () => { + document.removeEventListener("visibilitychange", handleVisibilityChange); + document.removeEventListener("fullscreenchange", handleVisibilityChange); + + shouldHoldLock.current = false; + const sentinel = wakeLock.current; + wakeLock.current = null; + requestInFlight.current = false; + void sentinel?.release(); + }; + }, [request, release, isLocked]); + + return { isLocked, request, release }; +}; diff --git a/client/src/pages/tools/timer.tsx b/client/src/pages/tools/timer.tsx index bf180fac..75130d96 100644 --- a/client/src/pages/tools/timer.tsx +++ b/client/src/pages/tools/timer.tsx @@ -31,6 +31,7 @@ import { } from "@/lib/time-tools"; import { getParam, updateURL, copyShareableURL } from "@/lib/url-sharing"; import { useToast } from "@/hooks/use-toast"; +import { useWakeLock } from "@/hooks/use-wake-lock"; import { ToolButton, ResetButton, @@ -68,6 +69,22 @@ export default function Timer() { const audioContextRef = useRef(null); const alarmIntervalsRef = useRef>(new Map()); const { toast } = useToast(); + const { + isLocked, + request: requestWakeLock, + release: releaseWakeLock, + } = useWakeLock(); + + // Effect to manage the wake lock + useEffect(() => { + const hasRunningTimers = timers.some(timer => timer.isRunning); + + if (hasRunningTimers) { + void requestWakeLock(); + } else { + void releaseWakeLock(); + } + }, [timers, isLocked, requestWakeLock, releaseWakeLock]); // Clear any alarm handle (interval or timeout) robustly const clearAlarmHandle = useCallback((handle: NodeJS.Timeout) => {