import React, { useState, useRef, useCallback } from 'react';
import { Send, Paperclip, Smile, X, Image, Music, FileText } from 'lucide-react';
import { useWebSocket } from '../../hooks/useWebSocket';
import { useFileUpload } from '../../hooks/useFileUpload';
import { validateFile, formatFileSize } from '../../utils/fileValidation';
const MessageInput = ({ conversation, onSendMessage, onTypingStart, onTypingStop }) => {
const [message, setMessage] = useState('');
const [isTyping, setIsTyping] = useState(false);
const [fileUpload, setFileUpload] = useState(null);
const fileInputRef = useRef();
const textareaRef = useRef();
const { sendMessage } = useWebSocket();
const { uploadFile, uploading, progress, error: uploadError, reset: resetUpload } = useFileUpload();
const typingTimeoutRef = useRef();
const handleSubmit = async (e) => {
e.preventDefault();
if (!message.trim() && !fileUpload) return;
// Reset typing
stopTyping();
if (fileUpload) {
// Send file message
await sendFileMessage();
} else {
// Send text message
sendTextMessage();
}
setMessage('');
setFileUpload(null);
resetUpload();
};
const sendTextMessage = () => {
if (!message.trim()) return;
const messageData = {
conversationId: conversation._id,
content: message.trim(),
type: 'text'
};
sendMessage('message:send', messageData);
onSendMessage({
...messageData,
_id: `temp-${Date.now()}`,
sender: { _id: 'current-user' },
createdAt: new Date(),
status: 'sending'
});
};
const sendFileMessage = async () => {
if (!fileUpload) return;
try {
const response = await uploadFile(fileUpload.file);
const messageData = {
conversationId: conversation._id,
content: message.trim() || `Sent ${getFileTypeDescription(fileUpload.file.type)}`,
type: getMessageType(fileUpload.file.type),
attachment: {
url: response.file.url,
filename: response.file.fileName,
originalName: fileUpload.file.name,
mimeType: fileUpload.file.type,
size: fileUpload.file.size,
thumbnail: response.file.thumbnail
}
};
sendMessage('message:send', messageData);
onSendMessage({
...messageData,
_id: `temp-${Date.now()}`,
sender: { _id: 'current-user' },
createdAt: new Date(),
status: 'sending'
});
} catch (error) {
console.error('Failed to send file message:', error);
}
};
const handleInputChange = (e) => {
setMessage(e.target.value);
if (!isTyping) {
startTyping();
}
// Reset typing timeout
clearTimeout(typingTimeoutRef.current);
typingTimeoutRef.current = setTimeout(stopTyping, 1000);
};
const startTyping = () => {
if (!isTyping) {
setIsTyping(true);
sendMessage('typing:start', { conversationId: conversation._id });
}
};
const stopTyping = () => {
if (isTyping) {
setIsTyping(false);
sendMessage('typing:stop', { conversationId: conversation._id });
}
};
const handleFileSelect = async (e) => {
const file = e.target.files[0];
if (!file) return;
try {
validateFile(file);
setFileUpload({
file,
name: file.name,
size: file.size,
type: file.type
});
// Reset file input
fileInputRef.current.value = '';
} catch (error) {
alert(error.message);
}
};
const removeFile = () => {
setFileUpload(null);
resetUpload();
};
const getFileTypeDescription = (mimeType) => {
if (mimeType.startsWith('image/')) return 'an image';
if (mimeType.startsWith('audio/')) return 'an audio file';
if (mimeType === 'application/pdf') return 'a PDF';
if (mimeType.startsWith('text/')) return 'a text file';
return 'a file';
};
const getMessageType = (mimeType) => {
if (mimeType.startsWith('image/')) return 'image';
if (mimeType.startsWith('audio/')) return 'audio';
return 'file';
};
const getFileIcon = (mimeType) => {
if (mimeType.startsWith('image/')) return
{uploadError}
)}