mango-v4-ui/components/wallet/ConnectedMenu.tsx

193 lines
7.4 KiB
TypeScript
Raw Normal View History

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'
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 { 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-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)
2023-01-03 14:20:00 -08:00
const actions = mangoStore.getState().actions
// const profileDetails = mangoStore((s) => s.profile.details)
2022-09-20 18:03:59 -07:00
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
const onConnectFetchAccountData = async (wallet: Wallet) => {
if (!wallet.adapter.publicKey) return
await actions.fetchMangoAccounts(wallet.adapter.publicKey)
actions.fetchTourSettings(wallet.adapter.publicKey?.toString() as string)
actions.fetchWalletTokens(wallet.adapter.publicKey)
2023-01-06 16:26:06 -08:00
actions.fetchTradeHistory()
}
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-07-27 23:35:18 -07:00
state.mangoAccount.current = undefined
2022-10-07 04:47:15 -07:00
state.mangoAccounts = []
state.mangoAccount.openOrders = {}
2023-01-15 21:13:34 -08:00
state.mangoAccount.interestTotals = { data: [], loading: false }
state.mangoAccount.performance = {
data: [],
loading: false,
initialLoad: 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-19 11:20:36 -08:00
const handleGetWalletMangoData = async (wallet: Wallet) => {
const actions = mangoStore.getState().actions
2022-11-19 11:20:36 -08:00
await actions.connectMangoClientWithWallet(wallet)
await onConnectFetchAccountData(wallet)
}
2022-11-15 20:12:51 -08:00
2022-11-19 11:20:36 -08:00
if (publicKey && wallet) {
2022-09-20 18:03:59 -07:00
actions.fetchProfileDetails(publicKey.toString())
2022-11-19 11:20:36 -08:00
handleGetWalletMangoData(wallet)
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
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-12-16 03:26:42 -08:00
!isMobile ? 'w-48 border-l border-th-bkg-3 px-4' : ''
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 ? (
2022-12-19 20:48:29 -08:00
<div className="ml-2.5 overflow-hidden text-left">
<p className="text-xs text-th-fgd-3">
{wallet?.adapter.name}
</p>
2023-01-05 03:32:18 -08:00
<p className="truncate pr-2 text-sm font-bold text-th-fgd-1">
2022-12-25 02:27:53 -08:00
{publicKey ? abbreviateAddress(publicKey) : ''}
2022-09-21 21:25:24 -07:00
</p>
{/* <p className="truncate pr-2 text-sm font-bold capitalize text-th-fgd-1">
2022-12-23 16:04:21 -08:00
{profileDetails?.profile_name
? profileDetails.profile_name
: 'Profile Unavailabe'}
</p> */}
2022-09-21 21:25:24 -07:00
</div>
) : null}
</div>
2022-08-01 22:32:21 -07:00
</Menu.Button>
<Transition
appear={true}
show={open}
as={Fragment}
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
2022-12-16 03:44:21 -08:00
className="default-transition flex w-full flex-row items-center rounded-none py-0.5 font-normal focus:outline-none md:hover:cursor-pointer md:hover:text-th-fgd-1"
2022-09-20 22:31:54 -07:00
onClick={() => setShowEditProfileModal(true)}
>
<UserCircleIcon className="h-4 w-4" />
<div className="pl-2 text-left">
{t('profile:edit-profile-pic')}
2022-09-20 22:31:54 -07:00
</div>
</button>
</Menu.Item>
2022-10-25 04:09:36 -07:00
{isMobile ? (
<Menu.Item>
2022-07-26 21:40:17 -07:00
<button
2022-12-16 03:44:21 -08:00
className="default-transition flex w-full flex-row items-center rounded-none py-0.5 font-normal 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}
{/* <Menu.Item>
2022-08-01 22:32:21 -07:00
<button
2022-11-30 19:32:32 -08:00
className="flex w-full flex-row items-center rounded-none py-0.5 font-normal hover:cursor-pointer hover:text-th-active focus:outline-none"
2022-08-01 22:32:21 -07:00
onClick={() => setShowProfileImageModal(true)}
>
<ProfileIcon className="h-4 w-4" />
<div className="pl-2 text-left">
{t('edit-profile-image')}
</div>
</button>
</Menu.Item> */}
2022-08-01 22:32:21 -07:00
<Menu.Item>
<button
2022-12-16 03:44:21 -08:00
className="default-transition flex w-full flex-row items-center rounded-none py-0.5 font-normal focus:outline-none md:hover:cursor-pointer md:hover:text-th-fgd-1"
2022-08-01 22:32:21 -07:00
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