import { useState, useEffect } from 'react'; interface ImagePreviewProps { src: string; alt?: string; className?: string; } export default function ImagePreview({ src, alt = 'Pasted image', className = '', }: ImagePreviewProps) { const [isExpanded, setIsExpanded] = useState(false); const [error, setError] = useState(false); const [isLoading, setIsLoading] = useState(true); const [imageData, setImageData] = useState(null); useEffect(() => { const loadImage = async () => { try { // Use the IPC handler to get the image data const data = await window.electron.getTempImage(src); if (data) { setImageData(data); setIsLoading(false); } else { setError(true); setIsLoading(false); } } catch (err) { console.error('Error loading image:', err); setError(true); setIsLoading(false); } }; loadImage(); }, [src]); const handleError = () => { setError(true); setIsLoading(false); }; const toggleExpand = () => { if (!error) { setIsExpanded(!isExpanded); } }; // Validate that this is a safe file path (should contain goose-pasted-images) if (!src.includes('goose-pasted-images')) { return
Invalid image path: {src}
; } if (error) { return
Unable to load image: {src}
; } return (
{isLoading && (
Loading...
)} {imageData && ( {alt} )} {isExpanded && !error && !isLoading && imageData && (
Click to collapse
)} {!isExpanded && !error && !isLoading && imageData && (
Click to expand
)}
); }