mango-v4-ui/pages/index.tsx

320 lines
11 KiB
TypeScript
Raw Normal View History

2022-08-11 12:29:07 -07:00
import {
HealthType,
toUiDecimalsForQuote,
} from '@blockworks-foundation/mango-v4'
2022-04-12 13:48:22 -07:00
import type { NextPage } from 'next'
2022-07-14 22:20:20 -07:00
import { useTranslation } from 'next-i18next'
2022-07-14 16:36:31 -07:00
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
2022-08-10 21:09:58 -07:00
import { useEffect, useMemo, useState } from 'react'
2022-07-15 04:09:23 -07:00
import AccountActions from '../components/account/AccountActions'
2022-07-14 22:20:20 -07:00
import DepositModal from '../components/modals/DepositModal'
import WithdrawModal from '../components/modals/WithdrawModal'
2022-08-13 03:44:13 -07:00
import mangoStore, { PerformanceDataItem } from '../store/state'
2022-07-14 22:20:20 -07:00
import { formatDecimal } from '../utils/numbers'
2022-07-23 04:30:50 -07:00
import FlipNumbers from 'react-flip-numbers'
2022-08-10 21:09:58 -07:00
import {
DownTriangle,
UpTriangle,
} from '../components/shared/DirectionTriangles'
2022-08-03 02:49:31 -07:00
import SimpleAreaChart from '../components/shared/SimpleAreaChart'
import { COLORS } from '../styles/colors'
import { useTheme } from 'next-themes'
import { IconButton } from '../components/shared/Button'
2022-08-13 03:44:13 -07:00
import { ArrowsExpandIcon, ChevronRightIcon } from '@heroicons/react/solid'
2022-08-03 02:49:31 -07:00
import { Transition } from '@headlessui/react'
2022-08-10 04:17:10 -07:00
import AccountTabs from '../components/account/AccountTabs'
2022-08-10 21:09:58 -07:00
import SheenLoader from '../components/shared/SheenLoader'
2022-08-15 17:16:21 -07:00
import AccountChart from '../components/account/AccountChart'
2022-08-10 23:23:18 -07:00
2022-07-14 16:36:31 -07:00
export async function getStaticProps({ locale }: { locale: string }) {
return {
props: {
2022-07-15 04:09:23 -07:00
...(await serverSideTranslations(locale, ['common', 'close-account'])),
2022-07-14 16:36:31 -07:00
},
}
}
2022-04-12 13:48:22 -07:00
2022-07-14 16:36:31 -07:00
const Index: NextPage = () => {
2022-07-14 22:20:20 -07:00
const { t } = useTranslation('common')
2022-07-27 23:35:18 -07:00
const mangoAccount = mangoStore((s) => s.mangoAccount.current)
2022-08-12 05:13:11 -07:00
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
)
2022-08-10 23:23:18 -07:00
const [showDepositModal, setShowDepositModal] = useState<boolean>(false)
const [showWithdrawModal, setShowWithdrawModal] = useState<boolean>(false)
2022-08-14 22:43:15 -07:00
const [chartToShow, setChartToShow] = useState<
2022-08-15 17:16:21 -07:00
'account-value' | 'cumulative-interest-value' | 'pnl' | ''
2022-08-14 22:43:15 -07:00
>('')
2022-08-13 03:44:13 -07:00
const [oneDayPerformanceData, setOneDayPerformanceData] = useState<
PerformanceDataItem[]
>([])
2022-08-10 23:23:18 -07:00
const [showExpandChart, setShowExpandChart] = useState<boolean>(false)
2022-08-03 02:49:31 -07:00
const { theme } = useTheme()
2022-08-13 03:44:13 -07:00
useEffect(() => {
if (!oneDayPerformanceData.length && performanceData.length) {
setOneDayPerformanceData(performanceData)
}
2022-08-13 22:07:53 -07:00
}, [oneDayPerformanceData, performanceData])
2022-08-03 02:49:31 -07:00
const onHoverMenu = (open: boolean, action: string) => {
if (
(!open && action === 'onMouseEnter') ||
(open && action === 'onMouseLeave')
) {
setShowExpandChart(!open)
}
}
2022-07-15 04:09:23 -07:00
2022-08-13 03:44:13 -07:00
const handleShowAccountValueChart = () => {
2022-08-15 17:16:21 -07:00
setChartToShow('account-value')
2022-08-03 02:49:31 -07:00
setShowExpandChart(false)
}
2022-08-13 03:44:13 -07:00
const handleHideChart = () => {
const set = mangoStore.getState().set
set((s) => {
s.mangoAccount.stats.performance.data = oneDayPerformanceData
})
setChartToShow('')
}
2022-08-14 22:43:15 -07:00
const { accountPnl, accountValueChange } = useMemo(() => {
2022-08-11 02:52:02 -07:00
if (performanceData.length) {
2022-08-14 22:43:15 -07:00
return {
2022-08-15 17:16:21 -07:00
accountPnl: performanceData[performanceData.length - 1].pnl,
2022-08-14 22:43:15 -07:00
accountValueChange:
2022-08-15 17:16:21 -07:00
((performanceData[performanceData.length - 1].account_equity -
performanceData[0].account_equity) /
performanceData[0].account_equity) *
2022-08-14 22:43:15 -07:00
100,
}
2022-08-10 21:09:58 -07:00
}
2022-08-14 22:43:15 -07:00
return { accountPnl: 0, accountValueChange: 0 }
2022-08-11 02:52:02 -07:00
}, [performanceData])
2022-08-10 21:09:58 -07:00
2022-08-13 03:44:13 -07:00
const interestTotalValue = useMemo(() => {
2022-08-11 16:52:31 -07:00
if (totalInterestData.length) {
return totalInterestData.reduce(
(a, c) => a + c.borrow_interest_usd + c.deposit_interest_usd,
0
)
2022-08-10 23:23:18 -07:00
}
return 0
2022-08-11 16:52:31 -07:00
}, [totalInterestData])
2022-08-10 23:23:18 -07:00
2022-08-13 03:44:13 -07:00
return !chartToShow ? (
2022-07-14 22:20:20 -07:00
<>
2022-08-03 05:13:40 -07:00
<div className="mb-8 flex flex-col md:mb-10 lg:flex-row lg:items-end lg:justify-between">
2022-08-03 02:49:31 -07:00
<div className="mb-4 flex items-center space-x-6 lg:mb-0">
<div>
<p className="mb-1">{t('account-value')}</p>
<div className="flex items-center text-5xl font-bold text-th-fgd-1">
$
{mangoAccount ? (
<FlipNumbers
height={48}
width={32}
play
delay={0.05}
duration={1}
numbers={formatDecimal(
toUiDecimalsForQuote(mangoAccount.getEquity().toNumber()),
2022-08-03 02:49:31 -07:00
2
)}
/>
) : (
(0).toFixed(2)
)}
</div>
2022-08-11 02:52:02 -07:00
{performanceData.length ? (
2022-08-10 21:09:58 -07:00
<div className="mt-1 flex items-center space-x-2">
{accountValueChange > 0 ? (
<UpTriangle />
) : accountValueChange < 0 ? (
<DownTriangle />
) : (
2022-08-10 23:23:18 -07:00
''
2022-08-10 21:09:58 -07:00
)}
<p
className={`mb-0.5 ${
accountValueChange > 0
? 'text-th-green'
: accountValueChange < 0
? 'text-th-red'
: 'text-th-fgd-4'
}`}
>
2022-08-10 23:23:18 -07:00
{isNaN(accountValueChange)
? '0.00'
: accountValueChange.toFixed(2)}
%
2022-08-10 21:09:58 -07:00
</p>
</div>
) : null}
2022-08-03 02:49:31 -07:00
</div>
2022-08-12 05:13:11 -07:00
{!loadPerformanceData ? (
2022-08-11 02:52:02 -07:00
performanceData.length ? (
2022-08-10 21:09:58 -07:00
<div
className="relative flex items-end"
onMouseEnter={() =>
onHoverMenu(showExpandChart, 'onMouseEnter')
}
onMouseLeave={() =>
onHoverMenu(showExpandChart, 'onMouseLeave')
}
2022-08-03 02:49:31 -07:00
>
2022-08-10 21:09:58 -07:00
<SimpleAreaChart
color={
accountValueChange >= 0
? COLORS.GREEN[theme]
: COLORS.RED[theme]
}
2022-08-15 17:16:21 -07:00
data={performanceData}
2022-08-10 21:09:58 -07:00
height={88}
name="accountValue"
width={180}
xKey="time"
yKey="account_equity"
/>
<Transition
appear={true}
className="absolute right-2 bottom-2"
show={showExpandChart}
enter="transition-all ease-in duration-300"
enterFrom="opacity-0 transform scale-75"
enterTo="opacity-100 transform scale-100"
leave="transition ease-out duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<IconButton
className="text-th-fgd-3"
hideBg
2022-08-13 03:44:13 -07:00
onClick={() => handleShowAccountValueChart()}
2022-08-10 21:09:58 -07:00
>
<ArrowsExpandIcon className="h-5 w-5" />
</IconButton>
</Transition>
</div>
) : null
) : (
<SheenLoader>
2022-08-11 21:20:17 -07:00
<div className="h-[72px] w-[180px] rounded-md bg-th-bkg-2" />
2022-08-10 21:09:58 -07:00
</SheenLoader>
)}
2022-07-14 22:20:20 -07:00
</div>
2022-07-15 04:09:23 -07:00
<AccountActions />
2022-07-14 22:20:20 -07:00
</div>
2022-08-14 22:43:15 -07:00
<div className="mb-8 grid grid-cols-4 gap-x-6 border-b border-th-bkg-3 md:mb-10 md:border-b-0">
2022-08-03 05:13:40 -07:00
<div className="col-span-3 border-t border-th-bkg-3 py-4 md:col-span-1 md:border-l md:border-t-0 md:pl-6">
<p className="text-th-fgd-3">{t('health')}</p>
<p className="text-2xl font-bold text-th-fgd-1">
{mangoAccount
2022-08-15 15:18:23 -07:00
? mangoAccount.getHealthRatioUi(HealthType.maint)
2022-08-03 05:13:40 -07:00
: 100}
%
</p>
</div>
<div className="col-span-3 border-t border-th-bkg-3 py-4 md:col-span-1 md:border-l md:border-t-0 md:pl-6">
<p className="text-th-fgd-3">{t('free-collateral')}</p>
<p className="text-2xl font-bold text-th-fgd-1">
$
{mangoAccount
? formatDecimal(
2022-08-11 12:29:07 -07:00
toUiDecimalsForQuote(
mangoAccount.getCollateralValue().toNumber()
),
2022-08-03 05:13:40 -07:00
2
)
: (0).toFixed(2)}
</p>
</div>
2022-08-14 22:43:15 -07:00
<div className="col-span-3 flex items-center justify-between border-t border-th-bkg-3 py-4 md:col-span-1 md:border-l md:border-t-0 md:pl-6">
<div>
<p className="text-th-fgd-3">{t('pnl')}</p>
<p className="text-2xl font-bold text-th-fgd-1">
${accountPnl.toFixed(2)}
</p>
</div>
{performanceData.length > 4 ? (
<IconButton onClick={() => setChartToShow('pnl')} size="small">
<ChevronRightIcon className="h-5 w-5" />
</IconButton>
) : null}
</div>
2022-08-11 16:52:31 -07:00
{/* <div className="col-span-3 border-t border-th-bkg-3 py-4 md:col-span-1 md:border-l md:border-t-0 md:pl-6">
2022-08-03 05:13:40 -07:00
<p className="text-th-fgd-3">{t('leverage')}</p>
<p className="text-2xl font-bold text-th-fgd-1">0.0x</p>
2022-08-11 16:52:31 -07:00
</div> */}
2022-08-13 03:44:13 -07:00
<div className="col-span-3 flex items-center justify-between border-t border-th-bkg-3 py-4 md:col-span-1 md:border-l md:border-t-0 md:pl-6">
<div>
<p className="text-th-fgd-3">{t('total-interest-value')}</p>
<p className="text-2xl font-bold text-th-fgd-1">
${interestTotalValue.toFixed(2)}
</p>
</div>
{interestTotalValue > 1 || interestTotalValue < -1 ? (
2022-08-15 17:16:21 -07:00
<IconButton
onClick={() => setChartToShow('cumulative-interest-value')}
size="small"
>
2022-08-13 03:44:13 -07:00
<ChevronRightIcon className="h-5 w-5" />
</IconButton>
) : null}
2022-08-10 23:23:18 -07:00
</div>
2022-08-03 05:13:40 -07:00
</div>
2022-08-10 04:17:10 -07:00
<AccountTabs />
2022-07-14 22:20:20 -07:00
{showDepositModal ? (
<DepositModal
isOpen={showDepositModal}
onClose={() => setShowDepositModal(false)}
/>
) : null}
{showWithdrawModal ? (
<WithdrawModal
isOpen={showWithdrawModal}
onClose={() => setShowWithdrawModal(false)}
/>
) : null}
</>
2022-08-15 17:16:21 -07:00
) : chartToShow === 'account-value' ? (
<AccountChart
chartToShow="account-value"
2022-08-13 03:44:13 -07:00
data={performanceData}
hideChart={handleHideChart}
mangoAccount={mangoAccount!}
2022-08-15 17:16:21 -07:00
yKey="account_equity"
2022-08-13 03:44:13 -07:00
/>
2022-08-15 17:16:21 -07:00
) : chartToShow === 'pnl' ? (
<AccountChart
chartToShow="pnl"
2022-08-11 02:52:02 -07:00
data={performanceData}
2022-08-13 03:44:13 -07:00
hideChart={handleHideChart}
2022-08-10 21:09:58 -07:00
mangoAccount={mangoAccount!}
2022-08-15 17:16:21 -07:00
yKey="pnl"
2022-08-03 02:49:31 -07:00
/>
2022-08-14 22:43:15 -07:00
) : (
2022-08-15 17:16:21 -07:00
<AccountChart
chartToShow="cumulative-interest-value"
data={performanceData.map((d) => ({
interest_value:
d.borrow_interest_cumulative_usd + d.deposit_interest_cumulative_usd,
time: d.time,
}))}
2022-08-14 22:43:15 -07:00
hideChart={handleHideChart}
mangoAccount={mangoAccount!}
2022-08-15 17:16:21 -07:00
yKey="interest_value"
2022-08-14 22:43:15 -07:00
/>
2022-07-14 22:20:20 -07:00
)
2022-04-12 13:48:22 -07:00
}
export default Index