mango-ui-v3/components/ConnectWalletButton.tsx

133 lines
5.2 KiB
TypeScript
Raw Normal View History

2021-12-03 08:03:51 -08:00
import { useCallback, useState } from 'react'
import useMangoStore from '../stores/useMangoStore'
import { Menu } from '@headlessui/react'
import {
2021-07-06 21:34:21 -07:00
CurrencyDollarIcon,
DuplicateIcon,
LogoutIcon,
} from '@heroicons/react/outline'
import { PROVIDER_LOCAL_STORAGE_KEY } from '../hooks/useWallet'
import useLocalStorageState from '../hooks/useLocalStorageState'
import { abbreviateAddress, copyToClipboard } from '../utils'
import WalletSelect from './WalletSelect'
2021-12-03 08:03:51 -08:00
import { WalletIcon, ProfileIcon } from './icons'
2021-07-06 21:34:21 -07:00
import AccountsModal from './AccountsModal'
import { useEffect } from 'react'
import { useTranslation } from 'next-i18next'
import { DEFAULT_PROVIDER, WALLET_PROVIDERS } from '../utils/wallet-adapters'
const ConnectWalletButton = () => {
const { t } = useTranslation('common')
const wallet = useMangoStore((s) => s.wallet.current)
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const pfp = useMangoStore((s) => s.wallet.pfp)
const connected = useMangoStore((s) => s.wallet.connected)
const set = useMangoStore((s) => s.set)
2021-07-06 21:34:21 -07:00
const [showAccountsModal, setShowAccountsModal] = useState(false)
const [selectedWallet, setSelectedWallet] = useState(DEFAULT_PROVIDER.url)
const [savedProviderUrl] = useLocalStorageState(
PROVIDER_LOCAL_STORAGE_KEY,
DEFAULT_PROVIDER.url
)
2021-12-03 08:03:51 -08:00
// update in useEffect to prevent SRR error from next.js
useEffect(() => {
setSelectedWallet(savedProviderUrl)
}, [savedProviderUrl])
2021-12-03 08:03:51 -08:00
const handleWalletConect = () => {
wallet.connect()
set((state) => {
2021-06-23 08:32:33 -07:00
state.selectedMangoAccount.initialLoad = true
})
}
2021-07-06 21:34:21 -07:00
const handleCloseAccounts = useCallback(() => {
setShowAccountsModal(false)
}, [])
return (
<>
{connected && wallet?.publicKey ? (
<Menu>
2021-11-04 05:25:11 -07:00
<div className="relative" id="profile-menu-tip">
2021-12-03 08:03:51 -08:00
<Menu.Button className="bg-th-bkg-4 flex items-center justify-center rounded-full w-10 h-10 text-white focus:outline-none hover:bg-th-bkg-4 hover:text-th-fgd-3">
{pfp?.isAvailable ? (
<img alt="" src={pfp.url} className="rounded-full" />
) : (
<ProfileIcon className="h-6 w-6" />
)}
</Menu.Button>
<Menu.Items className="bg-th-bkg-1 mt-2 p-1 absolute right-0 shadow-lg outline-none rounded-md w-48 z-20">
2021-07-06 21:34:21 -07:00
<Menu.Item>
<button
className="flex flex-row font-normal items-center rounded-none w-full p-2 hover:bg-th-bkg-2 hover:cursor-pointer focus:outline-none"
onClick={() => setShowAccountsModal(true)}
>
<CurrencyDollarIcon className="h-4 w-4" />
<div className="pl-2 text-left">{t('accounts')}</div>
</button>
2021-07-06 21:34:21 -07:00
</Menu.Item>
<Menu.Item>
<button
className="flex flex-row font-normal items-center rounded-none w-full p-2 hover:bg-th-bkg-2 hover:cursor-pointer focus:outline-none"
onClick={() => copyToClipboard(wallet?.publicKey)}
>
<DuplicateIcon className="h-4 w-4" />
<div className="pl-2 text-left">{t('copy-address')}</div>
</button>
</Menu.Item>
<Menu.Item>
<button
className="flex flex-row font-normal items-center rounded-none w-full p-2 hover:bg-th-bkg-2 hover:cursor-pointer focus:outline-none"
2021-12-03 08:03:51 -08:00
onClick={() => wallet.disconnect()}
>
<LogoutIcon className="h-4 w-4" />
<div className="pl-2 text-left">
<div className="pb-0.5">{t('disconnect')}</div>
<div className="text-th-fgd-4 text-xs">
{abbreviateAddress(wallet?.publicKey)}
</div>
</div>
</button>
</Menu.Item>
</Menu.Items>
</div>
</Menu>
) : (
2021-10-29 05:05:55 -07:00
<div
2022-02-04 17:06:20 -08:00
className="bg-th-bkg-4 h-14 flex divide-x divide-th-bkg-3 justify-between"
2021-11-04 05:25:11 -07:00
id="connect-wallet-tip"
2021-10-29 05:05:55 -07:00
>
<button
2021-12-03 08:03:51 -08:00
onClick={handleWalletConect}
disabled={!wallet || !mangoGroup}
2021-09-01 07:30:34 -07:00
className="rounded-none text-th-primary hover:bg-th-bkg-4 focus:outline-none disabled:text-th-fgd-4 disabled:cursor-wait"
>
<div className="flex flex-row items-center px-3 justify-center h-full default-transition hover:text-th-fgd-1">
<WalletIcon className="w-4 h-4 mr-2 fill-current" />
2021-11-10 03:46:54 -08:00
<div className="text-left">
<div className="mb-0.5 whitespace-nowrap">{t('connect')}</div>
2021-11-10 03:46:54 -08:00
<div className="font-normal text-th-fgd-3 leading-3 tracking-wider text-xxs">
{WALLET_PROVIDERS.find((p) => p.url === selectedWallet)?.name}
</div>
</div>
</div>
</button>
2021-09-01 07:30:34 -07:00
<div className="relative">
<WalletSelect isPrimary />
</div>
</div>
)}
2021-07-06 21:34:21 -07:00
{showAccountsModal ? (
<AccountsModal
onClose={handleCloseAccounts}
isOpen={showAccountsModal}
/>
2021-07-06 21:34:21 -07:00
) : null}
</>
)
}
export default ConnectWalletButton