import { useState, useRef, useEffect, useCallback, useMemo } from 'react'; interface ChatInputProps { onSendMessage: (text: string) => void; onStopTask: () => void; disabled: boolean; showStopButton: boolean; setContent?: (setter: (text: string) => void) => void; isDarkMode?: boolean; } export default function ChatInput({ onSendMessage, onStopTask, disabled, showStopButton, setContent, isDarkMode = false, }: ChatInputProps) { const [text, setText] = useState(''); const isSendButtonDisabled = useMemo(() => disabled || text.trim() === '', [disabled, text]); const textareaRef = useRef(null); // Handle text changes and resize textarea const handleTextChange = (e: React.ChangeEvent) => { const newText = e.target.value; setText(newText); // Resize textarea const textarea = textareaRef.current; if (textarea) { textarea.style.height = 'auto'; textarea.style.height = `${Math.min(textarea.scrollHeight, 100)}px`; } }; // Expose a method to set content from outside useEffect(() => { if (setContent) { setContent(setText); } }, [setContent]); // Initial resize when component mounts useEffect(() => { const textarea = textareaRef.current; if (textarea) { textarea.style.height = 'auto'; textarea.style.height = `${Math.min(textarea.scrollHeight, 100)}px`; } }, []); const handleSubmit = useCallback( (e: React.FormEvent) => { e.preventDefault(); if (text.trim()) { onSendMessage(text); setText(''); } }, [text, onSendMessage], ); const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) { e.preventDefault(); handleSubmit(e); } }, [handleSubmit], ); return (