|
| 1 | +import { useMemo, useState } from 'react' |
1 | 2 | import { Text } from 'rebass' |
| 3 | +import { type PoolAnalyticsWindow, usePoolLiquidityFlowsQuery, usePoolPriceQuery } from 'services/zapEarn' |
2 | 4 |
|
| 5 | +import { Stack } from 'components/Stack' |
3 | 6 | import useTheme from 'hooks/useTheme' |
| 7 | +import { usePoolDetailContext } from 'pages/Earns/PoolDetail/context' |
| 8 | +import LiquidityFlowsChart from 'pages/Earns/PoolDetail/tabs/analytics/LiquidityFlowsChart' |
| 9 | +import PoolPriceChart from 'pages/Earns/PoolDetail/tabs/analytics/PoolPriceChart' |
| 10 | +import { MetricCard, MetricsStrip, formatNumber, formatUsd } from 'pages/Earns/PoolDetail/tabs/analytics/shared' |
4 | 11 |
|
5 | 12 | const AnalyticsTab = () => { |
6 | 13 | const theme = useTheme() |
| 14 | + const { pool, poolParams } = usePoolDetailContext() |
| 15 | + const [window, setWindow] = useState<PoolAnalyticsWindow>('7d') |
| 16 | + |
| 17 | + const { |
| 18 | + data: priceAnalytics, |
| 19 | + isError: isPriceError, |
| 20 | + isLoading: isPriceLoading, |
| 21 | + } = usePoolPriceQuery({ |
| 22 | + chainId: poolParams.poolChainId, |
| 23 | + address: poolParams.poolAddress, |
| 24 | + window, |
| 25 | + }) |
| 26 | + |
| 27 | + const { |
| 28 | + data: liquidityFlows, |
| 29 | + isError: isLiquidityFlowsError, |
| 30 | + isLoading: isLiquidityFlowsLoading, |
| 31 | + } = usePoolLiquidityFlowsQuery({ |
| 32 | + chainId: poolParams.poolChainId, |
| 33 | + address: poolParams.poolAddress, |
| 34 | + window, |
| 35 | + }) |
| 36 | + |
| 37 | + const currentLiquidityBucket = liquidityFlows?.buckets?.[liquidityFlows.buckets.length - 1] |
| 38 | + |
| 39 | + const totalTvl = useMemo(() => { |
| 40 | + if (pool?.tvl !== undefined) return pool.tvl |
| 41 | + if (pool?.poolStats?.tvl !== undefined) return pool.poolStats.tvl |
| 42 | + if (pool?.reserveUsd !== undefined) { |
| 43 | + const parsedReserveUsd = Number(pool.reserveUsd) |
| 44 | + if (!Number.isNaN(parsedReserveUsd)) return parsedReserveUsd |
| 45 | + } |
| 46 | + |
| 47 | + return undefined |
| 48 | + }, [pool?.poolStats?.tvl, pool?.reserveUsd, pool?.tvl]) |
| 49 | + |
| 50 | + const metrics = [ |
| 51 | + { label: 'TVL', value: formatUsd(totalTvl) }, |
| 52 | + { label: 'Current TVL', value: formatUsd(currentLiquidityBucket?.tvlUsd ?? totalTvl) }, |
| 53 | + { label: 'Liquidity Provider', value: formatNumber(pool?.liquidity) }, |
| 54 | + ] |
7 | 55 |
|
8 | 56 | return ( |
9 | | - <Text as="div" color={theme.subText} fontSize={14}> |
10 | | - Analytics content will be updated when the final data and design are ready. |
11 | | - </Text> |
| 57 | + <Stack gap={16}> |
| 58 | + <MetricsStrip> |
| 59 | + {metrics.map(metric => ( |
| 60 | + <MetricCard gap={4} key={metric.label}> |
| 61 | + <Text color={theme.subText} fontSize={14}> |
| 62 | + {metric.label} |
| 63 | + </Text> |
| 64 | + <Text color={theme.text} fontSize={20} fontWeight={500}> |
| 65 | + {metric.value} |
| 66 | + </Text> |
| 67 | + </MetricCard> |
| 68 | + ))} |
| 69 | + </MetricsStrip> |
| 70 | + |
| 71 | + <PoolPriceChart |
| 72 | + analytics={priceAnalytics} |
| 73 | + baseSymbol={pool.tokens[0]?.symbol} |
| 74 | + isError={isPriceError} |
| 75 | + isLoading={isPriceLoading} |
| 76 | + onSelectWindow={setWindow} |
| 77 | + quoteSymbol={pool.tokens[1]?.symbol} |
| 78 | + window={window} |
| 79 | + /> |
| 80 | + |
| 81 | + <LiquidityFlowsChart |
| 82 | + analytics={liquidityFlows} |
| 83 | + isError={isLiquidityFlowsError} |
| 84 | + isLoading={isLiquidityFlowsLoading} |
| 85 | + onSelectWindow={setWindow} |
| 86 | + window={window} |
| 87 | + /> |
| 88 | + </Stack> |
12 | 89 | ) |
13 | 90 | } |
14 | 91 |
|
|
0 commit comments