import { Popover, Transition } from '@headlessui/react' import { ArrowRightOnRectangleIcon, CurrencyDollarIcon, UserCircleIcon, } from '@heroicons/react/20/solid' import { useWallet } from '@solana/wallet-adapter-react' import { useTranslation } from 'next-i18next' import { Fragment, useCallback, useEffect, useState } from 'react' import mangoStore from '@store/mangoStore' import { notify } from '../../utils/notifications' import ProfileImage from '../profile/ProfileImage' import { abbreviateAddress } from '../../utils/formatting' import { useViewport } from 'hooks/useViewport' import EditProfileModal from '@components/modals/EditProfileModal' import MangoAccountsListModal from '@components/modals/MangoAccountsListModal' import { TV_USER_ID_KEY } from 'utils/constants' import useLocalStorageState from 'hooks/useLocalStorageState' import Loading from '@components/shared/Loading' import SheenLoader from '@components/shared/SheenLoader' import useProfileDetails from 'hooks/useProfileDetails' const set = mangoStore.getState().set const actions = mangoStore.getState().actions const ConnectedMenu = () => { const { t } = useTranslation('common') const { publicKey, disconnect, wallet } = useWallet() const { isDesktop } = useViewport() const [tvUserId, setTvUserId] = useLocalStorageState(TV_USER_ID_KEY, '') const [showEditProfileModal, setShowEditProfileModal] = useState(false) const [showMangoAccountsModal, setShowMangoAccountsModal] = useState(false) const { data: profileDetails, isInitialLoading: loadProfileDetails, refetch: refetchProfileDetails, } = useProfileDetails() const groupLoaded = mangoStore((s) => s.groupLoaded) const mangoAccountLoading = mangoStore((s) => s.mangoAccount.initialLoad) const handleDisconnect = useCallback(() => { set((state) => { state.activityFeed.feed = [] state.mangoAccount.current = undefined state.mangoAccounts = [] state.mangoAccount.initialLoad = true state.mangoAccount.openOrders = {} }) disconnect() notify({ type: 'info', title: t('wallet-disconnected'), }) }, [t, disconnect]) useEffect(() => { if (publicKey && wallet && groupLoaded) { actions.connectMangoClientWithWallet(wallet) actions.fetchMangoAccounts(publicKey) refetchProfileDetails() actions.fetchWalletTokens(publicKey) if (!tvUserId) { setTvUserId(publicKey.toString()) } } }, [publicKey, wallet, groupLoaded, tvUserId, setTvUserId]) return ( <>
{!mangoAccountLoading ? ( ) : ( )} {publicKey && isDesktop ? ( loadProfileDetails || mangoAccountLoading ? (
) : (

{profileDetails?.profile_name ? abbreviateAddress(publicKey) : wallet?.adapter.name}

{profileDetails?.profile_name ? ( {profileDetails?.profile_name} ) : ( abbreviateAddress(publicKey) )}

) ) : null}
{!isDesktop ? ( ) : null}
{showEditProfileModal ? ( setShowEditProfileModal(false)} /> ) : null} {showMangoAccountsModal ? ( setShowMangoAccountsModal(false)} /> ) : null} ) } export default ConnectedMenu