1
1
import { ReactNode , useState } from 'react' ;
2
2
3
+ import { isValidUrl } from '@shared/utils/validate-url' ;
4
+
3
5
import { CollectibleItemLayout , CollectibleItemLayoutProps } from './collectible-item.layout' ;
4
6
import { ImageUnavailable } from './image-unavailable' ;
5
7
@@ -13,36 +15,34 @@ export function CollectibleImage(props: CollectibleImageProps) {
13
15
const [ isError , setIsError ] = useState ( false ) ;
14
16
const [ isLoading , setIsLoading ] = useState ( true ) ;
15
17
const [ width , setWidth ] = useState ( 0 ) ;
16
-
17
- if ( isError )
18
- return (
19
- < CollectibleItemLayout collectibleTypeIcon = { icon } { ...rest } >
20
- < ImageUnavailable />
21
- </ CollectibleItemLayout >
22
- ) ;
18
+ const isImageAvailable = src && isValidUrl ( src ) ;
23
19
24
20
return (
25
21
< CollectibleItemLayout collectibleTypeIcon = { icon } { ...rest } >
26
- < img
27
- alt = { alt }
28
- onError = { ( ) => setIsError ( true ) }
29
- loading = "lazy"
30
- onLoad = { event => {
31
- const target = event . target as HTMLImageElement ;
32
- setWidth ( target . naturalWidth ) ;
33
- setIsLoading ( false ) ;
34
- } }
35
- src = { src }
36
- style = { {
37
- width : '100%' ,
38
- height : '100%' ,
39
- aspectRatio : '1 / 1' ,
40
- objectFit : 'cover' ,
41
- // display: 'none' breaks onLoad event firing
42
- opacity : isLoading ? '0' : '1' ,
43
- imageRendering : width <= 40 ? 'pixelated' : 'auto' ,
44
- } }
45
- />
22
+ { isError || ! isImageAvailable ? (
23
+ < ImageUnavailable />
24
+ ) : (
25
+ < img
26
+ alt = { alt }
27
+ onError = { ( ) => setIsError ( true ) }
28
+ loading = "lazy"
29
+ onLoad = { event => {
30
+ const target = event . target as HTMLImageElement ;
31
+ setWidth ( target . naturalWidth ) ;
32
+ setIsLoading ( false ) ;
33
+ } }
34
+ src = { src }
35
+ style = { {
36
+ width : '100%' ,
37
+ height : '100%' ,
38
+ aspectRatio : '1 / 1' ,
39
+ objectFit : 'cover' ,
40
+ // display: 'none' breaks onLoad event firing
41
+ opacity : isLoading ? '0' : '1' ,
42
+ imageRendering : width <= 40 ? 'pixelated' : 'auto' ,
43
+ } }
44
+ />
45
+ ) }
46
46
</ CollectibleItemLayout >
47
47
) ;
48
48
}
0 commit comments