mars-v2-frontend/src/components/Borrow/Borrowings.tsx
Linkie Link e3118004ce
Small adjustments (#115)
* fix: added current SCs

* fix: fixed disabled buttons

* chore: updated all packages

* tidy: cleanup

---------

Co-authored-by: Bob van der Helm <34470358+bobthebuidlr@users.noreply.github.com>
2023-03-21 13:32:21 +01:00

91 lines
2.3 KiB
TypeScript

import { Suspense } from 'react'
import { Card } from 'components/Card'
import { getAccountDebts, getBorrowData } from 'utils/api'
import { getMarketAssets } from 'utils/assets'
import { BorrowTable } from 'components/Borrow/BorrowTable'
interface Props extends PageProps {
type: 'active' | 'available'
}
async function Content(props: Props) {
const debtData = await getAccountDebts(props.params?.account)
const borrowData = await getBorrowData()
const marketAssets = getMarketAssets()
function getBorrowAssets() {
return marketAssets.reduce(
(prev: { available: BorrowAsset[]; active: BorrowAssetActive[] }, curr) => {
const borrow = borrowData.find((borrow) => borrow.denom === curr.denom)
if (borrow) {
const debt = debtData.find((debt) => debt.denom === curr.denom)
if (debt) {
prev.active.push({
...borrow,
debt: debt.amount,
})
} else {
prev.available.push(borrow)
}
}
return prev
},
{ available: [], active: [] },
)
}
const { available, active } = getBorrowAssets()
const assets = props.type === 'active' ? active : available
if (!assets.length) return null
if (props.type === 'active') {
return (
<Card
className='h-fit w-full'
title={props.type === 'active' ? 'Borrowings' : 'Available to borrow'}
>
<BorrowTable data={assets} />
</Card>
)
}
return <BorrowTable data={assets} />
}
function Fallback() {
const marketAssets = getMarketAssets()
const available: BorrowAsset[] = marketAssets.reduce((prev: BorrowAsset[], curr) => {
prev.push({ ...curr, borrowRate: null, liquidity: null })
return prev
}, [])
return <BorrowTable data={available} />
}
export function AvailableBorrowings(props: PageProps) {
return (
<Card className='h-fit w-full' title={'Available to borrow'}>
<Suspense fallback={<Fallback />}>
{/* @ts-expect-error Server Component */}
<Content params={props.params} type='available' />
</Suspense>
</Card>
)
}
export function ActiveBorrowings(props: PageProps) {
return (
<Suspense fallback={null}>
{/* @ts-expect-error Server Component */}
<Content params={props.params} type='active' />
</Suspense>
)
}