From 23f382a4342cd970a23ac38e1dc825ee5b05492e Mon Sep 17 00:00:00 2001 From: kraken Date: Sat, 3 Aug 2024 02:12:29 +0530 Subject: feat: selective disabling of buttons --- apps/web/app/(dash)/chat/chatWindow.tsx | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/apps/web/app/(dash)/chat/chatWindow.tsx b/apps/web/app/(dash)/chat/chatWindow.tsx index eaa62b80..d186d0be 100644 --- a/apps/web/app/(dash)/chat/chatWindow.tsx +++ b/apps/web/app/(dash)/chat/chatWindow.tsx @@ -52,7 +52,8 @@ function ChatWindow({ }) { const [layout, setLayout] = useState<"chat" | "initial">("chat"); const [chatHistory, setChatHistory] = useState(initialChat); - const [isSpeaking, setIsSpeaking] = useState(false); + const [speakingIdx, setSpeakingIdx] = useState(null); + const speechSynth: SpeechSynthesis = window.speechSynthesis; const removeJustificationFromText = (text: string) => { // remove everything after the first "" word @@ -68,27 +69,23 @@ function ChatWindow({ return text; }; - const handleTTS = (text: string) => { - if (isSpeaking) return stopTTS(); + const handleTTS = (text: string, idx: number) => { + if (speakingIdx != null) return stopTTS(); if (!text) return; const utterThis: SpeechSynthesisUtterance = new SpeechSynthesisUtterance( text, ); - const speechSynth: SpeechSynthesis = window.speechSynthesis; utterThis.lang = "en-US"; speechSynth.speak(utterThis); - setIsSpeaking(true); + setSpeakingIdx(idx); utterThis.onend = () => { - setIsSpeaking(false); + setSpeakingIdx(null); }; }; const stopTTS = () => { - if (isSpeaking) { - const speechSynth: SpeechSynthesis = window.speechSynthesis; - speechSynth.cancel(); - setIsSpeaking(false); - } + speechSynth.cancel(); + setSpeakingIdx(null); }; const router = useRouter(); @@ -345,19 +342,23 @@ function ChatWindow({ {/* speak response */} -- cgit v1.2.3