"use client" import { useCallback, useState } from "react" import { cn } from "@lib/utils" interface ImagePreviewProps { url: string title?: string | null } export function ImagePreview({ url, title }: ImagePreviewProps) { const [imageError, setImageError] = useState(false) const [isLoading, setIsLoading] = useState(true) const [retryKey, setRetryKey] = useState(0) // On first failure, wait briefly then force a re-render with a new key to // retry the fetch (covers transient R2 timing issues). // On second failure, give up and show the error state. const handleImageError = useCallback(() => { if (retryKey === 0) { setTimeout(() => setRetryKey(1), 500) return } setImageError(true) setIsLoading(false) }, [retryKey]) if (imageError || !url) { return (

Failed to load image

) } return (
{isLoading && (
)}
{title setIsLoading(false)} loading="lazy" />
) }