import { Menu, Transition } from '@headlessui/react' import { ArrowRightOnRectangleIcon, CurrencyDollarIcon, UserCircleIcon, } from '@heroicons/react/20/solid' import { useWallet, Wallet } 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 { PublicKey } from '@solana/web3.js' import { useViewport } from 'hooks/useViewport' import { breakpoints } from '../../utils/theme' import EditProfileModal from '@components/modals/EditProfileModal' import MangoAccountsListModal from '@components/modals/MangoAccountsListModal' import { Wallet as AnchorWallet } from '@project-serum/anchor' const ConnectedMenu = () => { const { t } = useTranslation('common') const { publicKey, disconnect, wallet } = useWallet() const { width } = useViewport() const [showEditProfileModal, setShowEditProfileModal] = useState(false) const [showMangoAccountsModal, setShowMangoAccountsModal] = useState(false) const set = mangoStore((s) => s.set) const actions = mangoStore((s) => s.actions) const profileDetails = mangoStore((s) => s.profile.details) const loadProfileDetails = mangoStore((s) => s.profile.loadDetails) const isMobile = width ? width < breakpoints.md : false 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) } const handleDisconnect = useCallback(() => { set((state) => { state.activityFeed.feed = [] state.activityFeed.initialLoad = false state.mangoAccount.current = undefined state.mangoAccounts = [] state.mangoAccount.openOrders = {} state.mangoAccount.stats.interestTotals = { data: [], loading: false } state.mangoAccount.stats.performance = { data: [], loading: false } }) disconnect() wallet?.adapter.disconnect() notify({ type: 'info', title: t('wallet-disconnected'), }) }, [set, t, disconnect]) useEffect(() => { const handleGetWalletMangoData = async (wallet: Wallet) => { const actions = mangoStore.getState().actions await actions.connectMangoClientWithWallet(wallet) await onConnectFetchAccountData(wallet) } if (publicKey && wallet) { actions.fetchProfileDetails(publicKey.toString()) handleGetWalletMangoData(wallet) } }, [publicKey, actions, wallet]) const { profile_name, wallet_pk } = profileDetails return ( <> {({ open }) => (
{!loadProfileDetails && !isMobile ? (

{wallet_pk ? abbreviateAddress(new PublicKey(wallet_pk)) : ''}

{profile_name}

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