|
1 | | -import Main from './components/Main' |
2 | | -import Web3ContextProvider from './context/Web3Context' |
| 1 | +import { useEffect, useMemo, useState } from 'react'; |
| 2 | +import { type Events } from '@apillon/wallet-sdk'; |
| 3 | +import { useWallet } from '@apillon/wallet-react'; |
| 4 | +import { useWeb3Context } from './context/Web3Context'; |
| 5 | +import { useContract } from './hooks/useContract'; |
| 6 | +import { useWalletConnect } from './hooks/useWalletConnect'; |
| 7 | +import { useNft } from './hooks/useNft'; |
| 8 | +import Btn from './components/Btn'; |
| 9 | +import NftGallery from './components/Nft/NftGallery'; |
| 10 | +import CollectionInfo from './components/CollectionInfo'; |
| 11 | +import WalletConnect from './components/WalletConnect'; |
| 12 | +import Spinner from './components/Spinner'; |
| 13 | +import { toast } from 'react-toastify'; |
3 | 14 |
|
4 | 15 | function App() { |
| 16 | + const { collectionInfo, filterByAddress, loadingNfts, nfts, myNftIDs } = useWeb3Context(); |
| 17 | + const { loadCollectionInfo } = useContract(); |
| 18 | + const { wallet } = useWallet(); |
| 19 | + const { connected } = useWalletConnect(); |
| 20 | + const { getNfts, showNfts, showMyNfts } = useNft(); |
| 21 | + const [myNfts, setMyNfts] = useState(false); |
| 22 | + |
| 23 | + const filteredNfts = useMemo(() => { |
| 24 | + if (!filterByAddress) { |
| 25 | + return Object.values(nfts); |
| 26 | + } |
| 27 | + return Object.values(nfts).filter((nft) => myNftIDs.includes(nft.id)); |
| 28 | + }, [nfts, filterByAddress]); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + loadCollectionInfo(); |
| 32 | + }, []); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + if (connected) { |
| 36 | + getNfts(); |
| 37 | + loadCollectionInfo(); |
| 38 | + } |
| 39 | + }, [connected]); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + if (myNfts) { |
| 43 | + showMyNfts(); |
| 44 | + } else { |
| 45 | + showNfts(); |
| 46 | + } |
| 47 | + }, [myNfts]); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (wallet?.events) { |
| 51 | + wallet.events.on('addTokenStatus', ({ success }: Events['addTokenStatus']) => { |
| 52 | + success |
| 53 | + ? toast.success(`NFT successfully imported into the wallet!`) |
| 54 | + : toast.info('If you want to import NFT to wallet, paste contract address and token ID into your wallet.'); |
| 55 | + }); |
| 56 | + } |
| 57 | + }, [wallet]); |
| 58 | + |
5 | 59 | return ( |
6 | | - <Web3ContextProvider> |
7 | | - <Main /> |
8 | | - </Web3ContextProvider> |
9 | | - ) |
| 60 | + <div> |
| 61 | + <div className='relative flex flex-col justify-between gap-4 overflow-hidden rounded-xl bg-white p-8 text-center mobile:pt-14'> |
| 62 | + <CollectionInfo /> |
| 63 | + |
| 64 | + <div className='absolute right-[10px] top-[10px] flex flex-col items-end'> |
| 65 | + <WalletConnect /> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + |
| 69 | + {connected && collectionInfo?.name && ( |
| 70 | + <div className='max-w-lg mx-auto my-12'> |
| 71 | + <h3> |
| 72 | + The collection you are viewing supports nesting NFTs you own. To setup the nested relationship between NFTs, |
| 73 | + you first have to own them. |
| 74 | + </h3> |
| 75 | + <strong>Instructions:</strong> |
| 76 | + <ol> |
| 77 | + <li>Mint one or multiple NFTs</li> |
| 78 | + <li>Once minted, click on “My NFTs”</li> |
| 79 | + <li>The NFTs you own will be displayed</li> |
| 80 | + </ol> |
| 81 | + </div> |
| 82 | + )} |
| 83 | + |
| 84 | + {connected && collectionInfo?.name && ( |
| 85 | + <div className='mb-6'> |
| 86 | + <h2 className='text-center'>Show NFTs:</h2> |
| 87 | + <div className='flex justify-center items-center gap-2'> |
| 88 | + <Btn loading={loadingNfts && !filterByAddress} disabled={false} onClick={() => setMyNfts(false)}> |
| 89 | + All nfts |
| 90 | + </Btn> |
| 91 | + <Btn loading={loadingNfts && filterByAddress} disabled={false} onClick={() => setMyNfts(true)}> |
| 92 | + My nfts |
| 93 | + </Btn> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + )} |
| 97 | + |
| 98 | + {connected && ( |
| 99 | + <div> |
| 100 | + {(() => { |
| 101 | + if (loadingNfts) { |
| 102 | + return <Spinner />; |
| 103 | + } else if (Number(collectionInfo.totalSupply) === 0) { |
| 104 | + return <h2 className='text-center'>No NFTs, they must be minted first.</h2>; |
| 105 | + } else if (filterByAddress && !filteredNfts.length) { |
| 106 | + return <h2 className='text-center'>You don`t have any NFTs</h2>; |
| 107 | + } else if (!filteredNfts.length) { |
| 108 | + return <h2 className='text-center'>No NFTs, they must be minted first.</h2>; |
| 109 | + } else { |
| 110 | + return <NftGallery nfts={filteredNfts} />; |
| 111 | + } |
| 112 | + })()} |
| 113 | + </div> |
| 114 | + )} |
| 115 | + </div> |
| 116 | + ); |
10 | 117 | } |
11 | 118 |
|
12 | | -export default App |
| 119 | +export default App; |
0 commit comments