diff --git a/src/app/collective-rewards/rewards/LastCycleRewards.tsx b/src/app/collective-rewards/rewards/LastCycleRewards.tsx index 25115932..f3f38886 100644 --- a/src/app/collective-rewards/rewards/LastCycleRewards.tsx +++ b/src/app/collective-rewards/rewards/LastCycleRewards.tsx @@ -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 = ({ +const RewardsTokenMetrics: FC = ({ 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 +} + +type LastCycleRewardsProps = { + gauge: Address + data: { + [token: string]: { + symbol: string + address: Address + } + } + currency?: string +} - const isLoading = cycleLoading || logsLoading +export const LastCycleRewards: FC = ({ data: { rif, rbtc }, ...rest }) => { + const [{ isLoading: isLoadingRif }, setRifState] = useState({ isLoading: false }) + const [{ isLoading: isLoadingRbtc }, setRbtcState] = useState({ isLoading: false }) return ( - + + + setRifState} /> + setRbtcState} /> + ) } diff --git a/src/app/collective-rewards/rewards/components/Metrics/MetricsCard.tsx b/src/app/collective-rewards/rewards/components/Metrics/MetricsCard.tsx index 6931a60c..80313689 100644 --- a/src/app/collective-rewards/rewards/components/Metrics/MetricsCard.tsx +++ b/src/app/collective-rewards/rewards/components/Metrics/MetricsCard.tsx @@ -1,26 +1,17 @@ -import { cn, shortAddress } from '@/lib/utils' +import { cn } from '@/lib/utils' import { FC, ReactNode } from 'react' import { Address } from 'viem' -import { BoxIcon } from 'lucide-react' -import { EXPLORER_URL } from '@/lib/constants' import { withSpinner } from '@/components/LoadingSpinner/withLoadingSpinner' -import { Span, Typography } from '@/components/Typography' +import { Typography } from '@/components/Typography' type MetricsCardRow = { amount: string fiatAmount?: string + action?: JSX.Element + children?: ReactNode } type MetricsCardProps = { - /** - * The title of the card, usually indicating the type of balance. - */ - title: ReactNode - - data: { - [token: string]: MetricsCardRow - } - /** * Whether the card should have a border or not. */ @@ -31,33 +22,41 @@ type MetricsCardProps = { */ contractAddress?: Address 'data-testid'?: string + + /** + * The children of the card. Usually a MetricsCardRow. + */ + children?: ReactNode } const DEFAULT_CLASSES = 'h-min-[79px] w-full py-[12px] px-[12px] flex flex-col bg-foreground' -export const MetricsCardRow: FC = ({ amount, fiatAmount }) => ( -
- - {amount} - - {fiatAmount && ( +export const TokenMetricsCardRow: FC = ({ amount, fiatAmount, children }) => ( +
+
- {fiatAmount} + {amount} - )} + {fiatAmount && ( + + {fiatAmount} + + )} +
+ {children}
) @@ -65,39 +64,29 @@ export const MetricsCardRow: FC = ({ amount, fiatAmount }) => ( * Card for displaying balance and corresponding (fiat) value. */ export const MetricsCard: FC = ({ - title, borderless = false, - data: { rif, rbtc }, - contractAddress, + children, 'data-testid': dataTestId, }) => { const borderClasses = borderless ? '' : 'border border-white border-opacity-40 rounded-lg' return (
- {typeof title === 'string' ? ( -
- - {title} - -
- ) : ( - title - )} - - - {contractAddress && ( - - - - {shortAddress(contractAddress)} - - - )} + {children}
) } +export const MetricsCardTitle: FC<{ title: string; 'data-testid': string }> = ({ + title, + 'data-testid': dataTestId, +}) => ( + + {title} + +) + export const MetricsCardWithSpinner = withSpinner(MetricsCard) diff --git a/src/app/collective-rewards/rewards/hooks/useGetNotifyRewardLogs.ts b/src/app/collective-rewards/rewards/hooks/useGetNotifyRewardLogs.ts index c0a26f70..5fae824a 100644 --- a/src/app/collective-rewards/rewards/hooks/useGetNotifyRewardLogs.ts +++ b/src/app/collective-rewards/rewards/hooks/useGetNotifyRewardLogs.ts @@ -8,10 +8,10 @@ export type NotifyRewardEventLog = ReturnType -export const useGetNotifyRewardLogs = (gauge?: Address) => { +export const useGetNotifyRewardLogs = (gauge: Address) => { const { data, error, isLoading } = useQuery({ queryFn: async () => { - const { data } = await fetchNotifyRewardLogs(gauge!) + const { data } = await fetchNotifyRewardLogs(gauge) const events = parseEventLogs({ abi: GaugeAbi,