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

170 lines
6.5 KiB
TypeScript
Raw Normal View History

import { Popover, 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)
}
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.initialLoad = true
state.mangoAccount.openOrders = {}
2023-01-15 21:13:34 -08:00
state.mangoAccount.interestTotals = { data: [], loading: false }
state.mangoAccount.performance = {
data: [],
2023-02-11 04:40:23 -08:00
loading: true,
2023-01-15 21:13:34 -08:00
}
2022-07-26 21:40:17 -07:00
})
2022-08-19 23:06:00 -07:00
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
<>
<Popover>
<div className="relative">
<Popover.Button
className={`default-transition h-16 ${
!isMobile ? 'w-48 border-l border-th-bkg-3 px-4' : ''
2023-04-19 18:01:31 -07:00
} focus:outline-none focus-visible:bg-th-bkg-3 md:hover:bg-th-bkg-2`}
>
<div className="flex items-center" id="account-step-one">
<ProfileImage
imageSize="40"
placeholderSize="24"
isOwnerProfile
/>
{!loadProfileDetails && !isMobile ? (
<div className="ml-2.5 overflow-hidden text-left">
<p className="text-xs text-th-fgd-3">
{wallet?.adapter.name}
</p>
<p className="truncate pr-2 text-sm font-bold text-th-fgd-1">
{publicKey ? abbreviateAddress(publicKey) : ''}
</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> */}
</div>
) : null}
</div>
</Popover.Button>
<Transition
as={Fragment}
enter="transition ease-in duration-200"
enterFrom="opacity-0 scale-75"
enterTo="opacity-100 scale-100"
leave="transition ease-out duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<Popover.Panel 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 focus:outline-none md:rounded-r-none">
<button
className="default-transition flex w-full flex-row items-center rounded-none py-0.5 font-normal focus:outline-none focus-visible:text-th-active md:hover:cursor-pointer md:hover:text-th-fgd-1"
onClick={() => setShowEditProfileModal(true)}
>
<UserCircleIcon className="h-4 w-4" />
<div className="pl-2 text-left">
{t('profile:edit-profile-pic')}
</div>
</button>
{isMobile ? (
<button
className="default-transition flex w-full flex-row items-center rounded-none py-0.5 font-normal focus:outline-none focus-visible:text-th-active"
onClick={() => setShowMangoAccountsModal(true)}
>
<CurrencyDollarIcon className="h-4 w-4" />
<div className="pl-2 text-left">{t('accounts')}</div>
</button>
) : null}
<button
className="default-transition flex w-full flex-row items-center rounded-none py-0.5 font-normal focus:outline-none focus-visible:text-th-active md:hover:cursor-pointer md:hover:text-th-fgd-1"
onClick={handleDisconnect}
>
<ArrowRightOnRectangleIcon className="h-4 w-4" />
<div className="pl-2 text-left">
<div className="pb-0.5">{t('disconnect')}</div>
{publicKey ? (
<div className="font-mono text-xs text-th-fgd-4">
{abbreviateAddress(publicKey)}
2022-08-01 22:32:21 -07:00
</div>
) : null}
</div>
</button>
</Popover.Panel>
</Transition>
</div>
</Popover>
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