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

185 lines
7.1 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 } 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'
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'
import { TV_USER_ID_KEY } from 'utils/constants'
import useLocalStorageState from 'hooks/useLocalStorageState'
2023-07-21 06:06:49 -07:00
import Loading from '@components/shared/Loading'
import SheenLoader from '@components/shared/SheenLoader'
2023-11-08 20:24:44 -08:00
import useProfileDetails from 'hooks/useProfileDetails'
const set = mangoStore.getState().set
const actions = mangoStore.getState().actions
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 { isDesktop } = useViewport()
const [tvUserId, setTvUserId] = useLocalStorageState(TV_USER_ID_KEY, '')
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)
2023-11-08 20:24:44 -08:00
const {
data: profileDetails,
isInitialLoading: loadProfileDetails,
refetch: refetchProfileDetails,
} = useProfileDetails()
const groupLoaded = mangoStore((s) => s.groupLoaded)
2023-07-21 06:06:49 -07:00
const mangoAccountLoading = mangoStore((s) => s.mangoAccount.initialLoad)
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 = {}
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'),
})
}, [t, disconnect])
2022-07-26 21:40:17 -07:00
2022-09-20 18:03:59 -07:00
useEffect(() => {
if (publicKey && wallet && groupLoaded) {
actions.connectMangoClientWithWallet(wallet)
actions.fetchMangoAccounts(publicKey)
2023-11-08 20:24:44 -08:00
refetchProfileDetails()
actions.fetchWalletTokens(publicKey)
if (!tvUserId) {
setTvUserId(publicKey.toString())
}
2022-09-20 18:03:59 -07:00
}
}, [publicKey, wallet, groupLoaded, tvUserId, setTvUserId])
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
2023-10-12 20:11:24 -07:00
className={`default-transition h-16 border-l border-th-bkg-3 ${
isDesktop ? 'w-48 px-4' : 'w-16'
} hover:bg-th-bkg-2 focus:outline-none focus-visible:bg-th-bkg-3`}
>
<div
className="flex items-center justify-center md:justify-start"
id="account-step-one"
>
2023-07-21 06:06:49 -07:00
{!mangoAccountLoading ? (
<ProfileImage
imageSize="40"
placeholderSize="24"
isOwnerProfile
/>
) : (
<Loading className="h-6 w-6" />
)}
{publicKey && isDesktop ? (
loadProfileDetails || mangoAccountLoading ? (
<div className="ml-2.5">
<SheenLoader>
<div className="h-3 w-24 bg-th-bkg-2" />
</SheenLoader>
<SheenLoader className="mt-1.5">
<div className="h-4 w-16 bg-th-bkg-2" />
</SheenLoader>
</div>
) : (
<div className="ml-2.5 overflow-hidden text-left">
<p className="text-xs text-th-fgd-3">
2022-12-23 16:04:21 -08:00
{profileDetails?.profile_name
? abbreviateAddress(publicKey)
: wallet?.adapter.name}
</p>
<p className="truncate pr-2 text-sm font-bold text-th-fgd-1">
{profileDetails?.profile_name ? (
<span className="capitalize">
{profileDetails?.profile_name}
</span>
) : (
abbreviateAddress(publicKey)
)}
</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">
{!isDesktop ? (
<button
className="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
2023-04-19 18:12:45 -07:00
className="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')}
</div>
</button>
<button
2023-04-19 18:12:45 -07:00
className="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