Skip to content

Commit

Permalink
feat: add contract logo
Browse files Browse the repository at this point in the history
  • Loading branch information
helciofranco committed Jan 1, 2025
1 parent 90ed577 commit 32fae91
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { cssObj } from '@fuel-ui/css';
import { Avatar, Box, Card, Heading, Icon, Text } from '@fuel-ui/react';
import { Avatar, Box, Card, Heading, Icon, Image, Text } from '@fuel-ui/react';
import type { OperationTransactionAddress } from 'fuels';
import { Address, AddressType, ChainName, isB256, isBech32 } from 'fuels';
import type { FC } from 'react';
import { type FC, useMemo } from 'react';
import { EthAddress, FuelAddress, useAccounts } from '~/systems/Account';

import { useContractMetadata } from '~/systems/Contract/hooks/useContractMetadata';
import { TxRecipientCardLoader } from './TxRecipientCardLoader';
import { TxRecipientContractLogo } from './TxRecipientContractLogo';

export type TxRecipientCardProps = {
recipient?: OperationTransactionAddress;
Expand All @@ -29,8 +31,16 @@ export const TxRecipientCard: TxRecipientCardComponent = ({
const isContract = recipient?.type === AddressType.contract;
const isEthChain = recipient?.chain === ChainName.ethereum;
const isNetwork = address === 'Network';
const name =
accounts?.find((a) => a.address === fuelAddress)?.name || 'unknown';

const contract = useContractMetadata(address);

const name = useMemo<string>(() => {
if (isContract) {
return contract?.name || 'unknown';
}

return accounts?.find((a) => a.address === fuelAddress)?.name || 'unknown';
}, [isContract, contract, accounts, fuelAddress]);

return (
<Card
Expand Down Expand Up @@ -72,7 +82,11 @@ export const TxRecipientCard: TxRecipientCardComponent = ({
)}
{isContract && !isNetwork && (
<Box css={styles.iconWrapper}>
<Icon icon={Icon.is('Code')} size={20} />
<TxRecipientContractLogo
name={contract?.name}
image={contract?.image}
size={56}
/>
</Box>
)}
<Box.Flex css={styles.info}>
Expand Down Expand Up @@ -133,6 +147,7 @@ const styles = {
alignItems: 'center',
background: '$intentsBase3',
borderRadius: '$full',
overflow: 'hidden',
}),
info: cssObj({
flexDirection: 'column',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { cssObj } from '@fuel-ui/css';
import { Icon, Image } from '@fuel-ui/react';
import { useEffect, useRef, useState } from 'react';
import { getProjectImage } from '~/systems/Ecosystem/utils/getProjectImage';

type TxRecipientContractLogoProps = {
name?: string;
image?: string;
size?: number;
};

export function TxRecipientContractLogo({
name,
image,
size = 20,
}: TxRecipientContractLogoProps) {
const imgRef = useRef<HTMLImageElement>(null);
const [imageFallback, setImageFallback] = useState(false);
const [isImageLoading, setIsImageLoading] = useState(true);

useEffect(() => {
if (imgRef.current?.complete) {
if (imgRef.current.naturalWidth) {
setIsImageLoading(false);
return;
}

setImageFallback(true);
}
}, []);

if (image && !imageFallback) {
return (
<Image
ref={imgRef}
src={getProjectImage(image)}
alt={name}
width={size}
height={size}
css={styles.avatar}
data-hidden={isImageLoading}
onLoad={() => setIsImageLoading(false)}
onError={() => {
setImageFallback(true);
}}
/>
);
}

return <Icon icon={Icon.is('Code')} size={20} />;
}

const styles = {
avatar: cssObj({
'&[data-hidden="true"]': {
display: 'none',
},
}),
};

0 comments on commit 32fae91

Please sign in to comment.