import React, { useState, useRef, useEffect } from 'react'; import { Input } from '../ui'; import { Button } from '../ui'; import Typography from '../ui/Typography'; import { FiSend, FiAlertCircle } from 'react-icons/fi'; import { cn } from '../../lib/utils'; interface MessageInputProps { onSendMessage: (message: string) => Promise; disabled?: boolean; placeholder?: string; } /** * Input field with send button for chat messages */ const MessageInput: React.FC = ({ onSendMessage, disabled = false, placeholder = "Type a message..." }) => { const [inputValue, setInputValue] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(null); const inputRef = useRef(null); const handleSubmit = async () => { const message = inputValue.trim(); if (!message || isSubmitting || disabled) return; setIsSubmitting(true); setError(null); try { await onSendMessage(message); setInputValue(''); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Failed to send message'; setError(errorMessage); } finally { setIsSubmitting(false); } }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSubmit(); } }; const handleInputChange = (e: React.ChangeEvent) => { setInputValue(e.target.value); if (error) setError(null); }; useEffect(() => { if (!disabled && inputRef.current) { inputRef.current.focus(); } }, [disabled]); return (
{error && (
)}
{ e.preventDefault(); handleSubmit(); }} className="flex flex-col sm:flex-row gap-3" >
{!disabled && (
Press Enter to send, Shift+Enter for new line 1800 && "text-orange-600 dark:text-orange-400", inputValue.length > 1950 && "text-red-600 dark:text-red-400" )} aria-label={`Character count: ${inputValue.length} of 2000`} > {inputValue.length}/2000
)}
); }; export default MessageInput;