Skip to content

Commit 155cced

Browse files
authored
Merge pull request webdevcody#707 from kovacevjosip/hide-play-pause-on-replay-finish
Hide play/pause control when replay is finished
2 parents 49ac987 + c2db0cc commit 155cced

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

packages/app/src/app/result/replay-timestamps.tsx

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const ReplayCode = ({ code }: TReplayCode) => {
1818
const [replayTimeStamp, setReplayTimeStamp] = useState<ReplayTimeStamp[]>([]);
1919
const [currentIndex, setCurrentIndex] = useState(0);
2020
const [isPlaying, setIsPlaying] = useState(false);
21+
const [isPlayingFinished, setIsPlayingFinished] = useState(false);
2122

2223
useEffect(() => {
2324
const getReplay = () => {
@@ -29,24 +30,22 @@ export const ReplayCode = ({ code }: TReplayCode) => {
2930
}, []);
3031

3132
useEffect(() => {
32-
let timeout: NodeJS.Timeout;
33-
34-
if (isPlaying && replayTimeStamp && currentIndex < replayTimeStamp.length) {
35-
const currentTimestamp = replayTimeStamp[currentIndex];
36-
let nextTimestampDelay = 0;
37-
38-
if (currentIndex + 1 < replayTimeStamp.length) {
39-
nextTimestampDelay =
40-
replayTimeStamp[currentIndex + 1].time - currentTimestamp.time;
41-
} else {
42-
return;
43-
}
44-
45-
timeout = setTimeout(() => {
46-
setCurrentIndex(currentIndex + 1);
47-
}, nextTimestampDelay);
33+
const isPlayingActive = isPlaying && replayTimeStamp;
34+
if (!isPlayingActive) return;
35+
36+
const nextIndex = currentIndex + 1;
37+
if (nextIndex === replayTimeStamp.length) {
38+
setIsPlayingFinished(true);
39+
return;
4840
}
4941

42+
const currentTimestamp = replayTimeStamp[currentIndex];
43+
const nextTimestampDelay = replayTimeStamp[nextIndex].time - currentTimestamp.time;
44+
45+
const timeout = setTimeout(() => {
46+
setCurrentIndex(currentIndex + 1);
47+
}, nextTimestampDelay);
48+
5049
return () => clearTimeout(timeout);
5150
}, [currentIndex, isPlaying, replayTimeStamp]);
5251

@@ -55,6 +54,7 @@ export const ReplayCode = ({ code }: TReplayCode) => {
5554
};
5655

5756
const handleRestart = () => {
57+
setIsPlayingFinished(false);
5858
setIsPlaying(false);
5959
setCurrentIndex(0);
6060
};
@@ -70,16 +70,18 @@ export const ReplayCode = ({ code }: TReplayCode) => {
7070
return (
7171
<div className="py-2 w-full bg-accent text-2xl text-primary relative group">
7272
<div className="opacity-0 group-hover:opacity-100 absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 flex space-x-2 z-10">
73-
{isPlaying ? (
74-
<PauseIcon
75-
onClick={handlePlayPause}
76-
className="w-12 h-12 text-primary cursor-pointer"
77-
/>
78-
) : (
79-
<PlayIcon
80-
onClick={handlePlayPause}
81-
className="w-12 h-12 text-primary cursor-pointer"
82-
/>
73+
{!isPlayingFinished && (
74+
isPlaying ? (
75+
<PauseIcon
76+
onClick={handlePlayPause}
77+
className="w-12 h-12 text-primary cursor-pointer"
78+
/>
79+
) : (
80+
<PlayIcon
81+
onClick={handlePlayPause}
82+
className="w-12 h-12 text-primary cursor-pointer"
83+
/>
84+
)
8385
)}
8486

8587
<RefreshCcw

0 commit comments

Comments
 (0)