import React, { useState, useEffect } from 'react'; import { Attachment } from '../../entities/Attachment'; import { getAttachmentType } from '../../utils/attachmentsService'; interface AttachmentPreviewProps { attachment: Attachment; maxHeight?: string; className?: string; } const AttachmentPreview: React.FC = ({ attachment, maxHeight = '400px', className = '', }) => { const [textContent, setTextContent] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const type = getAttachmentType(attachment.mime_type); const fileUrl = attachment.file_url; useEffect(() => { if (type === 'text' && fileUrl) { setLoading(true); fetch(fileUrl, { credentials: 'include' }) .then((res) => res.text()) .then((text) => { setTextContent(text); setLoading(false); }) .catch((err) => { console.error('Failed to load text file:', err); setError('Failed to load file content'); setLoading(false); }); } }, [fileUrl, type]); if (!fileUrl) { console.warn('No file URL available for attachment:', attachment); return (
Preview not available
); } if (type === 'image') { return (
{attachment.original_filename}
); } if (type === 'pdf') { return (