import React, { useState } from "react"; import { FaDownload } from "react-icons/fa"; import { MdOutlineRemoveRedEye } from "react-icons/md"; import { Message, Document } from "./dashboardChatAreaTypes"; import FilePreviewPopup from "./FilePreviewPopup"; import styles from './DashboardChatArea.module.css'; interface MessageItemProps { message: Message; index: number; } // Helper function to format file size const formatFileSize = (bytes?: number): string => { if (!bytes) return ''; const sizes = ['B', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(1024)); return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i]; }; // Helper function to get file icon based on type or extension const getFileIcon = (type?: string, ext?: string): string => { // Use extension first if available, then fall back to MIME type const extension = ext?.toLowerCase(); const mimeType = type?.toLowerCase(); // Check extension first if (extension) { // Images if (['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'].includes(extension)) return '🖼️'; // Videos if (['mp4', 'avi', 'mov', 'wmv', 'flv', 'webm', 'mkv'].includes(extension)) return '🎥'; // Audio if (['mp3', 'wav', 'aac', 'flac', 'ogg', 'wma'].includes(extension)) return '🎵'; // Documents if (extension === 'pdf') return '📕'; if (['doc', 'docx'].includes(extension)) return '📘'; if (['xls', 'xlsx'].includes(extension)) return '📊'; if (['ppt', 'pptx'].includes(extension)) return '📋'; if (['txt', 'md', 'rtf'].includes(extension)) return '📝'; // Archives if (['zip', 'rar', '7z', 'tar', 'gz'].includes(extension)) return '📦'; // Code files if (['js', 'ts', 'jsx', 'tsx', 'html', 'css', 'py', 'java', 'cpp', 'c', 'php'].includes(extension)) return '💻'; } // Fall back to MIME type if extension didn't match if (mimeType) { if (mimeType.includes('image')) return '🖼️'; if (mimeType.includes('video')) return '🎥'; if (mimeType.includes('audio')) return '🎵'; if (mimeType.includes('pdf')) return '📕'; if (mimeType.includes('text')) return '📝'; if (mimeType.includes('word') || mimeType.includes('document')) return '📘'; if (mimeType.includes('excel') || mimeType.includes('spreadsheet')) return '📊'; if (mimeType.includes('powerpoint') || mimeType.includes('presentation')) return '📋'; if (mimeType.includes('zip') || mimeType.includes('archive')) return '📦'; } return '📄'; }; const MessageItem: React.FC = ({ message, index }) => { const [previewDocument, setPreviewDocument] = useState(null); const [isPreviewOpen, setIsPreviewOpen] = useState(false); // Debug logging to see if documents are present console.log('MessageItem rendering:', { messageId: message.id, role: message.role, hasDocuments: !!message.documents, documentsLength: message.documents?.length || 0, documents: message.documents }); const handleDocumentClick = (document: Document) => { // If there's a downloadUrl, use it; otherwise try the url const downloadLink = document.downloadUrl || document.url; if (downloadLink) { // Open the document in a new tab window.open(downloadLink, '_blank'); } }; const handlePreview = (document: Document, e: React.MouseEvent) => { e.stopPropagation(); console.log('handlePreview called with document:', document); console.log('document.id:', document.id, 'document.fileId:', document.fileId); // Use fileId if available, otherwise try to use id as fallback const fileId = document.fileId || document.id; if (!fileId) { console.error('Neither fileId nor id is available on document:', document); return; } console.log('Using fileId for preview:', fileId, 'type:', typeof fileId); setPreviewDocument(document); setIsPreviewOpen(true); }; const handleClosePreview = () => { setIsPreviewOpen(false); setPreviewDocument(null); }; const handleDownload = (document: Document, e: React.MouseEvent) => { e.stopPropagation(); // TODO: Implement download functionality console.log('Download document:', document.name); }; return (
{message.role === 'user' ? 'You' : message.role === 'assistant' ? 'Assistant' : 'System'}
{message.content}
{message.documents && message.documents.length > 0 && (
{message.documents.map((document, docIndex) => (
handleDocumentClick(document)} title={`Click to open ${document.name}`} > {getFileIcon(document.type, document.ext)}
{document.ext ? `${document.name}.${document.ext}` : document.name}
{document.size && ( {formatFileSize(document.size)} )}
))}
)} {message.timestamp && (
{new Date(message.timestamp).toLocaleTimeString()}
)} {/* File Preview Popup */} {previewDocument && ( )}
); }; export default MessageItem;