import clsx from 'clsx' import { Conditional } from 'components/Conditional' import { ContractPageHeader } from 'components/ContractPageHeader' import { FormControl } from 'components/FormControl' import { AddressInput } from 'components/forms/FormInput' import { useInputState } from 'components/forms/FormInput.hooks' import { JsonPreview } from 'components/JsonPreview' import { LinkTabs } from 'components/LinkTabs' import { openEditionMinterLinkTabs } from 'components/LinkTabs.data' import { useContracts } from 'contexts/contracts' import type { QueryType } from 'contracts/openEditionMinter/messages/query' import { dispatchQuery, QUERY_LIST } from 'contracts/openEditionMinter/messages/query' import type { NextPage } from 'next' import { useRouter } from 'next/router' import { NextSeo } from 'next-seo' import { useEffect, useState } from 'react' import { toast } from 'react-hot-toast' import { useQuery } from 'react-query' import { withMetadata } from 'utils/layout' import { links } from 'utils/links' import { resolveAddress } from 'utils/resolveAddress' import { useWallet } from 'utils/wallet' const OpenEditionMinterQueryPage: NextPage = () => { const { openEditionMinter: contract } = useContracts() const wallet = useWallet() const contractState = useInputState({ id: 'contract-address', name: 'contract-address', title: 'Open Edition Minter Address', subtitle: 'Address of the Open Edition Minter contract', }) const contractAddress = contractState.value const addressState = useInputState({ id: 'address', name: 'address', title: 'Address', subtitle: 'Address of the user - defaults to current address', }) const address = addressState.value const [type, setType] = useState('config') const { data: response } = useQuery( [contractAddress, type, contract, wallet.address, address] as const, async ({ queryKey }) => { const [_contractAddress, _type, _contract, _wallet] = queryKey const messages = contract?.use(_contractAddress) const res = await resolveAddress(address, wallet).then(async (resolvedAddress) => { const result = await dispatchQuery({ address: resolvedAddress, messages, type, }) return result }) return res }, { placeholderData: null, onError: (error: any) => { toast.error(error.message, { style: { maxWidth: 'none' } }) }, enabled: Boolean(contractAddress && contract), }, ) const router = useRouter() useEffect(() => { if (contractAddress.length > 0) { void router.replace({ query: { contractAddress } }) } if (contractAddress.length === 0) { void router.replace({ query: {} }) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [contractAddress]) useEffect(() => { const initial = new URL(document.URL).searchParams.get('contractAddress') if (initial && initial.length > 0) contractState.onChange(initial) }, []) return (
) } export default withMetadata(OpenEditionMinterQueryPage, { center: false })