Skip to content

Commit 064aa45

Browse files
authored
Add Wake Lock to transcription page to keep screen on
1 parent 89d291a commit 064aa45

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/public/transcription/TranscriptionApp.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { usePasswordProtectedEvent } from '../hooks/usePasswordProtectedEvent'
55
import { useTalkSelection } from './useTalkSelection'
66
import { useAutoAdvanceAfterTalk } from './useAutoAdvanceAfterTalk'
77
import { LiveTranscriptionView } from './LiveTranscriptionView'
8+
import { useWakeLock } from './useWakeLock'
89
import { DateTime } from 'luxon'
910

1011
export type PublicEventTranscriptionProps = {
@@ -16,6 +17,8 @@ export const TranscriptionApp = ({ eventId }: PublicEventTranscriptionProps) =>
1617
const [selectedTrack, setSelectedTrack] = useLocalStorage<string>('selectedTrack', '')
1718
const [tempPagePassword, saveTempPagePassword] = useState<string>('')
1819

20+
useWakeLock()
21+
1922
const { reply, eventData, isLoading, error } = usePasswordProtectedEvent(eventId, pagePassword)
2023
const gladiaAPIKey = reply?.gladiaAPIKey
2124

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { useEffect, useRef } from 'react'
2+
3+
/**
4+
* Requests a screen wake lock so the display stays on while the component is mounted.
5+
* The lock is automatically released when the component unmounts or when the page
6+
* becomes hidden, and re-acquired when the page becomes visible again.
7+
*/
8+
export const useWakeLock = () => {
9+
const wakeLockRef = useRef<WakeLockSentinel | null>(null)
10+
11+
useEffect(() => {
12+
if (!('wakeLock' in navigator)) return
13+
14+
const acquire = async () => {
15+
try {
16+
wakeLockRef.current = await navigator.wakeLock.request('screen')
17+
} catch {
18+
// Wake lock request may fail if the document is not visible or the
19+
// browser / OS denies the request. Fail silently.
20+
}
21+
}
22+
23+
const handleVisibilityChange = () => {
24+
if (document.visibilityState === 'visible') {
25+
acquire()
26+
}
27+
}
28+
29+
acquire()
30+
document.addEventListener('visibilitychange', handleVisibilityChange)
31+
32+
return () => {
33+
document.removeEventListener('visibilitychange', handleVisibilityChange)
34+
wakeLockRef.current?.release()
35+
wakeLockRef.current = null
36+
}
37+
}, [])
38+
}

0 commit comments

Comments
 (0)