import { useEffect, useState } from 'react' import styled from '@emotion/styled' import useMangoStore from '../stores/useMangoStore' import { Menu } from '@headlessui/react' import { DuplicateIcon, LogoutIcon } from '@heroicons/react/outline' import { ChevronUpIcon, ChevronDownIcon } from '@heroicons/react/solid' import { WALLET_PROVIDERS, DEFAULT_PROVIDER } from '../hooks/useWallet' import useLocalStorageState from '../hooks/useLocalStorageState' import { abbreviateAddress, copyToClipboard } from '../utils' import WalletSelect from './WalletSelect' import { WalletIcon } from './icons' const StyledWalletTypeLabel = styled.div` font-size: 0.65rem; ` const StyledWalletButtonWrapper = styled.div` width: 196px; ` const Code = styled.code` border: 1px solid hsla(0, 0%, 39.2%, 0.2); border-radius: 3px; background: hsla(0, 0%, 58.8%, 0.1); font-size: 0.75rem; ` const WALLET_OPTIONS = [ { name: 'Copy address', icon: }, { name: 'Disconnect', icon: }, ] const ConnectWalletButton = () => { const wallet = useMangoStore((s) => s.wallet.current) const connected = useMangoStore((s) => s.wallet.connected) const set = useMangoStore((s) => s.set) const [isCopied, setIsCopied] = useState(false) const [savedProviderUrl] = useLocalStorageState( 'walletProvider', DEFAULT_PROVIDER.url ) useEffect(() => { if (isCopied) { const timer = setTimeout(() => { setIsCopied(false) }, 2000) return () => clearTimeout(timer) } }, [isCopied]) const handleWalletMenu = (option) => { if (option === 'Copy address') { copyToClipboard(wallet.publicKey) setIsCopied(true) } else { wallet.disconnect() } } const handleWalletConect = () => { wallet.connect() set((state) => { state.selectedMarginAccount.initialLoad = true }) } return ( {connected && wallet?.publicKey ? ( {({ open }) => ( {isCopied ? 'Copied!' : abbreviateAddress(wallet.publicKey)} {open ? ( ) : ( )} {WALLET_OPTIONS.map(({ name, icon }) => ( handleWalletMenu(name)} > {icon} {name} ))} )} ) : ( Connect Wallet { WALLET_PROVIDERS.find((p) => p.url === savedProviderUrl) ?.name } )} ) } export default ConnectWalletButton
{isCopied ? 'Copied!' : abbreviateAddress(wallet.publicKey)}