'use client'; import Image from 'next/image'; import { useState, useRef, useEffect, useCallback } from 'react'; interface MessageInputProps { onSend?: (message: string) => void; onTypingStart?: () => void; onTypingStop?: () => void; } export function MessageInput({ onSend, onTypingStart, onTypingStop }: MessageInputProps) { const [message, setMessage] = useState(''); const typingTimeoutRef = useRef(null); const isTypingRef = useRef(false); // Handle typing indicator const handleTyping = useCallback(() => { // Start typing indicator if (!isTypingRef.current && onTypingStart) { isTypingRef.current = true; onTypingStart(); } // Clear existing timeout if (typingTimeoutRef.current) { clearTimeout(typingTimeoutRef.current); } // Set timeout to stop typing indicator after 2 seconds of inactivity typingTimeoutRef.current = setTimeout(() => { if (isTypingRef.current && onTypingStop) { isTypingRef.current = false; onTypingStop(); } }, 2000); }, [onTypingStart, onTypingStop]); // Cleanup on unmount useEffect(() => { return () => { if (typingTimeoutRef.current) { clearTimeout(typingTimeoutRef.current); } if (isTypingRef.current && onTypingStop) { onTypingStop(); } }; }, [onTypingStop]); const handleSend = () => { if (message.trim() && onSend) { // Stop typing indicator before sending if (typingTimeoutRef.current) { clearTimeout(typingTimeoutRef.current); } if (isTypingRef.current && onTypingStop) { isTypingRef.current = false; onTypingStop(); } onSend(message.trim()); setMessage(''); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }; const handleChange = (e: React.ChangeEvent) => { setMessage(e.target.value); handleTyping(); }; return (
{/* Text input area */}