import React, { useEffect, useState } from 'react'; import { Button } from './ui/button'; import { X } from 'lucide-react'; import { authenticatedFetch } from '../utils/api'; function ImageViewer({ file, onClose }) { const imagePath = `/api/projects/${file.projectName}/files/content?path=${encodeURIComponent(file.path)}`; const [imageUrl, setImageUrl] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { let objectUrl; const controller = new AbortController(); const loadImage = async () => { try { setLoading(true); setError(null); setImageUrl(null); const response = await authenticatedFetch(imagePath, { signal: controller.signal }); if (!response.ok) { throw new Error(`Request failed with status ${response.status}`); } const blob = await response.blob(); objectUrl = URL.createObjectURL(blob); setImageUrl(objectUrl); } catch (err) { if (err.name === 'AbortError') { return; } console.error('Error loading image:', err); setError('Unable to load image'); } finally { setLoading(false); } }; loadImage(); return () => { controller.abort(); if (objectUrl) { URL.revokeObjectURL(objectUrl); } }; }, [imagePath]); return (

{file.name}

{loading && (

Loading image…

)} {!loading && imageUrl && ( {file.name} )} {!loading && !imageUrl && (

{error || 'Unable to load image'}

{file.path}

)}

{file.path}

); } export default ImageViewer;