From 1ce382a4544013de0fd3d5decfeb173342461da1 Mon Sep 17 00:00:00 2001 From: Sara Date: Tue, 26 Sep 2023 15:33:26 +0200 Subject: [PATCH] lock wake for all browsers but firefox --- www/app/lib/wakeLock.ts | 28 ++++++++++++++++++++++++++++ www/app/transcripts/new/page.tsx | 8 ++++++++ 2 files changed, 36 insertions(+) create mode 100644 www/app/lib/wakeLock.ts diff --git a/www/app/lib/wakeLock.ts b/www/app/lib/wakeLock.ts new file mode 100644 index 00000000..6ee6e1ed --- /dev/null +++ b/www/app/lib/wakeLock.ts @@ -0,0 +1,28 @@ +// Not possible yet in firefox +// https://bugzilla.mozilla.org/show_bug.cgi?id=1589554 + +const canWakeLock = () => "wakeLock" in navigator; + +let wakelock: WakeLockSentinel | undefined; +async function lockWakeState() { + if (!canWakeLock()) return; + try { + wakelock = await navigator.wakeLock.request(); + wakelock.addEventListener("release", () => { + console.log( + "Screen Wake State Locked:", + wakelock ? !wakelock?.released : false, + ); + }); + console.log("Screen Wake State Locked:", !wakelock.released); + } catch (e) { + console.error("Failed to lock wake state with reason:", e.message); + } +} + +function releaseWakeState() { + if (wakelock) wakelock.release(); + wakelock = undefined; +} + +export { lockWakeState, releaseWakeState }; diff --git a/www/app/transcripts/new/page.tsx b/www/app/transcripts/new/page.tsx index 9c32bd8a..33681cca 100644 --- a/www/app/transcripts/new/page.tsx +++ b/www/app/transcripts/new/page.tsx @@ -15,6 +15,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faGear } from "@fortawesome/free-solid-svg-icons"; import About from "../../(aboutAndPrivacy)/about"; import Privacy from "../../(aboutAndPrivacy)/privacy"; +import { lockWakeState, releaseWakeState } from "../../lib/wakeLock"; const TranscriptCreate = () => { const [stream, setStream] = useState(null); @@ -45,6 +46,13 @@ const TranscriptCreate = () => { } = useAudioDevice(); const [hasRecorded, setHasRecorded] = useState(false); + useEffect(() => { + lockWakeState(); + return () => { + releaseWakeState(); + }; + }, []); + return ( <> {permissionOk ? (