2021-07-06 21:34:21 -07:00
|
|
|
import { useCallback, useState } from 'react'
|
2021-04-18 18:58:16 -07:00
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
2021-05-17 22:33:04 -07:00
|
|
|
import { Menu } from '@headlessui/react'
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
import {
|
2021-07-06 21:34:21 -07:00
|
|
|
CurrencyDollarIcon,
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
DuplicateIcon,
|
|
|
|
LogoutIcon,
|
|
|
|
} from '@heroicons/react/outline'
|
2021-08-04 14:47:05 -07:00
|
|
|
import {
|
|
|
|
WALLET_PROVIDERS,
|
|
|
|
DEFAULT_PROVIDER,
|
|
|
|
PROVIDER_LOCAL_STORAGE_KEY,
|
|
|
|
} from '../hooks/useWallet'
|
2021-04-18 18:58:16 -07:00
|
|
|
import useLocalStorageState from '../hooks/useLocalStorageState'
|
2021-05-17 22:33:04 -07:00
|
|
|
import { abbreviateAddress, copyToClipboard } from '../utils'
|
2021-04-18 18:58:16 -07:00
|
|
|
import WalletSelect from './WalletSelect'
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
import { WalletIcon, ProfileIcon } from './icons'
|
2021-07-06 21:34:21 -07:00
|
|
|
import AccountsModal from './AccountsModal'
|
2021-08-04 14:47:05 -07:00
|
|
|
import { useEffect } from 'react'
|
2021-10-20 05:42:40 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2021-04-18 18:58:16 -07:00
|
|
|
|
|
|
|
const ConnectWalletButton = () => {
|
2021-10-20 05:42:40 -07:00
|
|
|
const { t } = useTranslation('common')
|
2021-04-18 18:58:16 -07:00
|
|
|
const wallet = useMangoStore((s) => s.wallet.current)
|
2021-05-17 22:33:04 -07:00
|
|
|
const connected = useMangoStore((s) => s.wallet.connected)
|
2021-05-04 16:19:48 -07:00
|
|
|
const set = useMangoStore((s) => s.set)
|
2021-07-06 21:34:21 -07:00
|
|
|
const [showAccountsModal, setShowAccountsModal] = useState(false)
|
2021-08-04 14:47:05 -07:00
|
|
|
const [selectedWallet, setSelectedWallet] = useState(DEFAULT_PROVIDER.url)
|
2021-04-18 18:58:16 -07:00
|
|
|
const [savedProviderUrl] = useLocalStorageState(
|
2021-08-04 14:47:05 -07:00
|
|
|
PROVIDER_LOCAL_STORAGE_KEY,
|
2021-04-18 18:58:16 -07:00
|
|
|
DEFAULT_PROVIDER.url
|
|
|
|
)
|
|
|
|
|
2021-08-04 14:47:05 -07:00
|
|
|
// update in useEffect to prevent SRR error from next.js
|
|
|
|
useEffect(() => {
|
|
|
|
setSelectedWallet(savedProviderUrl)
|
|
|
|
}, [savedProviderUrl])
|
|
|
|
|
2021-05-04 16:19:48 -07:00
|
|
|
const handleWalletConect = () => {
|
|
|
|
wallet.connect()
|
|
|
|
set((state) => {
|
2021-06-23 08:32:33 -07:00
|
|
|
state.selectedMangoAccount.initialLoad = true
|
2021-05-04 16:19:48 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-06 21:34:21 -07:00
|
|
|
const handleCloseAccounts = useCallback(() => {
|
|
|
|
setShowAccountsModal(false)
|
|
|
|
}, [])
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
|
2021-04-18 18:58:16 -07:00
|
|
|
return (
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
<>
|
2021-05-17 22:33:04 -07:00
|
|
|
{connected && wallet?.publicKey ? (
|
|
|
|
<Menu>
|
2021-09-01 07:30:34 -07:00
|
|
|
<div className="relative">
|
|
|
|
<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">
|
2021-07-29 06:19:32 -07:00
|
|
|
<ProfileIcon className="h-6 w-6" />
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
</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>
|
2021-06-17 18:10:47 -07:00
|
|
|
<button
|
2021-10-09 03:05:45 -07:00
|
|
|
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"
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
onClick={() => setShowAccountsModal(true)}
|
|
|
|
>
|
|
|
|
<CurrencyDollarIcon className="h-4 w-4" />
|
2021-10-20 05:42:40 -07:00
|
|
|
<div className="pl-2 text-left">{t('accounts')}</div>
|
2021-06-17 18:10:47 -07:00
|
|
|
</button>
|
2021-07-06 21:34:21 -07:00
|
|
|
</Menu.Item>
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -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={() => copyToClipboard(wallet?.publicKey)}
|
|
|
|
>
|
|
|
|
<DuplicateIcon className="h-4 w-4" />
|
2021-10-20 05:42:40 -07:00
|
|
|
<div className="pl-2 text-left">{t('copy-address')}</div>
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
</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"
|
|
|
|
onClick={() => wallet.disconnect()}
|
|
|
|
>
|
|
|
|
<LogoutIcon className="h-4 w-4" />
|
|
|
|
<div className="pl-2 text-left">
|
2021-10-20 05:42:40 -07:00
|
|
|
<div className="pb-0.5">{t('disconnect')}</div>
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
<div className="text-th-fgd-4 text-xs">
|
|
|
|
{abbreviateAddress(wallet?.publicKey)}
|
|
|
|
</div>
|
2021-05-17 22:33:04 -07:00
|
|
|
</div>
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
</button>
|
|
|
|
</Menu.Item>
|
|
|
|
</Menu.Items>
|
|
|
|
</div>
|
2021-05-17 22:33:04 -07:00
|
|
|
</Menu>
|
|
|
|
) : (
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
<div className="bg-th-bkg-1 h-14 flex divide-x divide-th-bkg-3 justify-between">
|
2021-05-17 22:33:04 -07:00
|
|
|
<button
|
|
|
|
onClick={handleWalletConect}
|
|
|
|
disabled={!wallet}
|
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"
|
2021-05-17 22:33:04 -07:00
|
|
|
>
|
2021-05-18 03:57:10 -07:00
|
|
|
<div className="flex flex-row items-center px-3 justify-center h-full default-transition hover:text-th-fgd-1">
|
2021-05-17 22:33:04 -07:00
|
|
|
<WalletIcon className="w-4 h-4 mr-2 fill-current" />
|
|
|
|
<div>
|
2021-10-20 05:42:40 -07:00
|
|
|
<div className="mb-0.5 whitespace-nowrap">{t('connect')}</div>
|
2021-09-03 12:02:51 -07:00
|
|
|
<div className="font-normal text-th-fgd-3 text-left leading-3 tracking-wider text-xxs">
|
2021-08-04 14:47:05 -07:00
|
|
|
{WALLET_PROVIDERS.find((p) => p.url === selectedWallet)?.name}
|
2021-09-03 12:02:51 -07:00
|
|
|
</div>
|
2021-05-17 22:33:04 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</button>
|
2021-09-01 07:30:34 -07:00
|
|
|
<div className="relative">
|
2021-05-17 22:33:04 -07:00
|
|
|
<WalletSelect isPrimary />
|
2021-04-18 18:58:16 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-05-17 22:33:04 -07:00
|
|
|
)}
|
2021-07-06 21:34:21 -07:00
|
|
|
{showAccountsModal ? (
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
<AccountsModal
|
|
|
|
onClose={handleCloseAccounts}
|
|
|
|
isOpen={showAccountsModal}
|
|
|
|
/>
|
2021-07-06 21:34:21 -07:00
|
|
|
) : null}
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
</>
|
2021-04-18 18:58:16 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ConnectWalletButton
|