Skip to content

Commit 30a2d8e

Browse files
committed
fix: show available metadata for stacks nfts, ref #5349
1 parent 5fd025f commit 30a2d8e

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { ReactNode, useState } from 'react';
22

3+
import { isValidUrl } from '@shared/utils/validate-url';
4+
35
import { CollectibleItemLayout, CollectibleItemLayoutProps } from './collectible-item.layout';
46
import { ImageUnavailable } from './image-unavailable';
57

@@ -13,36 +15,34 @@ export function CollectibleImage(props: CollectibleImageProps) {
1315
const [isError, setIsError] = useState(false);
1416
const [isLoading, setIsLoading] = useState(true);
1517
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);
2319

2420
return (
2521
<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+
)}
4646
</CollectibleItemLayout>
4747
);
4848
}

src/app/features/collectibles/components/stacks/stacks-non-fungible-tokens.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@ import { Metadata as StacksNftMetadata } from '@hirosystems/token-metadata-api-c
22

33
import { StxAvatarIcon } from '@leather.io/ui';
44

5-
import { isValidUrl } from '@shared/utils/validate-url';
6-
75
import { CollectibleImage } from '../../../../components/collectibles/collectible-image';
8-
import { ImageUnavailable } from '../../../../components/collectibles/image-unavailable';
96

107
interface StacksNonFungibleTokensProps {
118
metadata: StacksNftMetadata;
129
}
13-
export function StacksNonFungibleTokens({ metadata }: StacksNonFungibleTokensProps) {
14-
const isImageAvailable = metadata.cached_image && isValidUrl(metadata.cached_image);
15-
16-
if (!isImageAvailable) return <ImageUnavailable />;
1710

11+
export function StacksNonFungibleTokens({ metadata }: StacksNonFungibleTokensProps) {
1812
return (
1913
<CollectibleImage
2014
alt="stacks nft"

0 commit comments

Comments
 (0)