2022-07-26 21:40:17 -07:00
|
|
|
import { Menu, Transition } from '@headlessui/react'
|
2022-09-20 22:31:54 -07:00
|
|
|
import {
|
|
|
|
ArrowRightOnRectangleIcon,
|
2022-10-25 04:09:36 -07:00
|
|
|
CurrencyDollarIcon,
|
2022-09-20 22:31:54 -07:00
|
|
|
UserCircleIcon,
|
|
|
|
} from '@heroicons/react/20/solid'
|
2022-11-03 09:59:21 -07:00
|
|
|
import { useWallet, Wallet } from '@solana/wallet-adapter-react'
|
2022-07-26 21:40:17 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-09-20 18:03:59 -07:00
|
|
|
import { Fragment, useCallback, useEffect, useState } from 'react'
|
2022-09-12 08:53:57 -07:00
|
|
|
import mangoStore from '@store/mangoStore'
|
2022-07-26 21:40:17 -07:00
|
|
|
import { notify } from '../../utils/notifications'
|
2022-09-23 03:57:19 -07:00
|
|
|
import ProfileImage from '../profile/ProfileImage'
|
2022-07-26 21:40:17 -07:00
|
|
|
import { abbreviateAddress } from '../../utils/formatting'
|
2022-09-20 18:03:59 -07:00
|
|
|
import { PublicKey } from '@solana/web3.js'
|
|
|
|
import { useViewport } from 'hooks/useViewport'
|
|
|
|
import { breakpoints } from '../../utils/theme'
|
2022-09-20 22:31:54 -07:00
|
|
|
import EditProfileModal from '@components/modals/EditProfileModal'
|
2022-10-25 04:09:36 -07:00
|
|
|
import MangoAccountsListModal from '@components/modals/MangoAccountsListModal'
|
2022-11-03 09:59:21 -07:00
|
|
|
import { Wallet as AnchorWallet } from '@project-serum/anchor'
|
2022-07-26 21:40:17 -07:00
|
|
|
|
|
|
|
const ConnectedMenu = () => {
|
|
|
|
const { t } = useTranslation('common')
|
2022-11-15 20:12:51 -08:00
|
|
|
const { publicKey, disconnect, wallet } = useWallet()
|
|
|
|
const { width } = useViewport()
|
|
|
|
|
2022-09-20 22:31:54 -07:00
|
|
|
const [showEditProfileModal, setShowEditProfileModal] = useState(false)
|
2022-10-25 04:09:36 -07:00
|
|
|
const [showMangoAccountsModal, setShowMangoAccountsModal] = useState(false)
|
2022-11-15 20:12:51 -08:00
|
|
|
|
2022-07-26 21:40:17 -07:00
|
|
|
const set = mangoStore((s) => s.set)
|
2022-09-20 18:03:59 -07:00
|
|
|
const actions = mangoStore((s) => s.actions)
|
|
|
|
const profileDetails = mangoStore((s) => s.profile.details)
|
|
|
|
const loadProfileDetails = mangoStore((s) => s.profile.loadDetails)
|
2022-11-15 20:12:51 -08:00
|
|
|
|
2022-09-20 18:03:59 -07:00
|
|
|
const isMobile = width ? width < breakpoints.md : false
|
2022-11-15 20:12:51 -08:00
|
|
|
|
2022-11-03 09:59:21 -07:00
|
|
|
const onConnectFetchAccountData = async (wallet: Wallet) => {
|
|
|
|
if (!wallet) return
|
|
|
|
const actions = mangoStore.getState().actions
|
|
|
|
await actions.fetchMangoAccounts(wallet.adapter as unknown as AnchorWallet)
|
|
|
|
actions.fetchTourSettings(wallet.adapter.publicKey?.toString() as string)
|
|
|
|
actions.fetchWalletTokens(wallet.adapter as unknown as AnchorWallet)
|
|
|
|
}
|
2022-11-15 20:12:51 -08:00
|
|
|
|
2022-07-26 21:40:17 -07:00
|
|
|
const handleDisconnect = useCallback(() => {
|
|
|
|
set((state) => {
|
2022-10-28 03:42:23 -07:00
|
|
|
state.activityFeed.feed = []
|
2022-10-28 05:23:28 -07:00
|
|
|
state.activityFeed.initialLoad = false
|
2022-07-27 23:35:18 -07:00
|
|
|
state.mangoAccount.current = undefined
|
2022-10-07 04:47:15 -07:00
|
|
|
state.mangoAccounts = []
|
2022-10-02 21:38:49 -07:00
|
|
|
state.mangoAccount.openOrders = {}
|
2022-10-06 16:06:49 -07:00
|
|
|
state.mangoAccount.stats.interestTotals = { data: [], loading: false }
|
|
|
|
state.mangoAccount.stats.performance = { data: [], loading: false }
|
2022-07-26 21:40:17 -07:00
|
|
|
})
|
2022-08-19 23:06:00 -07:00
|
|
|
disconnect()
|
2022-08-27 01:33:21 -07:00
|
|
|
wallet?.adapter.disconnect()
|
2022-07-26 21:40:17 -07:00
|
|
|
notify({
|
|
|
|
type: 'info',
|
|
|
|
title: t('wallet-disconnected'),
|
|
|
|
})
|
2022-08-19 23:06:00 -07:00
|
|
|
}, [set, t, disconnect])
|
2022-07-26 21:40:17 -07:00
|
|
|
|
2022-09-20 18:03:59 -07:00
|
|
|
useEffect(() => {
|
2022-11-03 09:59:21 -07:00
|
|
|
const handleGetWalletMangoData = async () => {
|
|
|
|
const actions = mangoStore.getState().actions
|
|
|
|
await actions.connectMangoClientWithWallet(wallet!)
|
|
|
|
await onConnectFetchAccountData(wallet!)
|
|
|
|
}
|
2022-11-15 20:12:51 -08:00
|
|
|
|
2022-09-20 18:03:59 -07:00
|
|
|
if (publicKey) {
|
|
|
|
actions.fetchProfileDetails(publicKey.toString())
|
2022-11-03 09:59:21 -07:00
|
|
|
handleGetWalletMangoData()
|
2022-09-20 18:03:59 -07:00
|
|
|
}
|
2022-11-15 20:12:51 -08:00
|
|
|
}, [publicKey, actions, wallet])
|
2022-09-20 18:03:59 -07:00
|
|
|
|
|
|
|
const { profile_name, wallet_pk } = profileDetails
|
|
|
|
|
2022-07-26 21:40:17 -07:00
|
|
|
return (
|
2022-08-01 22:32:21 -07:00
|
|
|
<>
|
|
|
|
<Menu>
|
|
|
|
{({ open }) => (
|
|
|
|
<div className="relative">
|
|
|
|
<Menu.Button
|
2022-09-21 21:25:24 -07:00
|
|
|
className={`default-transition h-16 ${
|
2022-09-20 23:13:14 -07:00
|
|
|
!isMobile ? 'w-48 border-l border-th-bkg-3 px-3' : ''
|
2022-09-21 21:25:24 -07:00
|
|
|
} hover:bg-th-bkg-2 focus:outline-none`}
|
2022-08-01 22:32:21 -07:00
|
|
|
>
|
2022-09-21 21:25:24 -07:00
|
|
|
<div className="flex items-center" id="account-step-one">
|
|
|
|
<ProfileImage
|
|
|
|
imageSize="40"
|
|
|
|
placeholderSize="24"
|
|
|
|
isOwnerProfile
|
|
|
|
/>
|
|
|
|
{!loadProfileDetails && !isMobile ? (
|
|
|
|
<div className="ml-2.5 w-32 text-left">
|
2022-10-05 17:04:16 -07:00
|
|
|
<p className="font-mono text-xs text-th-fgd-3">
|
2022-09-21 21:25:24 -07:00
|
|
|
{wallet_pk
|
|
|
|
? abbreviateAddress(new PublicKey(wallet_pk))
|
|
|
|
: ''}
|
|
|
|
</p>
|
|
|
|
<p className="truncate pr-2 text-sm font-bold capitalize text-th-fgd-1">
|
|
|
|
{profile_name}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2022-08-01 22:32:21 -07:00
|
|
|
</Menu.Button>
|
|
|
|
<Transition
|
|
|
|
appear={true}
|
|
|
|
show={open}
|
|
|
|
as={Fragment}
|
2022-08-16 15:58:03 -07:00
|
|
|
enter="transition ease-in duration-200"
|
|
|
|
enterFrom="opacity-0 scale-75"
|
|
|
|
enterTo="opacity-100 scale-100"
|
2022-08-01 22:32:21 -07:00
|
|
|
leave="transition ease-out duration-200"
|
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
2022-10-05 17:04:16 -07:00
|
|
|
<Menu.Items className="absolute right-0 top-[61px] z-20 mt-1 w-48 space-y-1.5 rounded-md rounded-t-none bg-th-bkg-2 px-4 py-2.5 md:rounded-r-none">
|
2022-09-20 22:31:54 -07: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"
|
|
|
|
onClick={() => setShowEditProfileModal(true)}
|
|
|
|
>
|
|
|
|
<UserCircleIcon className="h-4 w-4" />
|
|
|
|
<div className="pl-2 text-left">
|
|
|
|
{t('profile:edit-profile')}
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
</Menu.Item>
|
2022-10-25 04:09:36 -07:00
|
|
|
{isMobile ? (
|
|
|
|
<Menu.Item>
|
2022-07-26 21:40:17 -07:00
|
|
|
<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-10-25 04:09:36 -07:00
|
|
|
onClick={() => setShowMangoAccountsModal(true)}
|
2022-07-26 21:40:17 -07:00
|
|
|
>
|
|
|
|
<CurrencyDollarIcon className="h-4 w-4" />
|
|
|
|
<div className="pl-2 text-left">{t('accounts')}</div>
|
|
|
|
</button>
|
2022-10-25 04:09:36 -07:00
|
|
|
</Menu.Item>
|
|
|
|
) : null}
|
2022-08-16 21:22:55 -07:00
|
|
|
{/* <Menu.Item>
|
2022-08-01 22:32:21 -07:00
|
|
|
<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"
|
|
|
|
onClick={() => setShowProfileImageModal(true)}
|
|
|
|
>
|
|
|
|
<ProfileIcon className="h-4 w-4" />
|
|
|
|
<div className="pl-2 text-left">
|
|
|
|
{t('edit-profile-image')}
|
|
|
|
</div>
|
|
|
|
</button>
|
2022-08-16 21:22:55 -07:00
|
|
|
</Menu.Item> */}
|
2022-08-01 22:32:21 -07:00
|
|
|
<Menu.Item>
|
|
|
|
<button
|
|
|
|
className="flex w-full flex-row items-center rounded-none py-0.5 font-normal hover:cursor-pointer focus:outline-none md:hover:text-th-primary"
|
|
|
|
onClick={handleDisconnect}
|
|
|
|
>
|
2022-09-06 21:36:35 -07:00
|
|
|
<ArrowRightOnRectangleIcon className="h-4 w-4" />
|
2022-08-01 22:32:21 -07:00
|
|
|
<div className="pl-2 text-left">
|
|
|
|
<div className="pb-0.5">{t('disconnect')}</div>
|
|
|
|
{publicKey ? (
|
2022-10-05 17:04:16 -07:00
|
|
|
<div className="font-mono text-xs text-th-fgd-4">
|
2022-08-01 22:32:21 -07:00
|
|
|
{abbreviateAddress(publicKey)}
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
</Menu.Item>
|
|
|
|
</Menu.Items>
|
|
|
|
</Transition>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Menu>
|
2022-09-20 22:31:54 -07:00
|
|
|
{showEditProfileModal ? (
|
|
|
|
<EditProfileModal
|
|
|
|
isOpen={showEditProfileModal}
|
|
|
|
onClose={() => setShowEditProfileModal(false)}
|
2022-08-01 22:32:21 -07:00
|
|
|
/>
|
|
|
|
) : null}
|
2022-10-25 04:09:36 -07:00
|
|
|
|
|
|
|
{showMangoAccountsModal ? (
|
|
|
|
<MangoAccountsListModal
|
|
|
|
isOpen={showMangoAccountsModal}
|
|
|
|
onClose={() => setShowMangoAccountsModal(false)}
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-08-01 22:32:21 -07:00
|
|
|
</>
|
2022-07-26 21:40:17 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ConnectedMenu
|