From c0ceb68faada77c5a1ea0d9d4f493273bc80873b Mon Sep 17 00:00:00 2001 From: nmemmert Date: Thu, 11 Jun 2026 16:28:12 -0400 Subject: [PATCH] Avoid side effect in state updater for audio chapter advance Co-Authored-By: Claude Fable 5 --- src/App.jsx | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index c75984c..cda8fa9 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -898,8 +898,10 @@ const App = () => { const audioRef = useRef(null); const audioBookRef = useRef(audioBook); const audioNarratorRef = useRef(audioNarrator); + const audioStateRef = useRef(audioState); audioBookRef.current = audioBook; audioNarratorRef.current = audioNarrator; + audioStateRef.current = audioState; const playAudioChapter = async (abbrev, chapterNum, narrator) => { const res = await fetch(`https://bible.helloao.org/api/BSB/${abbrev}/${chapterNum}.json`); @@ -911,16 +913,14 @@ const App = () => { if (!audioRef.current) { audioRef.current = new Audio(); audioRef.current.addEventListener('ended', () => { - setAudioState((prev) => { - const nextChapter = prev.chapter + 1; - if (nextChapter > prev.total) { - return { status: 'idle', chapter: 0, total: 0 }; - } - playAudioChapter(audioBookRef.current, nextChapter, audioNarratorRef.current).catch(() => - setAudioState({ status: 'error', chapter: 0, total: 0 }), - ); - return prev; - }); + const nextChapter = audioStateRef.current.chapter + 1; + if (nextChapter > audioStateRef.current.total) { + setAudioState({ status: 'idle', chapter: 0, total: 0 }); + return; + } + playAudioChapter(audioBookRef.current, nextChapter, audioNarratorRef.current).catch(() => + setAudioState({ status: 'error', chapter: 0, total: 0 }), + ); }); } const audioEl = audioRef.current;