mango-v4-ui/pages/dashboard/risks.tsx

139 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-05-11 14:33:07 -07:00
import useMangoGroup from 'hooks/useMangoGroup'
import type { NextPage } from 'next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { DashboardNavbar } from '.'
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
2023-05-14 10:15:22 -07:00
import { useQuery } from '@tanstack/react-query'
import mangoStore from '@store/mangoStore'
import { getRiskStats } from '@blockworks-foundation/mango-v4'
import { PublicKey } from '@solana/web3.js'
import { formatNumericValue } from 'utils/numbers'
2023-05-11 14:33:07 -07:00
export async function getStaticProps({ locale }: { locale: string }) {
return {
props: {
...(await serverSideTranslations(locale, ['common'])),
},
}
}
2023-05-14 10:15:22 -07:00
type TableData = {
title: string
data: Array<Record<string, { val: string | number | PublicKey }>>
2023-05-14 10:15:22 -07:00
}
const formatValue = (val: string | number | PublicKey) => {
if (val instanceof PublicKey) {
return val.toString()
}
if (typeof val === 'string') {
return val
} else {
return formatNumericValue(val)
}
}
2023-05-11 14:33:07 -07:00
const RiskDashboard: NextPage = () => {
const { group } = useMangoGroup()
2023-05-14 10:15:22 -07:00
const client = mangoStore((s) => s.client)
const { data, isLoading, isFetching } = useQuery(
['risks'],
() => group && getRiskStats(client, group),
{
cacheTime: 1000 * 60 * 10,
retry: 3,
refetchOnWindowFocus: true,
enabled: !!group,
}
)
console.log('resp', isLoading, isFetching, data)
2023-05-11 14:33:07 -07:00
return (
<div className="grid grid-cols-12">
<div className="col-span-12 xl:col-span-8 xl:col-start-3">
<div className="p-8 pb-20 text-th-fgd-1 md:pb-16 xl:p-10">
2023-05-11 14:33:07 -07:00
<h1>Dashboard</h1>
<DashboardNavbar />
2023-05-14 10:15:22 -07:00
{group && data ? (
2023-05-11 14:33:07 -07:00
<div className="mt-4">
2023-05-14 10:15:22 -07:00
{Object.entries(data).map(
([tableType, table]: [string, TableData]) => {
return (
<div className="mt-12" key={tableType}>
<div className="mb-4">
<p className="text-th-fgd-4">{table.title}</p>
</div>
<Table>
<thead>
<TrHead className="border">
{Object.keys(table.data[0]).map(
(colName: string) => {
return (
<Th
xBorder
className="text-left"
key={colName}
>
{colName}{' '}
{colName.toLowerCase().includes('fee') ||
colName.toLowerCase().includes('slippage')
? '(bps)'
: ''}
{colName.toLowerCase().includes('assets') ||
colName.toLowerCase().includes('liabs') ||
colName.toLowerCase().includes('equity') ||
colName.toLowerCase().includes('price') ||
colName.toLowerCase().includes('position')
? '($)'
: ''}
2023-05-14 10:15:22 -07:00
</Th>
)
}
)}
</TrHead>
</thead>
<tbody>
{table.data.map((rowData, index: number) => {
return (
<TrBody key={index}>
{Object.values(rowData).map(
(
col: {
val: string | number | PublicKey
},
2023-05-14 10:15:22 -07:00
idx: number
) => {
return (
<Td
xBorder
className="text-left"
key={idx}
>
{formatValue(col.val)}
2023-05-14 10:15:22 -07:00
</Td>
)
}
)}
</TrBody>
)
})}
</tbody>
</Table>
</div>
)
}
)}
2023-05-11 14:33:07 -07:00
</div>
) : (
'Loading'
)}
</div>
</div>
</div>
)
}
export default RiskDashboard