Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign: Fix infinite loading of connect/invest button #2501

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CurrencyBalance, findBalance, Pool } from '@centrifuge/centrifuge-js'
import { useBalances, useCentrifugeTransaction } from '@centrifuge/centrifuge-react'
import { useBalances, useCentrifugeTransaction, useWallet } from '@centrifuge/centrifuge-react'
import { CentrifugeTransactionOptions } from '@centrifuge/centrifuge-react/dist/hooks/useCentrifugeTransaction'
import BN from 'bn.js'
import * as React from 'react'
Expand All @@ -25,6 +25,7 @@
const trancheMeta = metadata?.tranches?.[trancheId]
const liquidityState = useLiquidityRewards().state
const [isStableLoading, setIsStableLoading] = React.useState(true)
const { connectedType } = useWallet()

if (!tranche) throw new Error(`Token not found. Pool id: ${poolId}, token id: ${trancheId}`)

Expand Down Expand Up @@ -88,7 +89,7 @@

React.useEffect(() => {
const timer = setTimeout(() => {
if (isDataLoading) {
if (isDataLoading && connectedType) {
setIsStableLoading(true)
} else {
setIsStableLoading(false)
Expand All @@ -96,7 +97,7 @@
}, 300)

return () => clearTimeout(timer)
}, [isDataLoading])

Check warning on line 100 in centrifuge-app/src/components/InvestRedeem/InvestRedeemCentrifugeProvider.tsx

View workflow job for this annotation

GitHub Actions / build-app

React Hook React.useEffect has a missing dependency: 'connectedType'. Either include it or remove the dependency array

Check warning on line 100 in centrifuge-app/src/components/InvestRedeem/InvestRedeemCentrifugeProvider.tsx

View workflow job for this annotation

GitHub Actions / ff-prod / build-app

React Hook React.useEffect has a missing dependency: 'connectedType'. Either include it or remove the dependency array

const state: InvestRedeemState = {
poolId,
Expand Down
1 change: 0 additions & 1 deletion centrifuge-app/src/components/IssuerSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ export const PoolAnalysis = ({ metadata, inverted }: IssuerSectionProps & { inve
// Not sure why some pools have N/A, it should be empty but this is a fix for those pools in the meantime
const isEmpty = report?.author.name === 'N/A'

console.log(report)
return report?.author?.name || report?.author?.title ? (
isEmpty ? null : (
<Stack gap={1}>
Expand Down
115 changes: 73 additions & 42 deletions centrifuge-app/src/components/PoolOverview/TrancheTokenCards.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Perquintill } from '@centrifuge/centrifuge-js'
import { Box, Shelf, Text } from '@centrifuge/fabric'
import { Decimal } from 'decimal.js-light'
import { useMemo } from 'react'
import { useTheme } from 'styled-components'
import { InvestButton, Token } from '../../pages/Pool/Overview'
Expand All @@ -10,6 +11,15 @@
import { CentrifugeTargetAPYs, DYF_POOL_ID, NS3_POOL_ID, centrifugeTargetAPYs } from '../PoolCard'
import { PoolMetaDataPartial } from '../PoolList'

type Row = {
tokenName: string
apy: Decimal
tvl: Decimal
tokenPrice: Decimal
subordination: Decimal
trancheId: string
}

export const TrancheTokenCards = ({
trancheTokens,
poolId,
Expand All @@ -22,90 +32,111 @@
const pool = usePool(poolId)
const theme = useTheme()
const isTinlakePool = poolId.startsWith('0x')
const daysSinceCreation = pool?.createdAt ? daysBetween(new Date(pool.createdAt), new Date()) : 0

const getTrancheText = (trancheToken: Token) => {
if (trancheToken.seniority === 0) return 'junior'
if (trancheToken.seniority === 1) return 'senior'
return 'mezzanine'
}

const columnConfig = useMemo(() => {
const calculateApy = (trancheToken: Token) => {
if (isTinlakePool && getTrancheText(trancheToken) === 'senior') return formatPercentage(trancheToken.apy)
if (poolId === DYF_POOL_ID) return centrifugeTargetAPYs[poolId as CentrifugeTargetAPYs][0]
if (poolId === NS3_POOL_ID && trancheToken.seniority === 0)
return centrifugeTargetAPYs[poolId as CentrifugeTargetAPYs][0]
if (poolId === NS3_POOL_ID && trancheToken.seniority === 1)
return centrifugeTargetAPYs[poolId as CentrifugeTargetAPYs][1]
if (daysSinceCreation < 30) return 'N/A'
return trancheToken.yield30DaysAnnualized
? formatPercentage(new Perquintill(trancheToken.yield30DaysAnnualized))
: '-'
}
const calculateApy = (trancheToken: Token) => {
if (isTinlakePool && getTrancheText(trancheToken) === 'senior') return formatPercentage(trancheToken.apy)
if (poolId === DYF_POOL_ID) return centrifugeTargetAPYs[poolId as CentrifugeTargetAPYs][0]
if (poolId === NS3_POOL_ID && trancheToken.seniority === 0)
return centrifugeTargetAPYs[poolId as CentrifugeTargetAPYs][0]
if (poolId === NS3_POOL_ID && trancheToken.seniority === 1)
return centrifugeTargetAPYs[poolId as CentrifugeTargetAPYs][1]
const daysSinceCreation = pool?.createdAt ? daysBetween(new Date(pool.createdAt), new Date()) : 0
if (daysSinceCreation < 30) return 'N/A'
return trancheToken.yield30DaysAnnualized
? formatPercentage(new Perquintill(trancheToken.yield30DaysAnnualized))
: '-'
}

const columns = useMemo(() => {
return [
{
header: 'Token',
align: 'left',
formatter: (v: any) => v,
width: '40%',
align: 'left',
cell: (row: Row) => {
return (
<Text paddingY={2} fontWeight="400" variant="heading2">
{row.tokenName}
</Text>
)
},
},
{
header: poolId === DYF_POOL_ID || poolId === NS3_POOL_ID ? 'Target' : 'APY',
header: 'APY',
align: 'left',
formatter: (v: any) => (v ? calculateApy(v) : '-'),
cell: (row: Row) => {
return (
<Text paddingY={2} fontWeight="600" variant="heading2">
{row.apy}
</Text>
)
},
},
{
header: `TVL (${pool?.currency.symbol})`,
align: 'left',
formatter: (v: any) => (v ? formatBalance(v) : '-'),
cell: (row: Row) => {
return (
<Text paddingY={2} fontWeight="400" variant="heading2">
{formatBalance(row.tvl)}
</Text>
)
},
},
{
header: 'Token price',
header: `Token price (${pool?.currency.symbol})`,
align: 'left',
formatter: (v: any) => (v ? formatBalance(v, pool?.currency.symbol, 6) : '-'),
cell: (row: Row) => {
return (
<Text paddingY={2} fontWeight="400" variant="heading2">
{formatBalance(row.tokenPrice, undefined, 6)}
</Text>
)
},
},
...(pool.tranches.length > 1
? [
{
header: 'Subordination',
align: 'left',
formatter: (_: any, row: any) => {
if (row.value[1].seniority === 0) return '-'
return formatPercentage(row.value[1].protection)
cell: (row: Row) => {
return (
<Text paddingY={2} fontWeight="400" variant="heading2">
{formatPercentage(row.subordination)}
</Text>
)
},
},
]
: []),
{
header: '',
align: 'right',
formatter: (_: any, row: any) => (
<InvestButton poolId={poolId} trancheId={row.value[1].id} metadata={metadata} />
),
cell: (row: Row) => {
return <InvestButton poolId={poolId} trancheId={row.trancheId} metadata={metadata} />
},
},
]
}, [pool, poolId, isTinlakePool, daysSinceCreation, metadata])
}, [pool.tranches, metadata, poolId])

Check warning on line 126 in centrifuge-app/src/components/PoolOverview/TrancheTokenCards.tsx

View workflow job for this annotation

GitHub Actions / build-app

React Hook useMemo has a missing dependency: 'pool?.currency.symbol'. Either include it or remove the dependency array

Check warning on line 126 in centrifuge-app/src/components/PoolOverview/TrancheTokenCards.tsx

View workflow job for this annotation

GitHub Actions / ff-prod / build-app

React Hook useMemo has a missing dependency: 'pool?.currency.symbol'. Either include it or remove the dependency array

const columns = useMemo(() => {
return columnConfig.map((col, index) => {
const dataTable = useMemo(() => {
return trancheTokens.map((tranche) => {
return {
cell: (row: any) => (
<Text paddingY={2} fontWeight={col.header === 'APY' ? '600' : '400'} variant="heading2">
{col.formatter(row.value[index], row)}
</Text>
),
...col,
tokenName: tranche.name,
apy: calculateApy(tranche),
tvl: tranche.valueLocked,
tokenPrice: tranche.tokenPrice,
subordination: tranche.protection,
trancheId: tranche.id,
}
})
}, [columnConfig])

const dataTable = useMemo(() => {
return trancheTokens.map((tranche) => ({
value: [tranche.name, tranche, tranche.valueLocked, tranche.tokenPrice],
}))
}, [trancheTokens])

Check warning on line 139 in centrifuge-app/src/components/PoolOverview/TrancheTokenCards.tsx

View workflow job for this annotation

GitHub Actions / build-app

React Hook useMemo has a missing dependency: 'calculateApy'. Either include it or remove the dependency array

Check warning on line 139 in centrifuge-app/src/components/PoolOverview/TrancheTokenCards.tsx

View workflow job for this annotation

GitHub Actions / ff-prod / build-app

React Hook useMemo has a missing dependency: 'calculateApy'. Either include it or remove the dependency array

return (
<Shelf gap={3}>
Expand Down
Loading