import { useCallback } from 'react'; import { useChatFormContext, useToastContext } from '~/Providers'; import { ListeningIcon, Spinner } from '~/components/svg'; import { useLocalize, useSpeechToText } from '~/hooks'; import { TooltipAnchor } from '~/components/ui'; import { globalAudioId } from '~/common'; import { cn } from '~/utils'; export default function AudioRecorder({ disabled, ask, methods, textAreaRef, isSubmitting, }: { disabled: boolean; ask: (data: { text: string }) => void; methods: ReturnType; textAreaRef: React.RefObject; isSubmitting: boolean; }) { const { setValue, reset } = methods; const localize = useLocalize(); const { showToast } = useToastContext(); const onTranscriptionComplete = useCallback( (text: string) => { if (isSubmitting) { showToast({ message: localize('com_ui_speech_while_submitting'), status: 'error', }); return; } if (text) { const globalAudio = document.getElementById(globalAudioId) as HTMLAudioElement | null; if (globalAudio) { console.log('Unmuting global audio'); globalAudio.muted = false; } ask({ text }); reset({ text: '' }); } }, [ask, reset, showToast, localize, isSubmitting], ); const setText = useCallback( (text: string) => { setValue('text', text, { shouldValidate: true, }); }, [setValue], ); const { isListening, isLoading, startRecording, stopRecording } = useSpeechToText( setText, onTranscriptionComplete, ); if (!textAreaRef.current) { return null; } const handleStartRecording = async () => startRecording(); const handleStopRecording = async () => stopRecording(); const renderIcon = () => { if (isListening === true) { return ; } if (isLoading === true) { return ; } return ; }; return ( {renderIcon()} } /> ); }