Spaces:
Running
Running
Download frontend/src/components/ImageWithFallback.tsx from ManalifeAI/Pathora: direct link, hf CLI and curl.
- Browser
- Download file 914 Bytes
-
https://huggingface.co/spaces/ManalifeAI/Pathora/resolve/refs%2Fpr%2F5/frontend/src/components/ImageWithFallback.tsx
- Command line
-
hf download hf://spaces/ManalifeAI/Pathora@refs/pr/5/frontend/src/components/ImageWithFallback.tsx
-
curl -L -o ImageWithFallback.tsx https://huggingface.co/spaces/ManalifeAI/Pathora/resolve/refs%2Fpr%2F5/frontend/src/components/ImageWithFallback.tsx
914 Bytes
| import { useEffect, useRef, useState } from "react"; | |
| interface ImageWithFallbackProps { | |
| src: string; | |
| alt: string; | |
| className?: string; | |
| } | |
| export function ImageWithFallback({ src, alt, className }: ImageWithFallbackProps) { | |
| const imgRef = useRef<HTMLImageElement>(null); | |
| const [imgSrc, setImgSrc] = useState(src); | |
| const [error, setError] = useState(false); | |
| useEffect(() => { | |
| setImgSrc(src); | |
| setError(false); | |
| }, [src]); | |
| const handleError = () => { | |
| if (!error && imgSrc.startsWith('/')) { | |
| setError(true); | |
| // Try with explicit origin if relative URL | |
| const baseURL = import.meta.env.MODE === "development" | |
| ? "http://127.0.0.1:8000" | |
| : window.location.origin; | |
| setImgSrc(`${baseURL}${imgSrc}`); | |
| } | |
| }; | |
| return ( | |
| <img | |
| ref={imgRef} | |
| src={imgSrc} | |
| alt={alt} | |
| className={className} | |
| onError={handleError} | |
| /> | |
| ); | |
| } |