mango-v4-ui/pages/index.tsx

156 lines
4.9 KiB
TypeScript
Raw Normal View History

2022-07-14 22:20:20 -07:00
import { toUiDecimals } 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-07-21 22:09:04 -07:00
import { 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-07-20 21:50:56 -07:00
import TokenList from '../components/TokenList'
2022-07-14 22:20:20 -07:00
import mangoStore from '../store/state'
import { formatDecimal } from '../utils/numbers'
2022-07-23 04:30:50 -07:00
import FlipNumbers from 'react-flip-numbers'
2022-08-03 02:49:31 -07:00
import { 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 } from '@heroicons/react/solid'
import DetailedAreaChart from '../components/shared/DetailedAreaChart'
import { Transition } from '@headlessui/react'
2022-04-12 13:48:22 -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-08-03 02:49:31 -07:00
const chartData = [
[1, 300],
[2, 310],
[3, 320],
[4, 330],
[5, 340],
[6, 350],
[7, 360],
[8, 370],
[9, 380],
[10, 390],
]
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-07-14 22:20:20 -07:00
const [showDepositModal, setShowDepositModal] = useState(false)
const [showWithdrawModal, setShowWithdrawModal] = useState(false)
2022-08-03 02:49:31 -07:00
const [showDetailedValueChart, setShowDetailedValueChart] = useState(false)
const [showExpandChart, setShowExpandChart] = useState(false)
const { theme } = useTheme()
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-03 02:49:31 -07:00
const handleShowDetailedValueChart = () => {
setShowDetailedValueChart(true)
setShowExpandChart(false)
}
return !showDetailedValueChart ? (
2022-07-14 22:20:20 -07:00
<>
2022-08-03 02:49:31 -07:00
<div className="mb-8 flex flex-col border-b border-th-bkg-3 pb-8 lg:flex-row lg:items-end lg:justify-between">
<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(
toUiDecimals(mangoAccount.getEquity().toNumber()),
2
)}
/>
) : (
(0).toFixed(2)
)}
</div>
<div className="mt-1 flex items-center space-x-2">
<UpTriangle />
<p className="mb-0.5 text-th-green">2.13%</p>
</div>
</div>
<div
className="relative flex items-end"
onMouseEnter={() => onHoverMenu(showExpandChart, 'onMouseEnter')}
onMouseLeave={() => onHoverMenu(showExpandChart, 'onMouseLeave')}
>
<SimpleAreaChart
color={COLORS.GREEN[theme]}
data={chartData}
height={106}
name="accountValue"
width={240}
/>
<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
onClick={() => handleShowDetailedValueChart()}
>
<ArrowsExpandIcon className="h-5 w-5" />
</IconButton>
</Transition>
2022-07-23 04:30:50 -07:00
</div>
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-07-20 21:50:56 -07:00
<TokenList />
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-03 02:49:31 -07:00
) : (
<DetailedAreaChart
data={chartData}
hideChart={() => setShowDetailedValueChart(false)}
title={t('account-value')}
xKey="0"
yKey="1"
/>
2022-07-14 22:20:20 -07:00
)
2022-04-12 13:48:22 -07:00
}
export default Index