import React, { Fragment, useState } from "react"; import Image from "next/image"; import { Dialog, Transition } from "@headlessui/react"; import { toast } from "react-toastify"; import { getInjectiveAddress } from "utils/address"; import { getExperimentalChainConfigBasedOnChainId } from "utils/experimental-chains"; import { ChainId } from "types"; import useWalletStore from "stores/useWalletStore"; 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( ChainId.Mainnet ); await window.keplr.experimentalSuggestChain(chainData); const key = await window.keplr.getKey(ChainId.Mainnet); 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;