diff --git a/site/src/pages/AgentsPage/components/AgentChatInput.tsx b/site/src/pages/AgentsPage/components/AgentChatInput.tsx index 83bf8f4614..ca4fdfd20b 100644 --- a/site/src/pages/AgentsPage/components/AgentChatInput.tsx +++ b/site/src/pages/AgentsPage/components/AgentChatInput.tsx @@ -57,11 +57,11 @@ import { TooltipContent, TooltipTrigger, } from "#/components/Tooltip/Tooltip"; -import { useSpeechRecognition } from "#/hooks/useSpeechRecognition"; import { cn } from "#/utils/cn"; import { countInvisibleCharacters } from "#/utils/invisibleUnicode"; import { isMobileViewport } from "#/utils/mobile"; import { useOverflowCount } from "../hooks/useOverflowCount"; +import { useSpeechRecognition } from "../hooks/useSpeechRecognition"; import { fetchTextAttachmentContent, formatTextAttachmentPreview, diff --git a/site/src/hooks/useSpeechRecognition.test.ts b/site/src/pages/AgentsPage/hooks/useSpeechRecognition.test.ts similarity index 100% rename from site/src/hooks/useSpeechRecognition.test.ts rename to site/src/pages/AgentsPage/hooks/useSpeechRecognition.test.ts diff --git a/site/src/hooks/useSpeechRecognition.ts b/site/src/pages/AgentsPage/hooks/useSpeechRecognition.ts similarity index 93% rename from site/src/hooks/useSpeechRecognition.ts rename to site/src/pages/AgentsPage/hooks/useSpeechRecognition.ts index d8b89906d0..2ad0f06e1e 100644 --- a/site/src/hooks/useSpeechRecognition.ts +++ b/site/src/pages/AgentsPage/hooks/useSpeechRecognition.ts @@ -85,17 +85,13 @@ export function useSpeechRecognition(): { const [error, setError] = useState(null); const recognitionRef = useRef(null); - // Cache the constructor lookup once per hook instance so we don't hit - // the window property on every render. - const ctorRef = useRef( - getSpeechRecognitionCtor(), - ); - - const isSupported = ctorRef.current !== undefined; + // Browser API availability is constant for the lifetime of the + // page, so a lazy state initializer captures it once. + const [ctorSnapshot] = useState(getSpeechRecognitionCtor); + const isSupported = ctorSnapshot !== undefined; const start = useCallback(() => { - const Ctor = ctorRef.current; - if (!Ctor) { + if (!ctorSnapshot) { return; } @@ -107,7 +103,7 @@ export function useSpeechRecognition(): { setError(null); - const recognition = new Ctor(); + const recognition = new ctorSnapshot(); recognition.lang = navigator.language; recognition.continuous = true; recognition.interimResults = true; @@ -147,7 +143,7 @@ export function useSpeechRecognition(): { setTranscript(""); setIsRecording(true); recognition.start(); - }, []); + }, [ctorSnapshot]); const stop = useCallback(() => { // stop() lets the browser deliver any remaining final results