import React, { useEffect, useState } from 'react'; interface FilePreviewProps { url?: string; filename?: string; isModal?: boolean; } const FilePreview: React.FC = ({ url: initialUrl = '', filename: initialFilename = '', isModal = false }) => { const [url, setUrl] = useState(initialUrl); const [filename, setFilename] = useState(initialFilename); const [content, setContent] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const [fileType, setFileType] = useState(null); useEffect(() => { console.log('FilePreview component mounted'); if (isModal) { const handleStateChange = (event: CustomEvent<{ url: string; filename: string }>) => { console.log('Received state change event:', event.detail); const { url: newUrl, filename: newFilename } = event.detail; setUrl(newUrl); setFilename(newFilename); // Reset state when url is empty (modal closing) if (!newUrl) { setContent(null); setError(null); setFileType(null); setLoading(false); } }; // Add event listener only for modal mode window.addEventListener('filePreviewStateChange', handleStateChange as EventListener); // Cleanup return () => { window.removeEventListener('filePreviewStateChange', handleStateChange as EventListener); }; } else { // For integrated preview, use props directly setUrl(initialUrl); setFilename(initialFilename); } }, [isModal, initialUrl, initialFilename]); useEffect(() => { console.log('FilePreview state updated:', { url, filename }); if (!url || !filename) { console.log('No URL or filename, resetting state'); setContent(null); setError(null); setFileType(null); return; } const loadContent = async () => { console.log('Loading content for:', { url, filename }); setLoading(true); setError(null); try { console.log('Fetching file...'); const response = await fetch(url); const contentType = response.headers.get('content-type'); console.log('Received content type:', contentType); setFileType(contentType); if (contentType?.startsWith('image/')) { console.log('Setting content type as image'); setContent('image'); } else if (contentType?.startsWith('video/')) { console.log('Setting content type as video'); setContent('video'); } else if (contentType?.startsWith('application/pdf')) { console.log('Setting content type as pdf'); setContent('pdf'); } else if (contentType?.startsWith('text/')) { console.log('Loading text content'); const text = await response.text(); if (text.length > 100000) { console.log('Text content truncated due to length'); setContent(text.substring(0, 100000) + '\n\n... Content truncated. Please download the file to view the complete content.'); } else { setContent(text); } } else if (filename.toLowerCase().endsWith('.mp4')) { console.log('Fallback to video for .mp4 file'); setContent('video'); } else { console.log('Unsupported file type'); setError(`This file type (${contentType || 'unknown'}) is not supported for preview. Please download the file to view it.`); } } catch (err) { console.error('Error loading file:', err); setError('Failed to load file'); } finally { console.log('Finished loading content'); setLoading(false); } }; loadContent(); }, [url, filename]); const handleDownload = async () => { try { const response = await fetch(url); const blob = await response.blob(); const downloadUrl = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = downloadUrl; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(downloadUrl); } catch (err) { console.error('Error downloading file:', err); alert('Failed to download file. Please try again.'); } }; console.log('Rendering FilePreview with:', { content, error, loading, fileType }); return (
{/* Header with filename and download button */}
{filename} {fileType && ( {fileType.split('/')[1]} )}
{/* Preview content */}
{loading && (
)} {error && (

Preview Unavailable

{error}

)} {!loading && !error && content === 'image' && (
{filename}
)} {!loading && !error && content === 'video' && (
)} {!loading && !error && content === 'pdf' && (
)} {!loading && !error && content && !['image', 'video', 'pdf'].includes(content) && (
{content}
)}
); }; export default FilePreview;