|
| 1 | +import React, { useEffect, useMemo, useState } from "react"; |
| 2 | +import ReactDOM from "react-dom"; |
| 3 | +import { |
| 4 | + ChakraProvider, |
| 5 | + Flex, |
| 6 | + Heading, |
| 7 | + Image, |
| 8 | + Stack, |
| 9 | + AspectRatio, |
| 10 | + Box, |
| 11 | + IconButton, |
| 12 | +} from "@chakra-ui/react"; |
| 13 | +import { ThirdwebWeb3Provider, useWeb3 } from "@3rdweb/hooks"; |
| 14 | +import { PoweredBy } from "../shared/powered-by"; |
| 15 | +import { ConnectWallet } from "@3rdweb/react"; |
| 16 | +import { ThirdwebSDK, NFTMetadata, ModuleMetadata } from "@3rdweb/sdk"; |
| 17 | +import { ChevronRightIcon, ChevronLeftIcon } from "@chakra-ui/icons"; |
| 18 | + |
| 19 | +const connectors = { |
| 20 | + injected: {}, |
| 21 | +}; |
| 22 | + |
| 23 | +const Providers: React.FC = () => { |
| 24 | + return ( |
| 25 | + <ThirdwebWeb3Provider supportedChainIds={[137]} connectors={connectors}> |
| 26 | + <ChakraProvider> |
| 27 | + <App /> |
| 28 | + </ChakraProvider> |
| 29 | + </ThirdwebWeb3Provider> |
| 30 | + ); |
| 31 | +}; |
| 32 | + |
| 33 | +const App: React.FC = () => { |
| 34 | + return <Layout title="Foo Bar" />; |
| 35 | +}; |
| 36 | + |
| 37 | +export default function runNFT() { |
| 38 | + ReactDOM.render(<Providers />, document.getElementById("root")); |
| 39 | +} |
| 40 | + |
| 41 | +interface LayoutProps { |
| 42 | + title: string; |
| 43 | +} |
| 44 | + |
| 45 | +const Layout: React.FC<LayoutProps> = ({ title }) => { |
| 46 | + const { address, provider } = useWeb3(); |
| 47 | + |
| 48 | + const collection = useMemo(() => { |
| 49 | + if (!provider) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + return new ThirdwebSDK(provider).getNFTModule( |
| 53 | + "0x294261Ce5C5A5F8a709E1cA94D15b954bB42C2C8" |
| 54 | + ); |
| 55 | + }, [provider]); |
| 56 | + |
| 57 | + const [owned, setOwned] = useState<NFTMetadata[]>([]); |
| 58 | + const [metadata, setMetadata] = useState<ModuleMetadata>(undefined); |
| 59 | + |
| 60 | + useEffect(() => { |
| 61 | + let __mounted = true; |
| 62 | + const getAsyncData = async () => { |
| 63 | + const _owned = await collection.getOwned(address); |
| 64 | + const _metadata = await collection.getMetadata(); |
| 65 | + if (__mounted) { |
| 66 | + setOwned(_owned); |
| 67 | + setMetadata(_metadata); |
| 68 | + } |
| 69 | + }; |
| 70 | + if (address) { |
| 71 | + getAsyncData(); |
| 72 | + } |
| 73 | + return () => { |
| 74 | + __mounted = false; |
| 75 | + }; |
| 76 | + }, [collection, address]); |
| 77 | + |
| 78 | + const [currentIndex, setCurrentIndex] = useState(0); |
| 79 | + |
| 80 | + const next = () => { |
| 81 | + setCurrentIndex((val) => { |
| 82 | + let next = val + 1; |
| 83 | + if (next >= owned.length) { |
| 84 | + next = 0; |
| 85 | + } |
| 86 | + return next; |
| 87 | + }); |
| 88 | + }; |
| 89 | + const prev = () => { |
| 90 | + setCurrentIndex((val) => { |
| 91 | + let prev = val - 1; |
| 92 | + if (prev < 0) { |
| 93 | + prev = owned.length - 1; |
| 94 | + } |
| 95 | + return prev; |
| 96 | + }); |
| 97 | + }; |
| 98 | + |
| 99 | + return ( |
| 100 | + <Flex flexDir="column" w="100vw" h="100vh" px={10} pb={10}> |
| 101 | + <Flex |
| 102 | + as="header" |
| 103 | + flexShrink={0} |
| 104 | + py={4} |
| 105 | + justify="space-between" |
| 106 | + align="center" |
| 107 | + > |
| 108 | + <Heading as="h1">{metadata?.metadata?.name || ""}</Heading> |
| 109 | + <ConnectWallet /> |
| 110 | + </Flex> |
| 111 | + <Flex |
| 112 | + py={4} |
| 113 | + borderTop="1px solid rgba(0,0,0,.1)" |
| 114 | + as="main" |
| 115 | + flexGrow={1} |
| 116 | + justify="center" |
| 117 | + position="relative" |
| 118 | + overflow="hidden" |
| 119 | + > |
| 120 | + {owned.map((nft, idx) => ( |
| 121 | + <Stack |
| 122 | + key={nft.id} |
| 123 | + justify="center" |
| 124 | + align="center" |
| 125 | + w="100%" |
| 126 | + position="absolute" |
| 127 | + left={"calc(100% * " + idx + ")"} |
| 128 | + transform={`translateX(${currentIndex * -100}%)`} |
| 129 | + transition="transform .5s ease" |
| 130 | + > |
| 131 | + <AspectRatio ratio={1} w="80%"> |
| 132 | + <Box |
| 133 | + borderRadius="lg" |
| 134 | + overflow="hidden" |
| 135 | + bg="black" |
| 136 | + border="1px solid rgba(0, 0, 0, 0.1);" |
| 137 | + > |
| 138 | + <Image |
| 139 | + w="100%" |
| 140 | + h="100%" |
| 141 | + objectFit="contain" |
| 142 | + src={nft.image} |
| 143 | + alt={nft.name} |
| 144 | + /> |
| 145 | + </Box> |
| 146 | + </AspectRatio> |
| 147 | + <Heading fontWeight={500} fontSize="18px" size="sm" as="h3"> |
| 148 | + {nft.name} |
| 149 | + </Heading> |
| 150 | + </Stack> |
| 151 | + ))} |
| 152 | + <IconButton |
| 153 | + aria-label="next" |
| 154 | + icon={<ChevronRightIcon />} |
| 155 | + onClick={next} |
| 156 | + borderRadius="full" |
| 157 | + variant="ghost" |
| 158 | + position="absolute" |
| 159 | + top="50%" |
| 160 | + right="5px" |
| 161 | + transform="translateY(-50%)" |
| 162 | + /> |
| 163 | + <IconButton |
| 164 | + aria-label="previous" |
| 165 | + icon={<ChevronLeftIcon />} |
| 166 | + onClick={prev} |
| 167 | + borderRadius="full" |
| 168 | + variant="ghost" |
| 169 | + position="absolute" |
| 170 | + top="50%" |
| 171 | + left="5px" |
| 172 | + transform="translateY(-50%)" |
| 173 | + /> |
| 174 | + </Flex> |
| 175 | + <Flex as="footer" flexShrink={0} py={4} justify="flex-end"> |
| 176 | + <PoweredBy /> |
| 177 | + </Flex> |
| 178 | + </Flex> |
| 179 | + ); |
| 180 | +}; |
0 commit comments