|
| 1 | +import { useQuery } from "@tanstack/react-query"; |
| 2 | +import { fetchPublishedContractsFromDeploy } from "components/contract-components/fetchPublishedContractsFromDeploy"; |
1 | 3 | import { usePublishedContractsFromDeploy } from "components/contract-components/hooks"; |
2 | 4 | import { THIRDWEB_DEPLOYER_ADDRESS } from "constants/addresses"; |
3 | 5 | import { useMemo } from "react"; |
4 | | -import type { ThirdwebContract } from "thirdweb"; |
| 6 | +import { type ThirdwebContract, getContract } from "thirdweb"; |
5 | 7 | import { useActiveAccount } from "thirdweb/react"; |
6 | 8 |
|
| 9 | +export async function getAllModuleContractInfo( |
| 10 | + activeAccountAddress: string, |
| 11 | + installedModules: string[], |
| 12 | + contract: ThirdwebContract, |
| 13 | +) { |
| 14 | + if (!activeAccountAddress) { |
| 15 | + return []; |
| 16 | + } |
| 17 | + |
| 18 | + return Promise.all( |
| 19 | + installedModules.map(async (moduleAddress) => { |
| 20 | + const publishedContractsFromDeploy = |
| 21 | + await fetchPublishedContractsFromDeploy({ |
| 22 | + contract: getContract({ |
| 23 | + address: moduleAddress, |
| 24 | + chain: contract.chain, |
| 25 | + client: contract.client, |
| 26 | + }), |
| 27 | + client: contract.client, |
| 28 | + }); |
| 29 | + |
| 30 | + const reversedPublishedContractsFromDeploy = [ |
| 31 | + ...(publishedContractsFromDeploy || []), |
| 32 | + ].reverse(); |
| 33 | + |
| 34 | + const publishedContractToShow = |
| 35 | + reversedPublishedContractsFromDeploy.find( |
| 36 | + (publishedContract) => |
| 37 | + publishedContract.publisher === activeAccountAddress, |
| 38 | + ) || |
| 39 | + reversedPublishedContractsFromDeploy.find( |
| 40 | + (publishedContract) => |
| 41 | + publishedContract.publisher === THIRDWEB_DEPLOYER_ADDRESS, |
| 42 | + ) || |
| 43 | + reversedPublishedContractsFromDeploy[ |
| 44 | + reversedPublishedContractsFromDeploy.length - 1 |
| 45 | + ] || |
| 46 | + undefined; |
| 47 | + |
| 48 | + return publishedContractToShow as |
| 49 | + | typeof publishedContractToShow |
| 50 | + | undefined; |
| 51 | + }), |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +export function useAllModuleContractInfo( |
| 56 | + installedModules: string[], |
| 57 | + contract: ThirdwebContract, |
| 58 | +) { |
| 59 | + const address = useActiveAccount()?.address; |
| 60 | + |
| 61 | + return useQuery({ |
| 62 | + queryKey: ["all-module-contract-info", installedModules, contract, address], |
| 63 | + queryFn: () => |
| 64 | + getAllModuleContractInfo(address || "", installedModules || [], contract), |
| 65 | + enabled: !!address && !!contract, |
| 66 | + }); |
| 67 | +} |
| 68 | + |
7 | 69 | export function useModuleContractInfo(contract: ThirdwebContract) { |
8 | 70 | const address = useActiveAccount()?.address; |
9 | 71 |
|
|
0 commit comments