Skip to content

Commit

Permalink
Create one hook for statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed Dec 11, 2024
1 parent d2128ca commit 0d5d3e8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 38 deletions.
2 changes: 1 addition & 1 deletion dapp/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 0 additions & 28 deletions dapp/src/hooks/useEnhancedStatistics.ts

This file was deleted.

31 changes: 27 additions & 4 deletions dapp/src/hooks/useStatistics.ts
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions dapp/src/pages/DashboardPage/AcreTVLMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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"

type AcreTVLMessageProps = Omit<StackProps, "children">

export default function AcreTVLMessage(props: AcreTVLMessageProps) {
const { tvl } = useEnhancedStatistics()
const { tvl } = useStatistics()
const { isConnected } = useWallet()
const activitiesCount = useActivitiesCount()

Expand Down
4 changes: 2 additions & 2 deletions dapp/src/pages/DashboardPage/AcreTVLProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(
() =>
Expand Down
2 changes: 1 addition & 1 deletion dapp/src/pages/DashboardPage/PositionDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function PositionDetails() {
const { data: activities } = useActivities()
const isMobileMode = useMobileMode()

const { tvl } = useEnhancedStatistics()
const { tvl } = useStatistics()

const { isConnected } = useWallet()

Expand Down

0 comments on commit 0d5d3e8

Please sign in to comment.