generated from NomicFoundation/hardhat-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add collection page and necessary hooks
- Loading branch information
Showing
9 changed files
with
223 additions
and
21 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useQuery } from "@tanstack/react-query" | ||
import { MoralisApiListResponse, MoralisNFT } from "../types/Token" | ||
|
||
interface Props { | ||
tokenAddress: string | ||
chainId: number | ||
} | ||
|
||
if (!process.env.REACT_APP_PROXY_URL) { | ||
throw new Error("REACT_APP_PROXY_URL not set") | ||
} | ||
|
||
const useCollection = ({ tokenAddress, chainId }: Props) => { | ||
return useQuery({ | ||
queryKey: ["collection", chainId, tokenAddress], | ||
queryFn: async () => { | ||
if (!chainId || !tokenAddress) throw new Error("No chainId or token") | ||
|
||
// get collection metadata | ||
const nftRes = await fetch( | ||
`${process.env.REACT_APP_PROXY_URL}/${chainId}/moralis/nft/${tokenAddress}` | ||
) | ||
if (!nftRes.ok) { | ||
throw new Error("NFT request failed") | ||
} | ||
const collection = (await nftRes.json()) as MoralisApiListResponse | ||
return collection.result as MoralisNFT[] | ||
}, | ||
enabled: !!chainId && !!tokenAddress, | ||
}) | ||
} | ||
|
||
export default useCollection |
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,34 @@ | ||
import { useQuery } from "@tanstack/react-query" | ||
import { MoralisCollectionMetadata } from "../types/Token" | ||
|
||
interface Props { | ||
tokenAddress: string | ||
chainId: number | ||
} | ||
|
||
if (!process.env.REACT_APP_PROXY_URL) { | ||
throw new Error("REACT_APP_PROXY_URL not set") | ||
} | ||
|
||
const useCollectionMetadata = ({ tokenAddress, chainId }: Props) => { | ||
return useQuery({ | ||
queryKey: ["collectionMetadata", chainId, tokenAddress], | ||
queryFn: async () => { | ||
if (!chainId || !tokenAddress) throw new Error("No chainId or token") | ||
|
||
// get collection metadata | ||
const nftRes = await fetch( | ||
`${process.env.REACT_APP_PROXY_URL}/${chainId}/moralis/nft/${tokenAddress}/metadata` | ||
) | ||
if (!nftRes.ok) { | ||
throw new Error("NFT request failed") | ||
} | ||
const collectionMetadata = | ||
(await nftRes.json()) as MoralisCollectionMetadata | ||
return collectionMetadata | ||
}, | ||
enabled: !!chainId && !!tokenAddress, | ||
}) | ||
} | ||
|
||
export default useCollectionMetadata |
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
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,35 @@ | ||
.container { | ||
width: 100%; | ||
background: var(--box-bg); | ||
border-radius: 20px; | ||
padding: 20px; | ||
} | ||
|
||
.accountHeader { | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
gap: 10px; | ||
margin-bottom: 20px; | ||
background-color: var(--box-bg); | ||
border-radius: 10px; | ||
padding: 10px 20px; | ||
width: 100%; | ||
} | ||
|
||
.account { | ||
display: flex; | ||
align-items: center; | ||
gap: 10px; | ||
} | ||
.title { | ||
font-size: 1.5rem; | ||
} | ||
.accountHeader h1 { | ||
font-size: 1rem; | ||
} | ||
|
||
.blockie { | ||
width: 25px; | ||
aspect-ratio: 1/1; | ||
} |
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,57 @@ | ||
import React from "react" | ||
import { useParams } from "react-router-dom" | ||
|
||
import Layout from "../../components/Layout" | ||
import { CollectionNftGrid } from "../../components/NFTGrid" | ||
import classes from "./Collection.module.css" | ||
import { getAddress } from "viem" | ||
import Blockie from "../../components/Blockie" | ||
import useCollectionMetadata from "../../hooks/useCollectionMetadata" | ||
import { useChainId } from "wagmi" | ||
|
||
const Collection: React.FC = () => { | ||
const { address } = useParams() | ||
const chainId = useChainId() | ||
let validAddress = "" | ||
try { | ||
validAddress = getAddress(address || "") | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
const { data, isLoading, error } = useCollectionMetadata({ | ||
tokenAddress: validAddress, | ||
chainId, | ||
}) | ||
|
||
if (validAddress) { | ||
return ( | ||
<Layout> | ||
<div className={classes.container}> | ||
<div className={classes.accountHeader}> | ||
<div className={classes.title}> | ||
{data && !isLoading ? data.name : "Collection"} | ||
</div> | ||
<div className={classes.account}> | ||
<div className={classes.blockie}> | ||
<Blockie address={validAddress} /> | ||
</div> | ||
<h1> | ||
<code>{validAddress}</code> | ||
</h1> | ||
</div> | ||
</div> | ||
<CollectionNftGrid address={validAddress} /> | ||
</div> | ||
</Layout> | ||
) | ||
} | ||
|
||
return ( | ||
<Layout> | ||
<h1> | ||
<code>{address}</code> is not a valid address | ||
</h1> | ||
</Layout> | ||
) | ||
} | ||
export default Collection |
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