Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Chat): use useMemo for SpeechRecognition support detection (#2270)

Andrey Goder105707aa5af1
diff --git a/packages/core/src/Chat/useSpeechRecognition.ts b/packages/core/src/Chat/useSpeechRecognition.ts
index dd0852bebb..e9a7ef540a 100644
--- a/packages/core/src/Chat/useSpeechRecognition.ts
+++ b/packages/core/src/Chat/useSpeechRecognition.ts
@@ -13,7 +13,7 @@
* volume level from the microphone via AudioContext analyser.
*/
-import {useState, useCallback, useEffect, useRef} from 'react';
+import {useState, useCallback, useEffect, useMemo, useRef} from 'react';
// =============================================================================
// Types
@@ -276,7 +276,7 @@ export function useSpeechRecognition(
[externalAudioContext],
);
- const [isSupported, setIsSupported] = useState(false);
+ const isSupported = useMemo(() => getSpeechRecognition() != null, []);
const [isListening, setIsListening] = useState(false);
const [isSpeaking, setIsSpeaking] = useState(false);
const [volume, setVolume] = useState(0);
@@ -308,8 +308,6 @@ export function useSpeechRecognition(
};
useEffect(() => {
- const SR = getSpeechRecognition();
- setIsSupported(SR != null);
return () => {
recognitionRef.current?.abort();
recognitionRef.current = null;