import React, { Fragment, useState } from 'react' import Image from 'next/image' import { Dialog, Transition } from '@headlessui/react' import { toast } from 'react-toastify' import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' import { Coin } from '@cosmjs/stargate' import { getInjectiveAddress } from 'utils/address' import { getExperimentalChainConfigBasedOnChainId } from 'utils/experimental-chains' import { ChainId } from 'types' import useWalletStore from 'stores/useWalletStore' import { chain } from 'utils/chains' type Props = { isOpen: boolean onClose: () => void } const ConnectModal = ({ isOpen, onClose }: Props) => { const [isLoading, setIsLoading] = useState(false) const actions = useWalletStore((state) => state.actions) const metamaskInstalled = useWalletStore((state) => state.metamaskInstalled) const isKeplrInstalled = typeof window !== 'undefined' && window.keplr const handleConnectSuccess = () => { onClose() // defering update on loading state to avoid updating before close animation is finished setTimeout(() => { setIsLoading(false) }, 500) } const handleConnectKeplr = async () => { if (!window.keplr) { toast.error('You need Keplr extension installed') return } setIsLoading(true) try { const chainData = getExperimentalChainConfigBasedOnChainId(chain.chainId) if (chainData) { await window.keplr.experimentalSuggestChain(chainData) } const key = await window.keplr.getKey(chain.chainId) actions.setAddress(key.bech32Address) handleConnectSuccess() } catch (e) { // TODO: handle exception console.log(e) setIsLoading(false) } } const handleConnectMetamask = async () => { if (!metamaskInstalled) { toast.error('You need Metamask extension installed') return } setIsLoading(true) try { // TODO: missing type definitions const addresses = await (window.ethereum as any).request({ method: 'eth_requestAccounts', }) const [address] = addresses actions.setAddress(getInjectiveAddress(address)) handleConnectSuccess() } catch (e) { // TODO: handle exception console.log(e) setIsLoading(false) } } return (
Connect your wallet {isLoading ? (
Loading...
) : (
)}
) } export default ConnectModal