import { useEffect, useState } from 'react'; import { X } from 'lucide-react'; import { Button } from '../../ui/button'; import { authenticatedFetch } from '../../../utils/api'; import type { FileTreeImageSelection } from '../types/types'; type ImageViewerProps = { file: FileTreeImageSelection; onClose: () => void; }; export default function ImageViewer({ file, onClose }: ImageViewerProps) { 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: string | null = null; 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 (loadError: unknown) { if (loadError instanceof Error && loadError.name === 'AbortError') { return; } console.error('Error loading image:', loadError); 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}

); }