Skip to content

Commit 7f5bdad

Browse files
committed
next article timer
1 parent 1c62d16 commit 7f5bdad

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/components/ListenToPlayer/index.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const ListenToPlayer = ({ slug }) => {
1919
const [autoplay, setAutoplay] = useState(true)
2020
const [playbackSpeed, setPlaybackSpeed] = useState(1)
2121
const [currentTrackIndex, setCurrentTrackIndex] = useState(index)
22+
const [countdown, setCountdown] = useState(0)
2223
const duration = sound?.duration() ?? 0
2324

2425
useEffect(() => {
@@ -33,7 +34,18 @@ const ListenToPlayer = ({ slug }) => {
3334
onpause: () => setIsPlaying(false),
3435
onend: () => {
3536
if (autoplay && currentTrackIndex < playlist.length - 1) {
36-
setCurrentTrackIndex(currentTrackIndex + 1)
37+
setCountdown(5)
38+
const countdownInterval = setInterval(() => {
39+
setCountdown((prev) => Math.max(0, prev - 1))
40+
}, 1000)
41+
const timer = setTimeout(() => {
42+
clearInterval(countdownInterval)
43+
setCurrentTrackIndex(currentTrackIndex + 1)
44+
}, 5000)
45+
return () => {
46+
clearTimeout(timer)
47+
clearInterval(countdownInterval)
48+
}
3749
} else {
3850
setIsPlaying(false)
3951
}
@@ -122,6 +134,11 @@ const ListenToPlayer = ({ slug }) => {
122134
setPlaybackSpeed(speed)
123135
}
124136

137+
const title =
138+
countdown > 0
139+
? `Next article in ${countdown}s`
140+
: t(playlist[currentTrackIndex].title)
141+
125142
return (
126143
<>
127144
<TopOfPagePlayer
@@ -135,7 +152,7 @@ const ListenToPlayer = ({ slug }) => {
135152
autoplay={autoplay}
136153
setAutoplay={setAutoplay}
137154
showWidget={showWidget}
138-
title={t(playlist[currentTrackIndex].title)}
155+
title={title}
139156
duration={duration}
140157
timeRemaining={timeRemaining}
141158
onSeek={handleSeek}

0 commit comments

Comments
 (0)