import { HealthType, toUiDecimalsForQuote, } from '@blockworks-foundation/mango-v4' import type { NextPage } from 'next' import { useTranslation } from 'next-i18next' import { serverSideTranslations } from 'next-i18next/serverSideTranslations' import { useEffect, useMemo, useState } from 'react' import AccountActions from '../components/account/AccountActions' import DepositModal from '../components/modals/DepositModal' import WithdrawModal from '../components/modals/WithdrawModal' import mangoStore, { PerformanceDataItem } from '../store/mangoStore' import { formatDecimal, formatFixedDecimals } from '../utils/numbers' import FlipNumbers from 'react-flip-numbers' import { DownTriangle, UpTriangle, } from '../components/shared/DirectionTriangles' import SimpleAreaChart from '../components/shared/SimpleAreaChart' import { COLORS } from '../styles/colors' import { useTheme } from 'next-themes' import { IconButton } from '../components/shared/Button' import { ArrowsExpandIcon, ChevronRightIcon } from '@heroicons/react/solid' import { Transition } from '@headlessui/react' import AccountTabs from '../components/account/AccountTabs' import SheenLoader from '../components/shared/SheenLoader' import AccountChart from '../components/account/AccountChart' import { useViewport } from '../hooks/useViewport' import { breakpoints } from '../utils/theme' import useMangoAccount from '../components/shared/useMangoAccount' export async function getStaticProps({ locale }: { locale: string }) { return { props: { ...(await serverSideTranslations(locale, [ 'common', 'close-account', 'trade', ])), }, } } const Index: NextPage = () => { const { t } = useTranslation('common') const { mangoAccount } = useMangoAccount() const actions = mangoStore((s) => s.actions) const loadPerformanceData = mangoStore( (s) => s.mangoAccount.stats.performance.loading ) const performanceData = mangoStore( (s) => s.mangoAccount.stats.performance.data ) const totalInterestData = mangoStore( (s) => s.mangoAccount.stats.interestTotals.data ) const [showDepositModal, setShowDepositModal] = useState(false) const [showWithdrawModal, setShowWithdrawModal] = useState(false) const [chartToShow, setChartToShow] = useState< 'account-value' | 'cumulative-interest-value' | 'pnl' | '' >('') const [oneDayPerformanceData, setOneDayPerformanceData] = useState< PerformanceDataItem[] >([]) const [showExpandChart, setShowExpandChart] = useState(false) const { theme } = useTheme() const { width } = useViewport() const isMobile = width ? width < breakpoints.md : false useEffect(() => { if (mangoAccount) { const pubKey = mangoAccount.publicKey.toString() actions.fetchAccountPerformance(pubKey, 1) actions.fetchAccountInterestTotals(pubKey) } }, [actions, mangoAccount]) useEffect(() => { if (!oneDayPerformanceData.length && performanceData.length) { setOneDayPerformanceData(performanceData) } }, [oneDayPerformanceData, performanceData]) const onHoverMenu = (open: boolean, action: string) => { if ( (!open && action === 'onMouseEnter') || (open && action === 'onMouseLeave') ) { setShowExpandChart(!open) } } const handleShowAccountValueChart = () => { setChartToShow('account-value') setShowExpandChart(false) } const handleHideChart = () => { const set = mangoStore.getState().set set((s) => { s.mangoAccount.stats.performance.data = oneDayPerformanceData }) setChartToShow('') } const { accountPnl, accountValueChange } = useMemo(() => { if (performanceData.length) { return { accountPnl: performanceData[performanceData.length - 1].pnl, accountValueChange: ((performanceData[performanceData.length - 1].account_equity - performanceData[0].account_equity) / performanceData[0].account_equity) * 100, } } return { accountPnl: 0, accountValueChange: 0 } }, [performanceData]) const interestTotalValue = useMemo(() => { if (totalInterestData.length) { return totalInterestData.reduce( (a, c) => a + c.borrow_interest_usd + c.deposit_interest_usd, 0 ) } return 0 }, [totalInterestData]) const maintHealth = useMemo(() => { return mangoAccount ? mangoAccount.getHealthRatioUi(HealthType.maint) : 100 }, [mangoAccount]) return !chartToShow ? ( <>

{t('account-value')}

$ {mangoAccount ? ( ) : ( )}
{performanceData.length && mangoAccount ? (
{accountValueChange > 0 ? ( ) : accountValueChange < 0 ? ( ) : ( '' )}

0 ? 'text-th-green' : accountValueChange < 0 ? 'text-th-red' : 'text-th-fgd-4' } > {isNaN(accountValueChange) ? '0.00' : accountValueChange.toFixed(2)} %

) : (

0.00%

)}
{!loadPerformanceData ? ( mangoAccount && performanceData.length ? (
onHoverMenu(showExpandChart, 'onMouseEnter') } onMouseLeave={() => onHoverMenu(showExpandChart, 'onMouseLeave') } > = 0 ? COLORS.GREEN[theme] : COLORS.RED[theme] } data={performanceData} height={88} name="accountValue" width={180} xKey="time" yKey="account_equity" /> handleShowAccountValueChart()} >
) : null ) : (
)}

{t('health')}

{maintHealth}%

{t('free-collateral')}

{mangoAccount ? formatFixedDecimals( toUiDecimalsForQuote( mangoAccount.getCollateralValue().toNumber() ), true ) : (0).toFixed(2)}

{/*

{t('pnl')}

{formatFixedDecimals(accountPnl, true)}

{performanceData.length > 4 ? ( setChartToShow('pnl')} size={!isMobile ? 'small' : 'medium'} > ) : null}
*/}

{t('total-interest-value')}

{formatFixedDecimals(interestTotalValue, true)}

{interestTotalValue > 1 || interestTotalValue < -1 ? ( setChartToShow('cumulative-interest-value')} size={!isMobile ? 'small' : 'medium'} > ) : null}
{showDepositModal ? ( setShowDepositModal(false)} /> ) : null} {showWithdrawModal ? ( setShowWithdrawModal(false)} /> ) : null} ) : chartToShow === 'account-value' ? ( ) : chartToShow === 'pnl' ? ( ) : ( ({ interest_value: d.borrow_interest_cumulative_usd + d.deposit_interest_cumulative_usd, time: d.time, }))} hideChart={handleHideChart} mangoAccount={mangoAccount!} yKey="interest_value" /> ) } export default Index