import React, { useState, useEffect } from 'react'; import { Box, Typography, CircularProgress, Alert, Paper, } from '@mui/material'; import { documentService } from '../services/api'; interface DocumentViewerProps { documentId: string; filename: string; mimeType: string; } const DocumentViewer: React.FC = ({ documentId, filename, mimeType, }) => { const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [documentUrl, setDocumentUrl] = useState(null); useEffect(() => { loadDocument(); // Cleanup URL when component unmounts return () => { if (documentUrl) { window.URL.revokeObjectURL(documentUrl); } }; }, [documentId]); const loadDocument = async (): Promise => { try { setLoading(true); setError(null); const response = await documentService.view(documentId); const url = window.URL.createObjectURL(new Blob([response.data], { type: mimeType })); setDocumentUrl(url); } catch (err) { console.error('Failed to load document:', err); setError('Failed to load document for viewing'); } finally { setLoading(false); } }; const renderDocumentContent = (): React.ReactElement => { if (!documentUrl) return <>; // Handle images if (mimeType.startsWith('image/')) { return ( {filename} ); } // Handle PDFs if (mimeType === 'application/pdf') { return (