mango-v4-ui/components/account/AccountOverview.tsx

133 lines
4.7 KiB
TypeScript
Raw Normal View History

2023-10-17 04:28:43 -07:00
import { useMemo, useState } from 'react'
import AccountActions from './AccountActions'
import useMangoGroup from 'hooks/useMangoGroup'
import useMangoAccount from 'hooks/useMangoAccount'
import { toUiDecimalsForQuote } from '@blockworks-foundation/mango-v4'
import useAccountPerformanceData from 'hooks/useAccountPerformanceData'
import dayjs from 'dayjs'
import AccountHeroStats from './AccountHeroStats'
import { useTranslation } from 'react-i18next'
import Explore from '@components/explore/Explore'
import DetailedAreaOrBarChart from '@components/shared/DetailedAreaOrBarChart'
import { formatYAxis } from 'utils/formatting'
import { useWallet } from '@solana/wallet-adapter-react'
import ConnectEmptyState from '@components/shared/ConnectEmptyState'
import CreateAccountModal from '@components/modals/CreateAccountModal'
import { FaceSmileIcon } from '@heroicons/react/20/solid'
import Button from '@components/shared/Button'
2024-02-20 18:08:12 -08:00
import Announcements from './Announcements'
2023-10-17 04:28:43 -07:00
const EMPTY_STATE_WRAPPER_CLASSES =
'flex h-[180px] flex-col justify-center pb-4 md:h-full'
2023-12-03 04:27:49 -08:00
const DEFAULT_CHART_DATA = [
{
account_equity: 0,
time: dayjs().subtract(1, 'hour').toISOString(),
borrow_interest_cumulative_usd: 0,
deposit_interest_cumulative_usd: 0,
pnl: 0,
spot_value: 0,
transfer_balance: 0,
},
]
2023-10-17 04:28:43 -07:00
const AccountOverview = () => {
const { t } = useTranslation(['common', 'governance'])
const { group } = useMangoGroup()
const { mangoAccount, initialLoad } = useMangoAccount()
const { connected } = useWallet()
2023-10-17 22:08:40 -07:00
const { performanceData, loadingPerformanceData } =
2023-10-17 04:28:43 -07:00
useAccountPerformanceData()
const [daysToShow, setDaysToShow] = useState('1')
const [showCreateAccountModal, setShowCreateAccountModal] = useState(false)
const accountValue = useMemo(() => {
if (!group || !mangoAccount) return 0
return toUiDecimalsForQuote(mangoAccount.getEquity(group).toNumber())
}, [group, mangoAccount])
const latestAccountData = useMemo(() => {
return [
{
account_equity: accountValue,
2023-12-03 04:27:49 -08:00
time: dayjs().toISOString(),
2023-12-05 19:41:54 -08:00
borrow_interest_cumulative_usd: 0,
deposit_interest_cumulative_usd: 0,
pnl: 0,
spot_value: 0,
transfer_balance: 0,
2023-10-17 04:28:43 -07:00
},
]
2023-12-05 19:41:54 -08:00
}, [accountValue])
2023-10-17 04:28:43 -07:00
2023-12-03 04:27:49 -08:00
const chartData = useMemo(() => {
2023-12-05 19:41:54 -08:00
if (performanceData && performanceData?.length) {
2023-12-03 04:27:49 -08:00
return performanceData.concat(latestAccountData)
}
2023-12-05 19:41:54 -08:00
return DEFAULT_CHART_DATA.concat(latestAccountData)
2023-12-03 04:27:49 -08:00
}, [latestAccountData, performanceData])
2023-10-17 22:08:40 -07:00
2023-10-17 04:28:43 -07:00
return (
<>
<div className="grid grid-cols-12 border-b border-th-bkg-3">
2023-12-18 01:42:42 -08:00
<div className="col-span-12 border-b border-th-bkg-3 md:col-span-8 md:border-b-0 md:border-r">
2024-02-11 19:14:47 -08:00
<div
className="flex h-full w-full flex-col justify-between"
id="account-chart"
>
2023-10-17 04:28:43 -07:00
{mangoAccount || (connected && initialLoad) ? (
2024-02-11 19:14:47 -08:00
<div className="overflow-x-hidden p-4 md:px-6">
2023-10-17 04:28:43 -07:00
<DetailedAreaOrBarChart
2023-10-24 05:01:29 -07:00
changeAsPercent
2023-12-03 04:27:49 -08:00
data={chartData}
2023-10-17 04:28:43 -07:00
daysToShow={daysToShow}
setDaysToShow={setDaysToShow}
loading={loadingPerformanceData || initialLoad}
heightClass="h-[180px] lg:h-[205px]"
hideAxis
loaderHeightClass="h-[290px] lg:h-[315px]"
prefix="$"
tickFormat={(x) => `$${formatYAxis(x)}`}
title={t('account-value')}
xKey="time"
yKey="account_equity"
2023-11-02 17:27:20 -07:00
isPrivate
2023-10-17 04:28:43 -07:00
/>
</div>
) : connected ? (
<div className={EMPTY_STATE_WRAPPER_CLASSES}>
<div className="flex flex-col items-center">
<FaceSmileIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
<p className="mb-4">Create a Mango Account to get started</p>
<Button onClick={() => setShowCreateAccountModal(true)}>
{t('create-account')}
</Button>
</div>
</div>
) : (
<div className={EMPTY_STATE_WRAPPER_CLASSES}>
<ConnectEmptyState text={t('governance:connect-wallet')} />
</div>
)}
2023-10-18 17:12:15 -07:00
<AccountActions />
2023-10-17 04:28:43 -07:00
</div>
</div>
<div className="col-span-12 md:col-span-4">
<AccountHeroStats accountValue={accountValue} />
</div>
</div>
2024-02-21 02:19:46 -08:00
<Announcements />
2023-10-17 04:28:43 -07:00
<Explore />
{showCreateAccountModal ? (
<CreateAccountModal
isOpen={showCreateAccountModal}
onClose={() => setShowCreateAccountModal(false)}
/>
) : null}
</>
)
}
export default AccountOverview