diff --git a/apps/web/components/LocalAccountNFTs.tsx b/apps/web/components/LocalAccountNFTs.tsx index 7991779..70bed95 100644 --- a/apps/web/components/LocalAccountNFTs.tsx +++ b/apps/web/components/LocalAccountNFTs.tsx @@ -1,30 +1,12 @@ import useChain from '@/hooks/useChain' -import { OwnedNft } from 'alchemy-sdk' -import { useEffect, useState } from 'react' -import { getAlchemyNFT } from 'shared-config' +import { useListAccountNFTsQuery } from '@instate/kit' const AccountNFTs = ({ account }: { account?: string }) => { const { chainId } = useChain() - const [nfts, setNfts] = useState(null) - const [nftsLoading, setNftsLoading] = useState(false) - - const fetchNFTs = async () => { - if (!account) return - - setNftsLoading(true) - - const nfts = getAlchemyNFT(chainId) - - const { ownedNfts } = await nfts.getNftsForOwner(account) - - setNfts(ownedNfts) - - setNftsLoading(false) - } - - useEffect(() => { - fetchNFTs() - }, [account]) + const { data: nfts, isLoading } = useListAccountNFTsQuery({ + chainId, + account, + }) return (
@@ -43,7 +25,7 @@ const AccountNFTs = ({ account }: { account?: string }) => {
) }) - ) : nftsLoading ? ( + ) : isLoading ? (
diff --git a/packages/kit/index.ts b/packages/kit/index.ts index 631a6d0..e2eee66 100644 --- a/packages/kit/index.ts +++ b/packages/kit/index.ts @@ -4,3 +4,4 @@ export { ConnectButton } from "./src/ConnectButton"; export { InstateProvider } from "./rtk/store"; export { emojiAvatarForAddress } from "./utils/rainbow"; export * from "./rtk/generated"; +export { useListAccountNFTsQuery } from "./rtk/nft"; diff --git a/packages/kit/rtk/nft.tsx b/packages/kit/rtk/nft.tsx new file mode 100644 index 0000000..ac3b885 --- /dev/null +++ b/packages/kit/rtk/nft.tsx @@ -0,0 +1,22 @@ +import { createApi, fakeBaseQuery } from "@reduxjs/toolkit/query/react"; +import { getAlchemyNFT } from "shared-config"; + +export const nftAPI = createApi({ + reducerPath: "nftAPI", + baseQuery: fakeBaseQuery(), + endpoints: (builder) => ({ + listAccountNFTs: builder.query({ + queryFn: async ({ chainId, account }) => { + const nfts = getAlchemyNFT(chainId); + + const { ownedNfts } = await nfts.getNftsForOwner(account); + + return { + data: ownedNfts, + }; + }, + }), + }), +}); + +export const { useListAccountNFTsQuery } = nftAPI; diff --git a/packages/kit/rtk/store.tsx b/packages/kit/rtk/store.tsx index 52e3fd4..021722a 100644 --- a/packages/kit/rtk/store.tsx +++ b/packages/kit/rtk/store.tsx @@ -6,16 +6,20 @@ import { LocalAccountProvider } from "../src/LocalAccountButton"; import { RainbowKitProvider, darkTheme } from "@rainbow-me/rainbowkit"; import { chains } from "wagmi-config"; import { DaisyTheme } from "shared-config"; +import { nftAPI } from "./nft"; export const store = configureStore({ reducer: { // Add the generated reducer as a specific top-level slice [subgraphAPI.reducerPath]: subgraphAPI.reducer, + [nftAPI.reducerPath]: nftAPI.reducer, }, // Adding the api middleware enables caching, invalidation, polling, // and other useful features of `rtk-query`. middleware: (getDefaultMiddleware) => - getDefaultMiddleware().concat(subgraphAPI.middleware), + getDefaultMiddleware() + .concat(subgraphAPI.middleware) + .concat(nftAPI.middleware), }); export const InstateProvider = ({