-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1312 from matiasbenary/feat/add-ft-linkdrops
Clear and fix linkdrops
- Loading branch information
Showing
29 changed files
with
1,316 additions
and
1,021 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,76 +1,38 @@ | ||
import Image from 'next/image'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { useContext } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
import { NearContext } from './wallet-selector/WalletSelector'; | ||
|
||
const RoundedImage = styled(Image)` | ||
border-radius: 50%; | ||
`; | ||
import type { NFT } from '@/utils/types'; | ||
|
||
interface Nft { | ||
contractId: string; | ||
tokenId: string; | ||
} | ||
import RoundedImage from './RoundedImage'; | ||
import { NearContext } from './wallet-selector/WalletSelector'; | ||
|
||
interface NftImageProps { | ||
nft?: Nft; | ||
ipfs_cid?: string; | ||
alt: string; | ||
nft?: NFT; | ||
} | ||
|
||
const DEFAULT_IMAGE = 'https://ipfs.near.social/ipfs/bafkreibmiy4ozblcgv3fm3gc6q62s55em33vconbavfd2ekkuliznaq3zm'; | ||
|
||
const getImage = (key: string) => { | ||
const imgUrl = localStorage.getItem(`keysImage:${key}`); | ||
return imgUrl || null; | ||
}; | ||
|
||
const setImage = (key: string, url: string) => { | ||
localStorage.setItem(`keysImage:${key}`, url); | ||
}; | ||
|
||
export const NftImage: React.FC<NftImageProps> = ({ nft, ipfs_cid, alt }) => { | ||
export const NftImage: React.FC<NftImageProps> = ({ nft }) => { | ||
const { wallet } = useContext(NearContext); | ||
const [imageUrl, setImageUrl] = useState<string>(DEFAULT_IMAGE); | ||
|
||
const fetchNftData = useCallback(async () => { | ||
if (!wallet || !nft || !nft.contractId || !nft.tokenId || ipfs_cid) return; | ||
|
||
const imgCache = getImage(nft.tokenId); | ||
if (imgCache) { | ||
setImageUrl(imgCache); | ||
return; | ||
} | ||
const [nftMetadata, tokenData] = await Promise.all([ | ||
wallet.viewMethod({ contractId: nft.contractId, method: 'nft_metadata' }), | ||
wallet.viewMethod({ contractId: nft.contractId, method: 'nft_token', args: { token_id: nft.tokenId } }), | ||
]); | ||
|
||
const tokenMedia = tokenData?.metadata?.media || ''; | ||
|
||
if (tokenMedia.startsWith('https://') || tokenMedia.startsWith('http://') || tokenMedia.startsWith('data:image')) { | ||
setImageUrl(tokenMedia); | ||
} else if (nftMetadata?.base_uri) { | ||
setImageUrl(`${nftMetadata.base_uri}/${tokenMedia}`); | ||
} else if (tokenMedia.startsWith('Qm') || tokenMedia.startsWith('ba')) { | ||
setImageUrl(`https://ipfs.near.social/ipfs/${tokenMedia}`); | ||
} | ||
}, [wallet, nft, ipfs_cid]); | ||
|
||
useEffect(() => { | ||
if (ipfs_cid) { | ||
setImageUrl(`https://ipfs.near.social/ipfs/${ipfs_cid}`); | ||
} else { | ||
fetchNftData(); | ||
} | ||
}, [ipfs_cid, fetchNftData]); | ||
const [imageUrl, setImageUrl] = useState<string>(''); | ||
|
||
useEffect(() => { | ||
if (!wallet || !nft || !nft.contractId || !nft.tokenId || ipfs_cid || DEFAULT_IMAGE === imageUrl) return; | ||
setImage(nft.tokenId, imageUrl); | ||
}, [imageUrl, wallet, nft, ipfs_cid]); | ||
|
||
return <RoundedImage width={43} height={43} src={imageUrl} alt={alt} />; | ||
const fetchNftData = async () => { | ||
if (!wallet || !nft || !nft.token_id) return; | ||
|
||
const tokenMedia = nft.metadata?.media || ''; | ||
|
||
if (tokenMedia.startsWith('https://') || tokenMedia.startsWith('http://')) { | ||
setImageUrl(tokenMedia); | ||
} else if (tokenMedia.startsWith('data:image')) { | ||
setImageUrl(tokenMedia); | ||
} else if (nft.metadata?.base_uri) { | ||
setImageUrl(`${nft.metadata.base_uri}/${tokenMedia}`); | ||
} else if (tokenMedia.startsWith('Qm') || tokenMedia.startsWith('ba')) { | ||
setImageUrl(`https://ipfs.near.social/ipfs/${tokenMedia}`); | ||
} | ||
}; | ||
|
||
fetchNftData(); | ||
}, [nft, imageUrl, wallet]); | ||
|
||
return <RoundedImage src={imageUrl} alt={nft?.metadata?.title || ''} />; | ||
}; |
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,27 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
export const Img = styled.img` | ||
border-radius: 50%; | ||
overflow: hidden; | ||
object-fit: cover; | ||
`; | ||
|
||
export const DEFAULT_IMAGE = | ||
'https://ipfs.near.social/ipfs/bafkreibmiy4ozblcgv3fm3gc6q62s55em33vconbavfd2ekkuliznaq3zm'; | ||
|
||
const RoundedImage = ({ src, alt }: { src: string; alt: string }) => { | ||
const [imageUrl, setImageUrl] = useState(src); | ||
|
||
useEffect(() => { | ||
setImageUrl(src); | ||
}, [src]); | ||
|
||
const handleError = useCallback(() => { | ||
setImageUrl(DEFAULT_IMAGE); | ||
}, []); | ||
|
||
return <Img height={43} width={43} src={imageUrl || DEFAULT_IMAGE} alt={alt} onError={handleError} />; | ||
}; | ||
|
||
export default RoundedImage; |
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.