mango-ui-v3/components/ConnectWalletButton.tsx

204 lines
7.0 KiB
TypeScript
Raw Normal View History

2022-03-22 02:18:58 -07:00
import React, {
Fragment,
useCallback,
useState,
useMemo,
useEffect,
} from 'react'
2022-02-22 19:36:05 -08:00
import { Menu, Transition } from '@headlessui/react'
2022-03-22 02:18:58 -07:00
import { useWallet, Wallet } from '@solana/wallet-adapter-react'
import { WalletReadyState } from '@solana/wallet-adapter-base'
import {
2021-07-06 21:34:21 -07:00
CurrencyDollarIcon,
DuplicateIcon,
LogoutIcon,
} from '@heroicons/react/outline'
2022-03-22 02:18:58 -07:00
import { notify } from 'utils/notifications'
import { abbreviateAddress, copyToClipboard } from 'utils'
import useMangoStore from 'stores/useMangoStore'
import { ProfileIcon, WalletIcon } from './icons'
2022-03-18 11:41:23 -07:00
import { useTranslation } from 'next-i18next'
2022-03-22 02:18:58 -07:00
import { WalletSelect } from 'components/WalletSelect'
import AccountsModal from './AccountsModal'
2022-03-29 19:46:21 -07:00
import uniqBy from 'lodash/uniqBy'
2022-03-22 02:18:58 -07:00
export const handleWalletConnect = (wallet: Wallet) => {
if (!wallet) {
return
}
if (wallet.readyState === WalletReadyState.NotDetected) {
window.open(wallet.adapter.url, '_blank')
} else {
wallet?.adapter?.connect().catch((e) => {
if (e.name.includes('WalletLoadError')) {
notify({
title: `${wallet.adapter.name} Error`,
type: 'error',
description: `Please install ${wallet.adapter.name} and then reload this page.`,
})
}
})
}
}
2022-03-22 02:18:58 -07:00
export const ConnectWalletButton: React.FC = () => {
const { connected, publicKey, wallet, wallets, select } = useWallet()
const { t } = useTranslation('common')
const pfp = useMangoStore((s) => s.wallet.pfp)
const set = useMangoStore((s) => s.set)
2022-03-22 02:18:58 -07:00
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
2021-07-06 21:34:21 -07:00
const [showAccountsModal, setShowAccountsModal] = useState(false)
2022-03-22 02:18:58 -07:00
const installedWallets = useMemo(() => {
const installed: Wallet[] = []
for (const wallet of wallets) {
if (wallet.readyState === WalletReadyState.Installed) {
installed.push(wallet)
}
}
return installed
}, [wallets])
const displayedWallets = useMemo(() => {
2022-03-24 07:39:24 -07:00
return uniqBy([...installedWallets, ...wallets], (w) => {
return w.adapter.name
})
}, [wallets, installedWallets])
2022-03-22 02:18:58 -07:00
const handleConnect = useCallback(() => {
2022-03-30 04:08:05 -07:00
if (wallet) {
handleWalletConnect(wallet)
}
2022-03-22 02:18:58 -07:00
}, [wallet])
2021-07-06 21:34:21 -07:00
const handleCloseAccounts = useCallback(() => {
setShowAccountsModal(false)
}, [])
2022-03-22 02:18:58 -07:00
const handleDisconnect = useCallback(() => {
wallet?.adapter?.disconnect()
set((state) => {
state.mangoAccounts = []
state.selectedMangoAccount.current = null
2022-03-28 16:43:42 -07:00
state.tradeHistory = {
spot: [],
perp: [],
parsed: [],
initialLoad: false,
}
2022-03-22 02:18:58 -07:00
})
notify({
type: 'info',
title: t('wallet-disconnected'),
})
}, [wallet, set, t])
useEffect(() => {
if (!wallet && displayedWallets?.length) {
select(displayedWallets[0].adapter.name)
}
}, [wallet, displayedWallets, select])
return (
<>
2022-03-22 02:18:58 -07:00
{connected && publicKey ? (
<Menu>
2022-02-22 19:36:05 -08:00
{({ open }) => (
<div className="relative" id="profile-menu-tip">
<Menu.Button className="flex h-10 w-10 items-center justify-center rounded-full bg-th-bkg-4 text-white hover:bg-th-bkg-4 hover:text-th-fgd-3 focus:outline-none">
2022-02-22 19:36:05 -08:00
{pfp?.isAvailable ? (
<img alt="" src={pfp.url} className="rounded-full" />
) : (
<ProfileIcon className="h-6 w-6" />
)}
</Menu.Button>
<Transition
appear={true}
show={open}
as={Fragment}
enter="transition-all ease-in duration-200"
enterFrom="opacity-0 transform scale-75"
enterTo="opacity-100 transform scale-100"
leave="transition ease-out duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<Menu.Items className="absolute right-0 z-20 mt-1 w-48 space-y-1.5 rounded-md bg-th-bkg-3 px-4 py-2.5">
2022-02-22 19:36:05 -08:00
<Menu.Item>
<button
className="flex w-full flex-row items-center rounded-none py-0.5 font-normal hover:cursor-pointer hover:text-th-primary focus:outline-none"
2022-02-22 19:36:05 -08:00
onClick={() => setShowAccountsModal(true)}
>
<CurrencyDollarIcon className="h-4 w-4" />
<div className="pl-2 text-left">{t('accounts')}</div>
</button>
</Menu.Item>
<Menu.Item>
<button
className="flex w-full flex-row items-center rounded-none py-0.5 font-normal hover:cursor-pointer hover:text-th-primary focus:outline-none"
2022-03-22 02:18:58 -07:00
onClick={() => copyToClipboard(publicKey)}
2022-02-22 19:36:05 -08:00
>
<DuplicateIcon className="h-4 w-4" />
<div className="pl-2 text-left">{t('copy-address')}</div>
</button>
</Menu.Item>
<Menu.Item>
<button
className="flex w-full flex-row items-center rounded-none py-0.5 font-normal hover:cursor-pointer hover:text-th-primary focus:outline-none"
2022-03-22 02:18:58 -07:00
onClick={handleDisconnect}
2022-02-22 19:36:05 -08:00
>
<LogoutIcon className="h-4 w-4" />
<div className="pl-2 text-left">
<div className="pb-0.5">{t('disconnect')}</div>
<div className="text-xs text-th-fgd-4">
2022-03-22 02:18:58 -07:00
{abbreviateAddress(publicKey)}
2022-02-22 19:36:05 -08:00
</div>
</div>
</button>
</Menu.Item>
</Menu.Items>
</Transition>
</div>
)}
</Menu>
) : (
2021-10-29 05:05:55 -07:00
<div
className="flex h-14 justify-between divide-x divide-th-bkg-3"
2021-11-04 05:25:11 -07:00
id="connect-wallet-tip"
2021-10-29 05:05:55 -07:00
>
<button
2022-03-22 02:18:58 -07:00
onClick={handleConnect}
disabled={!mangoGroup}
className="rounded-none bg-th-primary-dark text-th-bkg-1 hover:brightness-[1.1] focus:outline-none disabled:cursor-wait disabled:text-th-bkg-2"
>
<div className="default-transition flex h-full flex-row items-center justify-center px-3">
<WalletIcon className="mr-2 h-4 w-4 fill-current" />
2021-11-10 03:46:54 -08:00
<div className="text-left">
<div className="mb-0.5 whitespace-nowrap font-bold">
2022-02-17 01:44:28 -08:00
{t('connect')}
</div>
2022-03-22 02:18:58 -07:00
{wallet?.adapter?.name && (
<div className="text-xxs font-normal leading-3 tracking-wider text-th-bkg-2">
{wallet.adapter.name}
</div>
)}
</div>
</div>
</button>
<div className="relative">
2022-03-22 02:18:58 -07:00
<WalletSelect wallets={displayedWallets} />
</div>
</div>
)}
2022-03-22 02:18:58 -07:00
{showAccountsModal && (
<AccountsModal
onClose={handleCloseAccounts}
isOpen={showAccountsModal}
/>
2022-03-22 02:18:58 -07:00
)}
</>
)
}