import { useEffect, useRef, useState } from 'react'; import { RefreshCw, X } from 'lucide-react'; import { imageUrlWithRetry } from '../app-core-utils.js'; export function GeneratedImage({ part, onPreviewImage }) { const [loadState, setLoadState] = useState('loading'); const [retryKey, setRetryKey] = useState(0); const src = imageUrlWithRetry(part.url, retryKey); return ( ); } export function ImagePreviewModal({ image, onClose }) { const [loadState, setLoadState] = useState('loading'); const [retryKey, setRetryKey] = useState(0); const closeButtonRef = useRef(null); const previousFocusRef = useRef(null); useEffect(() => { setLoadState('loading'); setRetryKey(0); }, [image?.url]); useEffect(() => { if (!image) { return undefined; } previousFocusRef.current = document.activeElement; const previousOverflow = document.body.style.overflow; document.body.style.overflow = 'hidden'; const frame = window.requestAnimationFrame(() => closeButtonRef.current?.focus()); return () => { window.cancelAnimationFrame(frame); document.body.style.overflow = previousOverflow; previousFocusRef.current?.focus?.(); previousFocusRef.current = null; }; }, [image]); if (!image) { return null; } const src = imageUrlWithRetry(image.url, retryKey); return (
event.stopPropagation()}> {image.alt setLoadState('loaded')} onError={() => setLoadState('failed')} />
{loadState === 'failed' ? (
event.stopPropagation()}>
) : null}
); }