import React, { useState, useEffect, useRef } from 'react'; interface ChatInputProps { onSendMessage: (text: string) => void; isLoading: boolean; } // Check for SpeechRecognition API // FIX: Cast window to `any` to access non-standard SpeechRecognition APIs without TypeScript errors. const SpeechRecognition = (window as any).SpeechRecognition || (window as any).webkitSpeechRecognition; const recognition = SpeechRecognition ? new SpeechRecognition() : null; if (recognition) { recognition.continuous = false; recognition.lang = 'en-US'; recognition.interimResults = false; } const ChatInput: React.FC = ({ onSendMessage, isLoading }) => { const [text, setText] = useState(''); const [isListening, setIsListening] = useState(false); const recognitionRef = useRef(recognition); useEffect(() => { const currentRecognition = recognitionRef.current; if (!currentRecognition) return; // FIX: Use `any` for the event type as SpeechRecognitionEvent is not defined in this project's TS config. const handleResult = (event: any) => { const transcript = event.results[0][0].transcript; setText(transcript); handleSend(); // Auto-send after transcription }; const handleEnd = () => { setIsListening(false); }; // FIX: Use `any` for the event type as SpeechRecognitionError (or the correct SpeechRecognitionErrorEvent) is not defined. const handleError = (event: any) => { console.error('Speech recognition error', event.error); setIsListening(false); } currentRecognition.addEventListener('result', handleResult); currentRecognition.addEventListener('end', handleEnd); currentRecognition.addEventListener('error', handleError); return () => { currentRecognition.removeEventListener('result', handleResult); currentRecognition.removeEventListener('end', handleEnd); currentRecognition.removeEventListener('error', handleError); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const handleSend = () => { if (text.trim() && !isLoading) { onSendMessage(text); setText(''); } }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleSend(); } }; const toggleListening = () => { if (!recognitionRef.current) return; if (isListening) { recognitionRef.current.stop(); } else { recognitionRef.current.start(); } setIsListening(!isListening); }; return ( ); }; export default ChatInput;