-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Closes #1686 - Closes `FE-1087` --- | 📷 Demo | | --- | | <video src="https://github.com/user-attachments/assets/d2771cb6-fe1a-4c83-9ab6-db2be421d1c6" /> |
- Loading branch information
1 parent
ab1ed94
commit 2460bee
Showing
15 changed files
with
362 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"fuels-wallet": minor | ||
--- | ||
|
||
Add a separate NFTs tab to the home screen. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
packages/app/src/systems/Account/components/BalanceNFTs/BalanceNFTs.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { cssObj } from '@fuel-ui/css'; | ||
import { Accordion, Badge, Box, Copyable, VStack } from '@fuel-ui/react'; | ||
import type { CoinAsset } from '@fuel-wallet/types'; | ||
import { useMemo } from 'react'; | ||
import { AssetListEmpty } from '~/systems/Asset/components/AssetList/AssetListEmpty'; | ||
import { shortAddress } from '~/systems/Core'; | ||
import { NFTImage } from './NFTImage'; | ||
import { | ||
UNKNOWN_COLLECTION_TITLE, | ||
groupNFTsByCollection, | ||
} from './groupNFTsByCollection'; | ||
|
||
interface BalanceNFTsProps { | ||
balances: CoinAsset[] | undefined; | ||
} | ||
|
||
export const BalanceNFTs = ({ balances = [] }: BalanceNFTsProps) => { | ||
const { collections, defaultValue } = useMemo(() => { | ||
const collections = groupNFTsByCollection(balances); | ||
const defaultValue = collections | ||
.map((collection) => collection.name) | ||
.filter((collection) => collection !== UNKNOWN_COLLECTION_TITLE); | ||
|
||
return { | ||
collections, | ||
defaultValue, | ||
}; | ||
}, [balances]); | ||
|
||
if (collections.length === 0) { | ||
return ( | ||
<AssetListEmpty | ||
text="You don't have any NFTs" | ||
supportText="To add NFTs, simply send them to your Fuel address." | ||
hideFaucet | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<Box css={styles.root}> | ||
<Accordion type="multiple" defaultValue={defaultValue}> | ||
{collections.map((collection) => { | ||
return ( | ||
<Accordion.Item key={collection.name} value={collection.name}> | ||
<Accordion.Trigger> | ||
<Badge variant="ghost" color="gray" as="span"> | ||
{collection.nfts.length} | ||
</Badge> | ||
{collection.name} | ||
</Accordion.Trigger> | ||
<Accordion.Content> | ||
<Box css={styles.grid}> | ||
{collection.nfts.map((nft) => { | ||
return ( | ||
<div key={nft.assetId}> | ||
<NFTImage assetId={nft.assetId} image={nft.image} /> | ||
<Copyable css={styles.name} value={nft.assetId}> | ||
{nft.name || shortAddress(nft.assetId)} | ||
</Copyable> | ||
</div> | ||
); | ||
})} | ||
</Box> | ||
</Accordion.Content> | ||
</Accordion.Item> | ||
); | ||
})} | ||
</Accordion> | ||
</Box> | ||
); | ||
}; | ||
|
||
const styles = { | ||
root: cssObj({ | ||
'.fuel_Accordion-trigger': { | ||
fontSize: '$base', | ||
fontWeight: '$medium', | ||
backgroundColor: 'transparent', | ||
color: '$intentsBase11', | ||
padding: '$0', | ||
gap: '$2', | ||
flexDirection: 'row-reverse', | ||
justifyContent: 'flex-start', | ||
}, | ||
'.fuel_Accordion-trigger:hover': { | ||
color: '$intentsBase12', | ||
}, | ||
'.fuel_Accordion-trigger[data-state="open"]': { | ||
color: '$intentsBase12', | ||
}, | ||
'.fuel_Accordion-trigger[data-state="closed"] .fuel_Accordion-icon': { | ||
transform: 'rotate(-45deg)', | ||
}, | ||
'.fuel_Accordion-trigger[data-state="open"] .fuel_Accordion-icon': { | ||
transform: 'rotate(0deg)', | ||
}, | ||
'.fuel_Accordion-item': { | ||
backgroundColor: 'transparent', | ||
borderBottom: '1px solid $border', | ||
borderRadius: '$none', | ||
|
||
svg: { | ||
width: '$3', | ||
height: '$3', | ||
}, | ||
}, | ||
'.fuel_Accordion-content': { | ||
border: '0', | ||
padding: '$0 5px $2 20px', | ||
}, | ||
'.fuel_Badge': { | ||
display: 'inline-flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
fontWeight: '$normal', | ||
fontSize: '$xs', | ||
padding: '$0', | ||
height: '$5', | ||
minWidth: '$5', | ||
pointerEvents: 'none', | ||
marginLeft: 'auto', | ||
lineHeight: 'normal', | ||
}, | ||
}), | ||
grid: cssObj({ | ||
display: 'grid', | ||
gridTemplateColumns: 'repeat(3, 1fr)', | ||
gap: '$3', | ||
}), | ||
name: cssObj({ | ||
marginTop: '$1', | ||
gap: '$0', | ||
fontSize: '$xs', | ||
lineHeight: '$none', | ||
textAlign: 'center', | ||
}), | ||
}; |
81 changes: 81 additions & 0 deletions
81
packages/app/src/systems/Account/components/BalanceNFTs/NFTImage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { cssObj } from '@fuel-ui/css'; | ||
import { Box, ContentLoader, Icon } from '@fuel-ui/react'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { shortAddress } from '~/systems/Core'; | ||
|
||
interface NFTImageProps { | ||
assetId: string; | ||
image: string | undefined; | ||
} | ||
|
||
export const NFTImage = ({ assetId, image }: NFTImageProps) => { | ||
const imgRef = useRef<HTMLImageElement>(null); | ||
|
||
const [fallback, setFallback] = useState(false); | ||
const [isLoading, setLoading] = useState(true); | ||
|
||
useEffect(() => { | ||
if (imgRef.current?.complete) { | ||
if (imgRef.current.naturalWidth) { | ||
setLoading(false); | ||
return; | ||
} | ||
|
||
setFallback(true); | ||
} | ||
}, []); | ||
|
||
if (image && !fallback) { | ||
return ( | ||
<Box css={styles.item}> | ||
{isLoading && ( | ||
<ContentLoader width="100%" height="100%" viewBox="0 0 22 22"> | ||
<rect x="0" y="0" rx="0" ry="0" width="22" height="22" /> | ||
</ContentLoader> | ||
)} | ||
<img | ||
ref={imgRef} | ||
src={image} | ||
alt={shortAddress(assetId)} | ||
data-loading={isLoading} | ||
onLoad={() => setLoading(false)} | ||
onError={() => { | ||
setFallback(true); | ||
}} | ||
/> | ||
</Box> | ||
); | ||
} | ||
|
||
return ( | ||
<Box css={styles.noImage}> | ||
<Icon icon={Icon.is('FileOff')} /> | ||
</Box> | ||
); | ||
}; | ||
|
||
const styles = { | ||
item: cssObj({ | ||
aspectRatio: '1 / 1', | ||
borderRadius: '12px', | ||
overflow: 'hidden', | ||
|
||
img: { | ||
width: '100%', | ||
objectFit: 'cover', | ||
}, | ||
'img[data-loading="true"]': { | ||
display: 'none', | ||
}, | ||
}), | ||
noImage: cssObj({ | ||
width: '100%', | ||
aspectRatio: '1 / 1', | ||
borderRadius: '12px', | ||
border: '1px solid $cardBorder', | ||
backgroundColor: '$cardBg', | ||
display: 'flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}), | ||
}; |
71 changes: 71 additions & 0 deletions
71
packages/app/src/systems/Account/components/BalanceNFTs/groupNFTsByCollection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { CoinAsset } from '@fuel-wallet/types'; | ||
|
||
export const UNKNOWN_COLLECTION_TITLE = 'Others'; | ||
|
||
interface NFT { | ||
assetId: string; | ||
name: string | undefined; | ||
image: string | undefined; | ||
} | ||
interface Collection { | ||
name: string; | ||
nfts: NFT[]; | ||
} | ||
|
||
export const groupNFTsByCollection = (balances: CoinAsset[]): Collection[] => { | ||
const grouped: Collection[] = balances | ||
// Filter only NFTs | ||
.filter((balance) => { | ||
return balance.asset?.contractId && Boolean(balance.asset?.isNft); | ||
}) | ||
|
||
// Group balances by collection name | ||
.reduce((acc, balance) => { | ||
const name = balance.asset?.collection || UNKNOWN_COLLECTION_TITLE; | ||
let collection = acc.find((item) => item.name === name); | ||
|
||
if (!collection) { | ||
collection = { name, nfts: [] }; | ||
acc.push(collection); | ||
} | ||
|
||
const image = balance.asset?.metadata?.image?.replace( | ||
'ipfs://', | ||
'https://ipfs.io/ipfs/' | ||
); | ||
|
||
collection.nfts.push({ | ||
assetId: balance.assetId, | ||
name: balance?.asset?.metadata?.name, | ||
image, | ||
}); | ||
|
||
return acc; | ||
}, [] as Collection[]) | ||
|
||
// Sort NFTs by name | ||
.map((collection) => { | ||
return { | ||
name: collection.name, | ||
nfts: collection.nfts.sort((a, b) => { | ||
if (a.name && b.name) { | ||
return a.name.localeCompare(b.name, undefined, { | ||
numeric: true, | ||
sensitivity: 'base', | ||
}); | ||
} | ||
|
||
return 0; | ||
}), | ||
}; | ||
}) | ||
|
||
// Move "Others" to the bottom | ||
.sort((a, b) => { | ||
if (a.name === UNKNOWN_COLLECTION_TITLE) return 1; | ||
if (b.name === UNKNOWN_COLLECTION_TITLE) return -1; | ||
return 0; | ||
}); | ||
|
||
return grouped; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.