import { useState, useEffect } from "react"; import { FileManager } from "../pocketbase/FileManager"; interface FilePreviewProps { url: string; filename: string; onClose?: () => void; } export default function FilePreview({ url, filename, onClose }: FilePreviewProps) { const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [fileType, setFileType] = useState(null); const [modalElement, setModalElement] = useState(null); useEffect(() => { const modal = document.getElementById('filePreviewModal') as HTMLDialogElement; setModalElement(modal); const initializeViewer = async () => { try { setLoading(true); setError(null); setFileContent(null); // Determine file type from extension const extension = filename.split('.').pop()?.toLowerCase() || ''; const type = getFileType(extension); setFileType(type); // If it's a code file, fetch its content if (type === 'code') { await fetchCodeContent(url); } setLoading(false); } catch (err) { setError("Failed to load file preview"); setLoading(false); } }; // Only initialize if we have both url and filename if (url && filename) { initializeViewer(); if (modal && !modal.open) { modal.showModal(); } } // Cleanup function return () => { if (modal?.open) { modal.close(); } setFileContent(null); }; }, [url, filename]); const handleClose = () => { if (modalElement?.open) { modalElement.close(); } onClose?.(); }; const [fileContent, setFileContent] = useState(null); const getFileType = (extension: string): string => { const imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg']; const videoTypes = ['mp4', 'webm', 'ogg', 'mov']; const documentTypes = ['pdf', 'doc', 'docx', 'txt', 'md']; const spreadsheetTypes = ['xls', 'xlsx', 'csv']; const presentationTypes = ['ppt', 'pptx']; const codeTypes = ['js', 'ts', 'jsx', 'tsx', 'html', 'css', 'json', 'py', 'java', 'cpp', 'h', 'c', 'cs', 'php', 'rb', 'swift', 'go', 'rs']; if (imageTypes.includes(extension)) return 'image'; if (videoTypes.includes(extension)) return 'video'; if (documentTypes.includes(extension)) return 'document'; if (spreadsheetTypes.includes(extension)) return 'spreadsheet'; if (presentationTypes.includes(extension)) return 'presentation'; if (codeTypes.includes(extension)) return 'code'; return 'other'; }; // Function to fetch and set code content const fetchCodeContent = async (url: string) => { try { const response = await fetch(url); const text = await response.text(); setFileContent(text); } catch (err) { console.error('Failed to fetch code content:', err); setError('Failed to load code content'); } }; const renderFileIcon = () => { switch (fileType) { case 'image': return ( ); case 'video': return ( ); case 'document': return ( ); case 'code': return ( ); case 'spreadsheet': return ( ); default: return ( ); } }; const renderPreview = () => { if (loading) { return (
); } if (error) { return (

{error}

); } switch (fileType) { case 'image': return (
{filename} setError("Failed to load image")} /> Download
); case 'video': return (
Download Video
); case 'code': if (fileContent !== null) { return (
                                {fileContent}
                            
); } return (
{renderFileIcon()}

{filename}

); case 'document': if (filename.toLowerCase().endsWith('.pdf')) { return (