import type { NFTError } from '@/api/types'; import { useNFTContext } from '@/nft/components/NFTProvider'; import { type MouseEvent, useCallback, useEffect, useState } from 'react'; import { defaultNFTSvg } from '../../../internal/svg/defaultNFTSvg'; import { cn } from '../../../styles/theme'; type NFTImageProps = { className?: string; square?: boolean; onLoading?: (mediaUrl: string) => void; onLoaded?: () => void; onError?: (error: NFTError) => void; }; export function NFTImage({ className, square = true, onLoading, onLoaded, onError, }: NFTImageProps) { const { imageUrl, description } = useNFTContext(); const [loaded, setLoaded] = useState(false); const [error, setError] = useState(false); const loadImage = useCallback(() => { if (imageUrl) { onLoading?.(imageUrl); const img = new Image(); img.onload = () => { setLoaded(true); onLoaded?.(); }; img.onerror = (error: string | Event) => { onError?.({ error: typeof error === 'string' ? error : error.type, code: 'NmNIc01', // NFT module NFTImage component 01 error message: 'Error loading image', }); setError(true); }; img.src = imageUrl; } }, [imageUrl, onLoading, onLoaded, onError]); useEffect(() => { loadImage(); }, [loadImage]); const handleRetry = useCallback( async (e: MouseEvent) => { e.stopPropagation(); setError(false); loadImage(); }, [loadImage], ); return (
*]:col-start-1 [&>*]:col-end-1 [&>*]:row-start-1 [&>*]:row-end-1', { 'aspect-square': square }, className, )} >
{defaultNFTSvg}
{description}
{error && (
)}
); }