generated from Dhaiwat10/react-library-starter
-
Notifications
You must be signed in to change notification settings - Fork 154
NFT component improvements #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
23b61d1
feat: add ENS support for NFTGallery
Dhaiwat10 225da8f
refactor(NFT): accept contractAddress and tokenId to <NFT />
Dhaiwat10 3e22a6a
Update <NFTGallery/> stories
Dhaiwat10 7f5debc
feat: add support for video NFTs
Dhaiwat10 1fcf370
feat: add support for audio NFTs and some progress on tests
Dhaiwat10 0606463
Fix tests and improve data fetching logic
Dhaiwat10 5db3afe
Refactor stories
Dhaiwat10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,20 +1,19 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
import { NFT } from './NFT'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
describe('NFT', () => { | ||
it('displays the NFT name', () => { | ||
const { container } = render( | ||
<NFT | ||
tokenId='1' | ||
name='Dev #1' | ||
imageUrl='https://storage.opensea.io/files/acef01b1f111088c40a0d86a4cd8a2bd.svg' | ||
assetContractName='Devs for Revolution' | ||
assetContractSymbol='DEVS' | ||
/> | ||
); | ||
|
||
expect(container.textContent).toContain('Dev #1'); | ||
it('displays an image NFT properly', async () => { | ||
act(() => { | ||
render(<NFT tokenId='1' contractAddress='0x25ed58c027921e14d86380ea2646e3a1b5c55a8b' />); | ||
}); | ||
const name = await screen.findByText('Dev #1'); | ||
const image = await screen.findByAltText('Dev #1'); | ||
expect(name).toBeInTheDocument(); | ||
expect(image).toBeInTheDocument(); | ||
}); | ||
|
||
//TODO: test for video NFT | ||
}); |
This file contains hidden or 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 |
---|---|---|
@@ -1,55 +1,129 @@ | ||
import React from 'react'; | ||
import { Box, Heading, Image, Flex, Tag, Text } from '@chakra-ui/react'; | ||
import React, { useCallback, useEffect, useRef } from 'react'; | ||
import { | ||
Box, | ||
Heading, | ||
Image, | ||
Flex, | ||
Tag, | ||
Text, | ||
VStack, | ||
Skeleton, | ||
Alert, | ||
AlertIcon, | ||
} from '@chakra-ui/react'; | ||
import fetch from 'cross-fetch'; | ||
|
||
export interface NFTProps { | ||
/** | ||
* The id for the NFT, unique within the contract | ||
*/ | ||
contractAddress: string; | ||
tokenId: string; | ||
/** | ||
* The name of the NFT, potentially null | ||
*/ | ||
} | ||
|
||
export interface NFTData { | ||
tokenId: string; | ||
imageUrl?: string; | ||
name: string | null; | ||
/** | ||
* The image of the NFT, cached from OpenSea | ||
*/ | ||
imageUrl: string; | ||
/** | ||
* The name of the NFT collection | ||
*/ | ||
assetContractName: string; | ||
/** | ||
* The symbol for the NFT collection | ||
*/ | ||
assetContractSymbol: string; | ||
assetContractName: string; | ||
animationUrl?: string; | ||
} | ||
|
||
/** | ||
* Component to display an NFT given render params | ||
* Component to fetch and display NFT data | ||
*/ | ||
export const NFT = ({ | ||
tokenId, | ||
name, | ||
imageUrl, | ||
assetContractName, | ||
assetContractSymbol, | ||
}: NFTProps) => { | ||
const displayName = name || tokenId; | ||
export const NFT = ({ contractAddress, tokenId }: NFTProps) => { | ||
const _isMounted = useRef(true); | ||
const [nftData, setNftData] = React.useState<NFTData>(); | ||
const [errorMessage, setErrorMessage] = React.useState<string>(); | ||
|
||
const fetchNFTData = useCallback(async () => { | ||
try { | ||
const res = await fetch(`https://api.opensea.io/api/v1/asset/${contractAddress}/${tokenId}/`); | ||
if (!res.ok) { | ||
throw Error( | ||
`OpenSea request failed with status: ${res.status}. Make sure you are on mainnet.` | ||
); | ||
} | ||
const data = await res.json(); | ||
if (_isMounted.current) { | ||
setNftData({ | ||
tokenId: data.token_id, | ||
imageUrl: data.image_url, | ||
name: data.name, | ||
assetContractName: data.asset_contract.name, | ||
assetContractSymbol: data.asset_contract.symbol, | ||
animationUrl: data.animation_url, | ||
}); | ||
} | ||
} catch (error: any) { | ||
setErrorMessage(error.message); | ||
} | ||
}, [contractAddress, tokenId]); | ||
|
||
useEffect(() => { | ||
_isMounted.current = true; | ||
fetchNFTData(); | ||
return () => { | ||
_isMounted.current = false; | ||
}; | ||
}, [contractAddress, tokenId]); | ||
|
||
return <NFTCard data={nftData} errorMessage={errorMessage} />; | ||
}; | ||
|
||
/** | ||
* Private component to display an NFT given the data | ||
*/ | ||
export const NFTCard = ({ | ||
data, | ||
errorMessage = '', | ||
}: { | ||
data: NFTData | undefined | null; | ||
errorMessage?: string | undefined; | ||
}) => { | ||
const name = data?.name; | ||
const imageUrl = data?.imageUrl; | ||
const assetContractName = data?.assetContractName; | ||
const assetContractSymbol = data?.assetContractSymbol; | ||
const animationUrl = data?.animationUrl; | ||
const tokenId = data?.tokenId; | ||
const displayName = name || `${assetContractSymbol} #${tokenId}`; | ||
|
||
if (errorMessage) { | ||
return ( | ||
<Alert status='error'> | ||
<AlertIcon /> | ||
{errorMessage} | ||
</Alert> | ||
); | ||
} | ||
|
||
return ( | ||
<Box maxW='xs' borderRadius='lg' borderWidth='1px' overflow='hidden'> | ||
<Image src={imageUrl} alt={displayName} borderRadius='lg' /> | ||
<Box p='6'> | ||
<Flex alignItems='center' justifyContent='space-between' pb='2'> | ||
<Heading as='h3' size='sm'> | ||
{displayName} | ||
</Heading> | ||
<Tag size='sm'>{assetContractSymbol}</Tag> | ||
</Flex> | ||
<Text fontSize='xs'> | ||
{assetContractName} #{tokenId} | ||
</Text> | ||
<Skeleton isLoaded={!!data} maxW='xs' h='md'> | ||
<Box maxW='xs' borderRadius='lg' borderWidth='1px' overflow='hidden'> | ||
{animationUrl ? ( | ||
animationUrl.endsWith('.mp3') ? ( | ||
<VStack> | ||
<Image src={imageUrl} alt={displayName} borderRadius='lg' /> | ||
<audio src={animationUrl} controls autoPlay muted style={{ borderRadius: '7px' }} /> | ||
</VStack> | ||
) : ( | ||
<video src={animationUrl} controls autoPlay muted /> | ||
) | ||
) : ( | ||
<Image src={imageUrl} alt={displayName} borderRadius='lg' /> | ||
)} | ||
<Box p='6'> | ||
<Flex alignItems='center' justifyContent='space-between' pb='2'> | ||
<Heading as='h3' size='sm'> | ||
{displayName} | ||
</Heading> | ||
{assetContractSymbol && <Tag size='sm'>{assetContractSymbol}</Tag>} | ||
</Flex> | ||
<Text fontSize='xs'> | ||
{assetContractName} #{tokenId} | ||
</Text> | ||
</Box> | ||
</Box> | ||
</Box> | ||
</Skeleton> | ||
); | ||
}; |
26 changes: 24 additions & 2 deletions
26
packages/components/src/components/NFTGallery/NFTGallery.stories.tsx
This file contains hidden or 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 |
---|---|---|
@@ -1,11 +1,33 @@ | ||
import React from 'react'; | ||
import { ethers } from 'ethers'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { NFTGallery } from '.'; | ||
|
||
export default { | ||
title: 'Components/NFTGallery', | ||
component: NFTGallery, | ||
parameters: { | ||
// TODO: Fix window.ethereum is undefined breaking chromatic | ||
chromatic: { disableSnapshot: true }, | ||
}, | ||
}; | ||
|
||
export const Default = () => <NFTGallery address='0x1A16c87927570239FECD343ad2654fD81682725e' />; | ||
export const nftsOwnedByAnAccount = () => ( | ||
<NFTGallery address='0x1A16c87927570239FECD343ad2654fD81682725e' /> | ||
); | ||
|
||
export const nftsOwnedByAnENS = () => { | ||
const [provider, setProvider] = useState<ethers.providers.Web3Provider>(); | ||
|
||
useEffect(() => { | ||
const provider = new ethers.providers.Web3Provider(window.ethereum); | ||
setProvider(provider); | ||
}, []); | ||
|
||
if (!provider) { | ||
return <>Loading...</>; | ||
} | ||
|
||
return <NFTGallery address='dhaiwat.eth' web3Provider={provider} />; | ||
}; | ||
|
||
export const WithAnError = () => <NFTGallery address='bad_address' />; |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think we could take the approach i mentioned here for the NFT component:#46 (comment)
We should keep the existing component around and use it for the
NFTGallery
component so we don't need to refetch the NFT data that already comes from the previous APIThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@etr2460 missed that. That sounds fair