-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make use of composition for cards
- Loading branch information
1 parent
9d6d74b
commit 2e3ccc3
Showing
3 changed files
with
98 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,74 @@ | ||
import { formatBalanceToHuman } from '@/app/user/Balances/balanceUtils' | ||
import { Address } from 'viem' | ||
import { usePricesContext } from '@/shared/context/PricesContext' | ||
import { FC } from 'react' | ||
import { Cycle, useCycleContext } from '@/app/collective-rewards/metrics/context/CycleContext' | ||
import { useCycleContext } from '@/app/collective-rewards/metrics/context/CycleContext' | ||
import { | ||
NotifyRewardEventLog, | ||
useGetNotifyRewardLogs, | ||
MetricsCardWithSpinner, | ||
formatMetrics, | ||
getLastCycleRewards, | ||
MetricsCardTitle, | ||
MetricsCardWithSpinner, | ||
TokenMetricsCardRow, | ||
useGetNotifyRewardLogs, | ||
} from '@/app/collective-rewards/rewards' | ||
import { useHandleErrors } from '@/app/collective-rewards/utils' | ||
import { formatBalanceToHuman } from '@/app/user/Balances/balanceUtils' | ||
import { usePricesContext } from '@/shared/context/PricesContext' | ||
import { FC, useEffect, useState } from 'react' | ||
import { Address } from 'viem' | ||
|
||
type LastCycleRewardsProps = { | ||
type TokenRewardsProps = { | ||
gauge: Address | ||
currency?: string | ||
data: { | ||
[token: string]: { | ||
symbol: string | ||
address: Address | ||
} | ||
token: { | ||
symbol: string | ||
address: Address | ||
} | ||
setState: (state: { isLoading: boolean }) => void | ||
} | ||
|
||
const useGetRewardMetrics = (cycle: Cycle, logs: NotifyRewardEventLog, symbol: string, currency: string) => { | ||
const { prices } = usePricesContext() | ||
|
||
const lastCycleRewards = getLastCycleRewards(cycle, logs) | ||
const lastCycleRewardsInHuman = Number(formatBalanceToHuman(lastCycleRewards.builderAmount)) | ||
const price = prices[symbol]?.price ?? 0 | ||
|
||
return formatMetrics(lastCycleRewardsInHuman, price, symbol, currency) | ||
} | ||
|
||
export const LastCycleRewards: FC<LastCycleRewardsProps> = ({ | ||
const RewardsTokenMetrics: FC<TokenRewardsProps> = ({ | ||
gauge, | ||
data: { rif, rbtc }, | ||
token: { symbol, address }, | ||
currency = 'USD', | ||
setState, | ||
}) => { | ||
const { data: cycle, isLoading: cycleLoading, error: cycleError } = useCycleContext() | ||
const { data: rewardsPerToken, isLoading: logsLoading, error: rewardsError } = useGetNotifyRewardLogs(gauge) | ||
|
||
const error = cycleError ?? rewardsError | ||
|
||
useHandleErrors([{ error: error, title: 'Error loading last cycle rewards' }]) | ||
|
||
const rifRewardsMetrics = useGetRewardMetrics(cycle, rewardsPerToken[rif.address], rif.symbol, currency) | ||
const rbtcRewardsMetrics = useGetRewardMetrics(cycle, rewardsPerToken[rbtc.address], rbtc.symbol, currency) | ||
const { prices } = usePricesContext() | ||
|
||
const lastCycleRewards = getLastCycleRewards(cycle, rewardsPerToken[address]) | ||
const lastCycleRewardsInHuman = Number(formatBalanceToHuman(lastCycleRewards.builderAmount)) | ||
const price = prices[symbol]?.price ?? 0 | ||
const { amount, fiatAmount } = formatMetrics(lastCycleRewardsInHuman, price, symbol, currency) | ||
|
||
useEffect(() => { | ||
setState({ isLoading: cycleLoading || logsLoading }) | ||
}, [cycleLoading, logsLoading, setState]) | ||
|
||
return <TokenMetricsCardRow amount={amount} fiatAmount={fiatAmount} /> | ||
} | ||
|
||
type LastCycleRewardsProps = { | ||
gauge: Address | ||
data: { | ||
[token: string]: { | ||
symbol: string | ||
address: Address | ||
} | ||
} | ||
currency?: string | ||
} | ||
|
||
const isLoading = cycleLoading || logsLoading | ||
export const LastCycleRewards: FC<LastCycleRewardsProps> = ({ data: { rif, rbtc }, ...rest }) => { | ||
const [{ isLoading: isLoadingRif }, setRifState] = useState({ isLoading: false }) | ||
const [{ isLoading: isLoadingRbtc }, setRbtcState] = useState({ isLoading: false }) | ||
|
||
return ( | ||
<MetricsCardWithSpinner | ||
title="Last cycle rewards" | ||
data={{ rif: rifRewardsMetrics, rbtc: rbtcRewardsMetrics }} | ||
isLoading={isLoading} | ||
borderless | ||
/> | ||
<MetricsCardWithSpinner isLoading={isLoadingRif || isLoadingRbtc} borderless> | ||
<MetricsCardTitle title="Last cycle rewards" data-testid="LastCycleRewards" /> | ||
<RewardsTokenMetrics {...rest} token={rif} setState={() => setRifState} /> | ||
<RewardsTokenMetrics {...rest} token={rbtc} setState={() => setRbtcState} /> | ||
</MetricsCardWithSpinner> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters