@@ -18,6 +18,7 @@ export const ReplayCode = ({ code }: TReplayCode) => {
18
18
const [ replayTimeStamp , setReplayTimeStamp ] = useState < ReplayTimeStamp [ ] > ( [ ] ) ;
19
19
const [ currentIndex , setCurrentIndex ] = useState ( 0 ) ;
20
20
const [ isPlaying , setIsPlaying ] = useState ( false ) ;
21
+ const [ isPlayingFinished , setIsPlayingFinished ] = useState ( false ) ;
21
22
22
23
useEffect ( ( ) => {
23
24
const getReplay = ( ) => {
@@ -29,24 +30,22 @@ export const ReplayCode = ({ code }: TReplayCode) => {
29
30
} , [ ] ) ;
30
31
31
32
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 ;
48
40
}
49
41
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
+
50
49
return ( ) => clearTimeout ( timeout ) ;
51
50
} , [ currentIndex , isPlaying , replayTimeStamp ] ) ;
52
51
@@ -55,6 +54,7 @@ export const ReplayCode = ({ code }: TReplayCode) => {
55
54
} ;
56
55
57
56
const handleRestart = ( ) => {
57
+ setIsPlayingFinished ( false ) ;
58
58
setIsPlaying ( false ) ;
59
59
setCurrentIndex ( 0 ) ;
60
60
} ;
@@ -70,16 +70,18 @@ export const ReplayCode = ({ code }: TReplayCode) => {
70
70
return (
71
71
< div className = "py-2 w-full bg-accent text-2xl text-primary relative group" >
72
72
< 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
+ )
83
85
) }
84
86
85
87
< RefreshCcw
0 commit comments