@@ -46,6 +46,7 @@ export function Replay() {
46
46
const { addMessage, updateMessage, setMessages, messageEndRef, messages } =
47
47
useAppChat ( ) ;
48
48
const timerRef = useRef < NodeJS . Timeout > ( ) ;
49
+ const intervalRef = useRef < NodeJS . Timeout > ( ) ;
49
50
const [ buttonState , setButtonState ] = useState < ButtonState > ( 'replay' ) ;
50
51
const [ countdown , setCountdown ] = useState ( DEFAULT_COUNTDOWN ) ;
51
52
const playbackRef = useRef < {
@@ -60,30 +61,35 @@ export function Replay() {
60
61
}
61
62
} ;
62
63
64
+ const clearCountDownInterval = ( ) => {
65
+ setCountdown ( 0 ) ;
66
+ if ( intervalRef . current ) {
67
+ clearInterval ( intervalRef . current ) ;
68
+ intervalRef . current = undefined ;
69
+ }
70
+ } ;
71
+
63
72
useEffect ( ( ) => {
64
73
return ( ) => {
65
74
clearPlayTimer ( ) ;
75
+ clearCountDownInterval ( ) ;
66
76
} ;
67
77
} , [ ] ) ;
68
78
69
79
useEffect ( ( ) => {
70
- let countDownInterval : NodeJS . Timeout ;
71
80
if ( isReportHtmlMode && allMessages . length ) {
72
- countDownInterval = setInterval ( ( ) => {
81
+ intervalRef . current = setInterval ( ( ) => {
73
82
setCountdown ( ( prevCountdown ) => {
74
83
if ( prevCountdown > 1 ) {
75
84
return prevCountdown - 1 ;
76
85
} else {
77
- clearInterval ( countDownInterval ) ;
86
+ clearCountDownInterval ( ) ;
78
87
handleTogglePlay ( ) ;
79
88
return 0 ;
80
89
}
81
90
} ) ;
82
91
} , 1000 ) ;
83
92
}
84
- return ( ) => {
85
- countDownInterval && clearInterval ( countDownInterval ) ;
86
- } ;
87
93
} , [ allMessages ] ) ;
88
94
89
95
useEffect ( ( ) => {
@@ -158,6 +164,7 @@ export function Replay() {
158
164
} ;
159
165
160
166
const handleTogglePlay = ( ) => {
167
+ clearCountDownInterval ( ) ;
161
168
switch ( buttonState ) {
162
169
case 'replay' :
163
170
case 'continue' :
0 commit comments