Skip to content

Commit 36d83ee

Browse files
fix: Read Aloud audio playback stops when switching to another section (#2020)
1 parent e776548 commit 36d83ee

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

code/frontend/src/components/Answer/Answer.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ export const Answer = ({
129129
}
130130
}, [isActive, synthesizer]);
131131

132+
useEffect(() => {
133+
return () => {
134+
if (isSpeaking) {
135+
resetSpeech();
136+
}
137+
};
138+
}, []);
139+
132140
useEffect(() => {
133141
setChevronIsExpanded(isRefAccordionOpen);
134142
// if (chevronIsExpanded && refContainer.current) {
@@ -221,7 +229,7 @@ export const Answer = ({
221229
const handleSpeakPauseResume = () => {
222230
if (isSpeaking) {
223231
if (isPaused) {
224-
onSpeak(index, "speak");
232+
onSpeak(index, "speak", resetSpeech);
225233
audioDestination?.resume();
226234
setIsPaused(false);
227235
setStartTime(Date.now());
@@ -238,7 +246,7 @@ export const Answer = ({
238246
}
239247
}
240248
} else {
241-
onSpeak(index, "speak");
249+
onSpeak(index, "speak", resetSpeech);
242250
startSpeech();
243251
}
244252
};

code/frontend/src/pages/chat/Chat.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const [ASSISTANT, TOOL, ERROR] = ["assistant", "tool", "error"];
3838
const Chat = () => {
3939
const lastQuestionRef = useRef<string>("");
4040
const chatMessageStreamEnd = useRef<HTMLDivElement | null>(null);
41+
const audioStopRef = useRef<(() => void) | null>(null);
4142
const [isLoading, setIsLoading] = useState<boolean>(false);
4243
const [isGenerating, setIsGenerating] = useState<boolean>(false); // Add this state
4344
const [showLoadingMessage, setShowLoadingMessage] = useState<boolean>(false);
@@ -303,11 +304,13 @@ const Chat = () => {
303304
};
304305

305306
const clearChat = () => {
307+
audioStopRef.current?.();
306308
lastQuestionRef.current = "";
307309
setActiveCitation(undefined);
308310
setAnswers([]);
309311
setConversationId(uuidv4());
310312
setSelectedConvId("");
313+
setActiveCardIndex(null);
311314
};
312315

313316
const stopGenerating = () => {
@@ -385,9 +388,12 @@ const Chat = () => {
385388
[]
386389
);
387390

388-
const handleSpeech = (index: number, status: string) => {
391+
const handleSpeech = (index: number, status: string, stopAudioFn?: () => void) => {
389392
if (status != "pause") setActiveCardIndex(index);
390393
setIsTextToSpeachActive(status == "speak" ? true : false);
394+
if (status == "speak" && stopAudioFn) {
395+
audioStopRef.current = stopAudioFn;
396+
}
391397
};
392398
const onSetShowHistoryPanel = () => {
393399
if (!showHistoryPanel) {
@@ -423,8 +429,15 @@ const Chat = () => {
423429
console.error("No conversation Id found");
424430
return;
425431
}
432+
433+
if (id !== selectedConvId) {
434+
audioStopRef.current?.();
435+
setActiveCardIndex(null);
436+
}
437+
426438
const messages = getMessagesByConvId(id);
427439
if (messages.length === 0) {
440+
setAnswers([]);
428441
setFetchingConvMessages(true);
429442
const responseMessages = await historyRead(id);
430443
setAnswers(responseMessages);
@@ -433,7 +446,10 @@ const Chat = () => {
433446
} else {
434447
setAnswers(messages);
435448
}
436-
setSelectedConvId(id);
449+
450+
if (id !== selectedConvId) {
451+
setSelectedConvId(id);
452+
}
437453
};
438454

439455
useEffect(() => {
@@ -477,6 +493,7 @@ const Chat = () => {
477493
);
478494
setChatHistory(tempChatHistory);
479495
if (id === selectedConvId) {
496+
audioStopRef.current?.();
480497
lastQuestionRef.current = "";
481498
setActiveCitation(undefined);
482499
setAnswers([]);

0 commit comments

Comments
 (0)