|
| 1 | +import { cssObj } from '@fuel-ui/css'; |
| 2 | +import { Accordion, Badge, Box, Copyable, VStack } from '@fuel-ui/react'; |
| 3 | +import type { CoinAsset } from '@fuel-wallet/types'; |
| 4 | +import { useMemo } from 'react'; |
| 5 | +import { AssetListEmpty } from '~/systems/Asset/components/AssetList/AssetListEmpty'; |
| 6 | +import { shortAddress } from '~/systems/Core'; |
| 7 | +import { NFTImage } from './NFTImage'; |
| 8 | +import { |
| 9 | + UNKNOWN_COLLECTION_TITLE, |
| 10 | + groupNFTsByCollection, |
| 11 | +} from './groupNFTsByCollection'; |
| 12 | + |
| 13 | +interface BalanceNFTsProps { |
| 14 | + balances: CoinAsset[] | undefined; |
| 15 | +} |
| 16 | + |
| 17 | +export const BalanceNFTs = ({ balances = [] }: BalanceNFTsProps) => { |
| 18 | + const { collections, defaultValue } = useMemo(() => { |
| 19 | + const collections = groupNFTsByCollection(balances); |
| 20 | + const defaultValue = collections |
| 21 | + .map((collection) => collection.name) |
| 22 | + .filter((collection) => collection !== UNKNOWN_COLLECTION_TITLE); |
| 23 | + |
| 24 | + return { |
| 25 | + collections, |
| 26 | + defaultValue, |
| 27 | + }; |
| 28 | + }, [balances]); |
| 29 | + |
| 30 | + if (collections.length === 0) { |
| 31 | + return ( |
| 32 | + <AssetListEmpty |
| 33 | + text="You don't have any NFTs" |
| 34 | + supportText="To add NFTs, simply send them to your Fuel address." |
| 35 | + hideFaucet |
| 36 | + /> |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + return ( |
| 41 | + <Box css={styles.root}> |
| 42 | + <Accordion type="multiple" defaultValue={defaultValue}> |
| 43 | + {collections.map((collection) => { |
| 44 | + return ( |
| 45 | + <Accordion.Item key={collection.name} value={collection.name}> |
| 46 | + <Accordion.Trigger> |
| 47 | + <Badge variant="ghost" color="gray" as="span"> |
| 48 | + {collection.nfts.length} |
| 49 | + </Badge> |
| 50 | + {collection.name} |
| 51 | + </Accordion.Trigger> |
| 52 | + <Accordion.Content> |
| 53 | + <Box css={styles.grid}> |
| 54 | + {collection.nfts.map((nft) => { |
| 55 | + return ( |
| 56 | + <div key={nft.assetId}> |
| 57 | + <NFTImage assetId={nft.assetId} image={nft.image} /> |
| 58 | + <Copyable css={styles.name} value={nft.assetId}> |
| 59 | + {nft.name || shortAddress(nft.assetId)} |
| 60 | + </Copyable> |
| 61 | + </div> |
| 62 | + ); |
| 63 | + })} |
| 64 | + </Box> |
| 65 | + </Accordion.Content> |
| 66 | + </Accordion.Item> |
| 67 | + ); |
| 68 | + })} |
| 69 | + </Accordion> |
| 70 | + </Box> |
| 71 | + ); |
| 72 | +}; |
| 73 | + |
| 74 | +const styles = { |
| 75 | + root: cssObj({ |
| 76 | + '.fuel_Accordion-trigger': { |
| 77 | + fontSize: '$base', |
| 78 | + fontWeight: '$medium', |
| 79 | + backgroundColor: 'transparent', |
| 80 | + color: '$intentsBase11', |
| 81 | + padding: '$0', |
| 82 | + gap: '$2', |
| 83 | + flexDirection: 'row-reverse', |
| 84 | + justifyContent: 'flex-start', |
| 85 | + }, |
| 86 | + '.fuel_Accordion-trigger:hover': { |
| 87 | + color: '$intentsBase12', |
| 88 | + }, |
| 89 | + '.fuel_Accordion-trigger[data-state="open"]': { |
| 90 | + color: '$intentsBase12', |
| 91 | + }, |
| 92 | + '.fuel_Accordion-trigger[data-state="closed"] .fuel_Accordion-icon': { |
| 93 | + transform: 'rotate(-45deg)', |
| 94 | + }, |
| 95 | + '.fuel_Accordion-trigger[data-state="open"] .fuel_Accordion-icon': { |
| 96 | + transform: 'rotate(0deg)', |
| 97 | + }, |
| 98 | + '.fuel_Accordion-item': { |
| 99 | + backgroundColor: 'transparent', |
| 100 | + borderBottom: '1px solid $border', |
| 101 | + borderRadius: '$none', |
| 102 | + |
| 103 | + svg: { |
| 104 | + width: '$3', |
| 105 | + height: '$3', |
| 106 | + }, |
| 107 | + }, |
| 108 | + '.fuel_Accordion-content': { |
| 109 | + border: '0', |
| 110 | + padding: '$0 5px $2 20px', |
| 111 | + }, |
| 112 | + '.fuel_Badge': { |
| 113 | + display: 'inline-flex', |
| 114 | + justifyContent: 'center', |
| 115 | + alignItems: 'center', |
| 116 | + fontWeight: '$normal', |
| 117 | + fontSize: '$xs', |
| 118 | + padding: '$0', |
| 119 | + height: '$5', |
| 120 | + minWidth: '$5', |
| 121 | + pointerEvents: 'none', |
| 122 | + marginLeft: 'auto', |
| 123 | + lineHeight: 'normal', |
| 124 | + }, |
| 125 | + }), |
| 126 | + grid: cssObj({ |
| 127 | + display: 'grid', |
| 128 | + gridTemplateColumns: 'repeat(3, 1fr)', |
| 129 | + gap: '$3', |
| 130 | + }), |
| 131 | + name: cssObj({ |
| 132 | + marginTop: '$1', |
| 133 | + gap: '$0', |
| 134 | + fontSize: '$xs', |
| 135 | + lineHeight: '$none', |
| 136 | + textAlign: 'center', |
| 137 | + }), |
| 138 | +}; |
0 commit comments