From 0d5d3e8c6f578fd7fbc47974c2db78111853763b Mon Sep 17 00:00:00 2001 From: Karolina Kosiorowska Date: Wed, 11 Dec 2024 09:38:45 +0100 Subject: [PATCH] Create one hook for statistics --- dapp/src/hooks/index.ts | 2 +- dapp/src/hooks/useEnhancedStatistics.ts | 28 ----------------- dapp/src/hooks/useStatistics.ts | 31 ++++++++++++++++--- .../pages/DashboardPage/AcreTVLMessage.tsx | 4 +-- .../pages/DashboardPage/AcreTVLProgress.tsx | 4 +-- .../pages/DashboardPage/PositionDetails.tsx | 2 +- 6 files changed, 33 insertions(+), 38 deletions(-) delete mode 100644 dapp/src/hooks/useEnhancedStatistics.ts diff --git a/dapp/src/hooks/index.ts b/dapp/src/hooks/index.ts index 07e87118c..1945ee45e 100644 --- a/dapp/src/hooks/index.ts +++ b/dapp/src/hooks/index.ts @@ -17,7 +17,7 @@ export * from "./usePagination" export * from "./useModal" export * from "./useTransactionModal" export * from "./useVerifyDepositAddress" -export { default as useEnhancedStatistics } from "./useEnhancedStatistics" +export { default as useStatistics } from "./useStatistics" export * from "./useDisconnectWallet" export { default as useWalletConnectionAlert } from "./useWalletConnectionAlert" export { default as useResetWalletState } from "./useResetWalletState" diff --git a/dapp/src/hooks/useEnhancedStatistics.ts b/dapp/src/hooks/useEnhancedStatistics.ts deleted file mode 100644 index d7852dba5..000000000 --- a/dapp/src/hooks/useEnhancedStatistics.ts +++ /dev/null @@ -1,28 +0,0 @@ -import useStatistics from "./useStatistics" - -const useEnhancedStatistics = () => { - const { data } = useStatistics() - - const bitcoinTvl = data?.btc ?? 0 - const usdTvl = data?.usd ?? 0 - const tvlCap = data?.cap ?? 0 - - const isCapExceeded = bitcoinTvl > tvlCap - - const progress = isCapExceeded ? 100 : Math.floor((bitcoinTvl / tvlCap) * 100) - - const remaining = isCapExceeded ? 0 : tvlCap - bitcoinTvl - - return { - tvl: { - progress, - value: bitcoinTvl, - usdValue: usdTvl, - isCapExceeded, - remaining, - cap: tvlCap, - }, - } -} - -export default useEnhancedStatistics diff --git a/dapp/src/hooks/useStatistics.ts b/dapp/src/hooks/useStatistics.ts index 20cddddb0..22dc77acd 100644 --- a/dapp/src/hooks/useStatistics.ts +++ b/dapp/src/hooks/useStatistics.ts @@ -1,13 +1,36 @@ -import { useQuery } from "@tanstack/react-query" -import { REFETCH_INTERVAL_IN_MILLISECONDS, queryKeysFactory } from "#/constants" +import { queryKeysFactory, REFETCH_INTERVAL_IN_MILLISECONDS } from "#/constants" import { acreApi } from "#/utils" +import { useQuery } from "@tanstack/react-query" const { acreKeys } = queryKeysFactory -export default function useStatistics() { - return useQuery({ +const useStatistics = () => { + const { data } = useQuery({ queryKey: [...acreKeys.statistics()], queryFn: acreApi.getStatistics, refetchInterval: REFETCH_INTERVAL_IN_MILLISECONDS, }) + + const bitcoinTvl = data?.btc ?? 0 + const usdTvl = data?.usd ?? 0 + const tvlCap = data?.cap ?? 0 + + const isCapExceeded = bitcoinTvl > tvlCap + + const progress = isCapExceeded ? 100 : Math.floor((bitcoinTvl / tvlCap) * 100) + + const remaining = isCapExceeded ? 0 : tvlCap - bitcoinTvl + + return { + tvl: { + progress, + value: bitcoinTvl, + usdValue: usdTvl, + isCapExceeded, + remaining, + cap: tvlCap, + }, + } } + +export default useStatistics diff --git a/dapp/src/pages/DashboardPage/AcreTVLMessage.tsx b/dapp/src/pages/DashboardPage/AcreTVLMessage.tsx index dadfa67b5..97619481d 100644 --- a/dapp/src/pages/DashboardPage/AcreTVLMessage.tsx +++ b/dapp/src/pages/DashboardPage/AcreTVLMessage.tsx @@ -1,6 +1,6 @@ import React from "react" import { Box, HStack, StackProps, VStack } from "@chakra-ui/react" -import { useActivitiesCount, useEnhancedStatistics, useWallet } from "#/hooks" +import { useActivitiesCount, useStatistics, useWallet } from "#/hooks" import { BoltFilled } from "#/assets/icons" import { TextMd } from "#/components/shared/Typography" import { CurrencyBalance } from "#/components/shared/CurrencyBalance" @@ -8,7 +8,7 @@ import { CurrencyBalance } from "#/components/shared/CurrencyBalance" type AcreTVLMessageProps = Omit export default function AcreTVLMessage(props: AcreTVLMessageProps) { - const { tvl } = useEnhancedStatistics() + const { tvl } = useStatistics() const { isConnected } = useWallet() const activitiesCount = useActivitiesCount() diff --git a/dapp/src/pages/DashboardPage/AcreTVLProgress.tsx b/dapp/src/pages/DashboardPage/AcreTVLProgress.tsx index 228fd4546..51c645153 100644 --- a/dapp/src/pages/DashboardPage/AcreTVLProgress.tsx +++ b/dapp/src/pages/DashboardPage/AcreTVLProgress.tsx @@ -7,7 +7,7 @@ import { Grid, Box, } from "@chakra-ui/react" -import { useEnhancedStatistics } from "#/hooks" +import { useStatistics } from "#/hooks" import { BitcoinIcon } from "#/assets/icons" import { CurrencyBalance } from "#/components/shared/CurrencyBalance" import ProgressBar from "#/components/shared/ProgressBar" @@ -19,7 +19,7 @@ const STEP_COUNT = 5 export function AcreTVLProgress(props: AcreTVLProgressProps) { const styles = useMultiStyleConfig("AcreTVLProgress") - const { tvl } = useEnhancedStatistics() + const { tvl } = useStatistics() const steps = useMemo( () => diff --git a/dapp/src/pages/DashboardPage/PositionDetails.tsx b/dapp/src/pages/DashboardPage/PositionDetails.tsx index 9faebe703..ac295c967 100644 --- a/dapp/src/pages/DashboardPage/PositionDetails.tsx +++ b/dapp/src/pages/DashboardPage/PositionDetails.tsx @@ -49,7 +49,7 @@ export default function PositionDetails() { const { data: activities } = useActivities() const isMobileMode = useMobileMode() - const { tvl } = useEnhancedStatistics() + const { tvl } = useStatistics() const { isConnected } = useWallet()