Skip to content

Commit

Permalink
fix: handle invalid image paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss committed Nov 11, 2024
1 parent a9c7f11 commit 0ce5706
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';

import IconEmptyImage from '@assets/icon-empty-image.svg';
import { Loading } from '@components/atoms';
Expand All @@ -11,6 +11,12 @@ export interface NFTCardImageProps {
}

const NFTCardImage: React.FC<NFTCardImageProps> = ({ isFetched, image, hasBadge = false }) => {
const [hasError, setHasError] = useState(false);

const handleError = (): void => {
setHasError(true);
};

if (!isFetched) {
return (
<NFTCardImageSkeletonBox>
Expand All @@ -19,7 +25,7 @@ const NFTCardImage: React.FC<NFTCardImageProps> = ({ isFetched, image, hasBadge
);
}

if (!image) {
if (!image || hasError) {
return (
<NFTCardImageWrapper className='empty'>
<img className='empty-image' src={IconEmptyImage} alt='empty image' />
Expand All @@ -29,7 +35,7 @@ const NFTCardImage: React.FC<NFTCardImageProps> = ({ isFetched, image, hasBadge

return (
<NFTCardImageWrapper>
<img className='nft-image' src={image} />
<img className='nft-image' src={image} onError={handleError} alt='nft image' />
</NFTCardImageWrapper>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import FailedIcon from '@assets/failed.svg';
import SuccessIcon from '@assets/success.svg';
import { TokenBalance } from '@components/molecules';
import { UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
import React, { useCallback, useMemo } from 'react';
import React, { useCallback, useMemo, useState } from 'react';
import { TransactionHistoryListItemWrapper } from './transaction-history-list-item.styles';

export interface TransactionHistoryListItemProps {
Expand Down Expand Up @@ -43,31 +43,44 @@ const TransactionHistoryListItem: React.FC<TransactionHistoryListItemProps> = (a
queryGRC721TokenUri,
onClickItem,
} = args;
const [hasLogoError, setHasLogoError] = useState(false);

const tokenUriQuery =
type === 'TRANSFER_GRC721' && queryGRC721TokenUri !== undefined
? queryGRC721TokenUri(logo || '', '0')
: null;

const logoImage = useMemo(() => {
if (hasLogoError) {
return `${UnknownTokenIcon}`;
}

if (type === 'TRANSFER_GRC721' && tokenUriQuery) {
return tokenUriQuery?.data || `${UnknownTokenIcon}`;
}

if (type === 'ADD_PACKAGE') {
return `${AddPackageIcon}`;
}

if (type === 'CONTRACT_CALL') {
return `${ContractIcon}`;
}

if (type === 'MULTI_CONTRACT_CALL') {
return `${ContractIcon}`;
}

if (!logo) {
return `${UnknownTokenIcon}`;
}

return `${logo}`;
}, [type, logo, tokenUriQuery]);
}, [hasLogoError, type, logo, tokenUriQuery]);

const handleLogoError = (): void => {
setHasLogoError(true);
};

const getValueTypeClassName = useCallback(() => {
if (valueType === 'ACTIVE') {
Expand All @@ -82,7 +95,7 @@ const TransactionHistoryListItem: React.FC<TransactionHistoryListItemProps> = (a
return (
<TransactionHistoryListItemWrapper onClick={(): void => onClickItem(hash)}>
<div className='logo-wrapper'>
<img className='logo' src={logoImage} alt='logo image' />
<img className='logo' src={logoImage} alt='logo image' onError={handleLogoError} />
<img
className='badge'
src={status === 'SUCCESS' ? SuccessIcon : FailedIcon}
Expand Down

0 comments on commit 0ce5706

Please sign in to comment.