SimReady-assets-explorer / src /AssetImage.tsx
ZIP Space Deployer
Deploy ZIP — 2026-10-26 20:15 UTC
0dfa89f
Raw History Blame Contribute Delete
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);}}/>;
}