Turn off barcode reader after 15m timeout

A future enhancement would be to make sure it only times out 15m after
an album is done playing (possibly by ignoring signals while an album is
playing, and then at the end of the album set the timer again?) but that
wasn't something I've implemented yet.
This commit is contained in:
Chandler Swift 2026-02-18 20:37:13 -06:00
parent 31d0bddc38
commit 9bb7f8d147
Signed by: chandlerswift
GPG key ID: A851D929D52FB93F

View file

@ -62,6 +62,22 @@ function App() {
const [currentTime, setCurrentTime] = useState(0); const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0); const [duration, setDuration] = useState(0);
const audioRef = useRef<HTMLAudioElement | null>(null); const audioRef = useRef<HTMLAudioElement | null>(null);
const [lastClicked, setLastClicked] = useState(new Date);
const click = () => setLastClicked(new Date);
useEffect(() => {
const IDLE_TIMEOUT_MS = 15 * 60 * 1000; // 15 minutes
window.setTimeout(() => {
setDisplayText("Asleep. Tap screen to scan.");
fetch('/api/reader/auto', { method: 'POST' });
}, lastClicked.getTime() + IDLE_TIMEOUT_MS - new Date().getTime());
return () => {
setDisplayText(WELCOME_TEXT);
fetch('/api/reader/on', { method: 'POST' });
}
}, [lastClicked]);
const handleEnded = () => { const handleEnded = () => {
if ((album?.tracks?.length ?? 0) > nowPlaying + 1) { // if there's a next track if ((album?.tracks?.length ?? 0) > nowPlaying + 1) { // if there's a next track
@ -216,7 +232,7 @@ function App() {
<p className='welcome' style={{ gridColumn: 'span 2' }}>This album cannot be played, as Chandler doesn't own a copy.</p> <p className='welcome' style={{ gridColumn: 'span 2' }}>This album cannot be played, as Chandler doesn't own a copy.</p>
); );
return ( return (
<div className="app"> <div className="app" onPointerDown={click}>
<audio <audio
ref={audioRef} ref={audioRef}
src={album.tracks[nowPlaying].source} src={album.tracks[nowPlaying].source}
@ -234,7 +250,7 @@ function App() {
</div> </div>
) )
} else { } else {
return (<div className="welcome">{displayText}</div>); return (<div className="welcome" onPointerDown={click}>{displayText}</div>);
} }
} }