Download src/AssetImage.tsx from fffiloni/SimReady-assets-explorer: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/spaces/fffiloni/SimReady-assets-explorer/resolve/main/src/AssetImage.tsx
- Command line
-
hf download hf://spaces/fffiloni/SimReady-assets-explorer/src/AssetImage.tsx
-
curl -L -o AssetImage.tsx https://huggingface.co/spaces/fffiloni/SimReady-assets-explorer/resolve/main/src/AssetImage.tsx
1.47 kB
| import {useState} from 'react'; | |
| import {Box} from 'lucide-react'; | |
| import type {Asset} from './types'; | |
| import {normalizePreview,previewSources} from './preview'; | |
| const cache=new Map<string,string>(); | |
| type Props={asset:Asset;className?:string}; | |
| export default function AssetImage({asset,className=''}:Props){ | |
| const sources=previewSources(asset),cacheKey=sources.join('\n'); | |
| return <PreviewImage key={cacheKey} asset={asset} className={className} sources={sources} cacheKey={cacheKey}/>; | |
| } | |
| function PreviewImage({asset,className,sources,cacheKey}:Props&{sources:string[];cacheKey:string}){ | |
| const cached=cache.get(cacheKey); | |
| const [attempt,setAttempt]=useState(0),[normalized,setNormalized]=useState(cached),[ready,setReady]=useState(Boolean(cached)); | |
| if(attempt>=sources.length)return <span className="image-unavailable"><Box size={38}/><span>{asset.name}</span><small>Preview unavailable · Open 3D</small></span>; | |
| return <img crossOrigin="anonymous" className={`${className} asset-image`} style={{opacity:ready?1:0}} | |
| src={normalized||sources[attempt]} alt={asset.name} draggable={false} decoding="async" | |
| onLoad={event=>{ | |
| if(normalized)return; | |
| const result=normalizePreview(event.currentTarget); | |
| if(result){cache.set(cacheKey,result);if(cache.size>160)cache.delete(cache.keys().next().value!);setNormalized(result);} | |
| setReady(true); | |
| }} | |
| onError={()=>{setNormalized(undefined);setReady(false);setAttempt(n=>n+1);}}/>; | |
| } | |