Skip to content

Commit 9722306

Browse files
committed
fix: count down
1 parent baced47 commit 9722306

File tree

1 file changed

+13
-6
lines changed
  • apps/agent-tars/src/renderer/src/components/ChatUI

1 file changed

+13
-6
lines changed

apps/agent-tars/src/renderer/src/components/ChatUI/Replay.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function Replay() {
4646
const { addMessage, updateMessage, setMessages, messageEndRef, messages } =
4747
useAppChat();
4848
const timerRef = useRef<NodeJS.Timeout>();
49+
const intervalRef = useRef<NodeJS.Timeout>();
4950
const [buttonState, setButtonState] = useState<ButtonState>('replay');
5051
const [countdown, setCountdown] = useState(DEFAULT_COUNTDOWN);
5152
const playbackRef = useRef<{
@@ -60,30 +61,35 @@ export function Replay() {
6061
}
6162
};
6263

64+
const clearCountDownInterval = () => {
65+
setCountdown(0);
66+
if (intervalRef.current) {
67+
clearInterval(intervalRef.current);
68+
intervalRef.current = undefined;
69+
}
70+
};
71+
6372
useEffect(() => {
6473
return () => {
6574
clearPlayTimer();
75+
clearCountDownInterval();
6676
};
6777
}, []);
6878

6979
useEffect(() => {
70-
let countDownInterval: NodeJS.Timeout;
7180
if (isReportHtmlMode && allMessages.length) {
72-
countDownInterval = setInterval(() => {
81+
intervalRef.current = setInterval(() => {
7382
setCountdown((prevCountdown) => {
7483
if (prevCountdown > 1) {
7584
return prevCountdown - 1;
7685
} else {
77-
clearInterval(countDownInterval);
86+
clearCountDownInterval();
7887
handleTogglePlay();
7988
return 0;
8089
}
8190
});
8291
}, 1000);
8392
}
84-
return () => {
85-
countDownInterval && clearInterval(countDownInterval);
86-
};
8793
}, [allMessages]);
8894

8995
useEffect(() => {
@@ -158,6 +164,7 @@ export function Replay() {
158164
};
159165

160166
const handleTogglePlay = () => {
167+
clearCountDownInterval();
161168
switch (buttonState) {
162169
case 'replay':
163170
case 'continue':

0 commit comments

Comments
 (0)