import { useState, useEffect } from "react"; import { Skeleton } from "@/components/ui/skeleton"; import { invoke } from "@tauri-apps/api/core"; import { Image } from "lucide-react"; interface ProxyImageProps { src: string; alt: string; className?: string; } export function ProxyImage({ src, alt, className }: ProxyImageProps) { const [proxiedSrc, setProxiedSrc] = useState(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [needsProxy, setNeedsProxy] = useState(false); const [proxiedImageFailed, setProxiedImageFailed] = useState(false); // Try direct loading first, fall back to proxy if needed useEffect(() => { setIsLoading(true); setError(null); setNeedsProxy(false); setProxiedSrc(null); setProxiedImageFailed(false); if (!src) { setError("No image source provided"); setIsLoading(false); return; } return () => { // Cleanup }; }, [src]); // Load via proxy if direct loading failed useEffect(() => { let isMounted = true; async function loadImageViaProxy() { if (!needsProxy || !src) return; try { setIsLoading(true); const localPath = await invoke("fetch_image", { url: src }); if (isMounted) { setProxiedSrc(localPath); setIsLoading(false); } } catch (err) { // console.error("Error fetching image:", err); if (isMounted) { setError(err instanceof Error ? err.message : "Failed to load image"); setIsLoading(false); } } } if (needsProxy) { loadImageViaProxy(); } return () => { isMounted = false; }; }, [src, needsProxy]); // Direct loading render if (!needsProxy && !error) { return ( <> {alt} setIsLoading(false)} onError={() => { // console.log("Direct image loading failed, falling back to proxy"); setNeedsProxy(true); }} /> {isLoading && } ); } // Proxy loading or error states if (isLoading) { return ; } if (error || !proxiedSrc || proxiedImageFailed) { return (
); } // Successful proxy image return ( {alt} { // console.log("Proxied image failed to render properly"); setProxiedImageFailed(true); }} /> ); }