diff --git a/src/components/ImageViewer.jsx b/src/components/ImageViewer.jsx index b5077ae..15401a0 100644 --- a/src/components/ImageViewer.jsx +++ b/src/components/ImageViewer.jsx @@ -1,9 +1,63 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { Button } from './ui/button'; import { X } from 'lucide-react'; 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 token = localStorage.getItem('auth-token'); + if (!token) { + setError('Missing authentication token'); + return; + } + + const response = await fetch(imagePath, { + headers: { + 'Authorization': `Bearer ${token}` + }, + 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 (
Unable to load image
-{file.path}
-Loading imageā¦
+{error || 'Unable to load image'}
+{file.path}
+