import { useEffect } from 'react'; import { TooltipProvider, Tooltip, TooltipTrigger, TooltipContent } from '~/components/ui'; import { ListeningIcon, Spinner } from '~/components/svg'; import { useLocalize, useSpeechToText } from '~/hooks'; import { useChatFormContext } from '~/Providers'; import { globalAudioId } from '~/common'; export default function AudioRecorder({ textAreaRef, methods, ask, disabled, }: { textAreaRef: React.RefObject; methods: ReturnType; ask: (data: { text: string }) => void; disabled: boolean; }) { const localize = useLocalize(); const handleTranscriptionComplete = (text: string) => { if (text) { const globalAudio = document.getElementById(globalAudioId) as HTMLAudioElement; if (globalAudio) { console.log('Unmuting global audio'); globalAudio.muted = false; } ask({ text }); methods.reset({ text: '' }); clearText(); } }; const { isListening, isLoading, startRecording, stopRecording, interimTranscript, speechText, clearText, } = useSpeechToText(handleTranscriptionComplete); useEffect(() => { if (isListening && textAreaRef.current) { methods.setValue('text', interimTranscript, { shouldValidate: true, }); } else if (textAreaRef.current) { textAreaRef.current.value = speechText; methods.setValue('text', speechText, { shouldValidate: true }); } }, [interimTranscript, speechText, methods, textAreaRef]); const handleStartRecording = async () => { await startRecording(); }; const handleStopRecording = async () => { await stopRecording(); }; const renderIcon = () => { if (isListening) { return ; } if (isLoading) { return ; } return ; }; return ( {localize('com_ui_use_micrphone')} ); }