|
1 |
| -import { useMemo } from "react"; |
| 1 | +import { useEffect, useMemo, useState } from "react"; |
2 | 2 |
|
3 | 3 | import { DEFAULT_CHAIN } from "consts/chains";
|
4 |
| -import { getOneWeekAgoTimestamp } from "utils/date"; |
5 | 4 |
|
6 | 5 | import { HomePageBlockQuery } from "src/graphql/graphql";
|
7 | 6 | import { isUndefined } from "src/utils";
|
8 | 7 |
|
9 |
| -import { useBlockByTimestamp } from "../useBlockByTimestamp"; |
10 | 8 | import { useHomePageContext } from "../useHomePageContext";
|
11 | 9 |
|
12 | 10 | import { useHomePageBlockQuery } from "./useHomePageBlockQuery";
|
| 11 | +import { useBlockNumber } from "wagmi"; |
| 12 | +import { averageBlockTimeInSeconds } from "consts/averageBlockTimeInSeconds"; |
13 | 13 |
|
14 | 14 | type Court = HomePageBlockQuery["courts"][number];
|
15 | 15 |
|
@@ -39,12 +39,17 @@ export interface HomePageExtraStatsType {
|
39 | 39 |
|
40 | 40 | export const useHomePageExtraStats = (): HomePageExtraStatsType => {
|
41 | 41 | const { data } = useHomePageContext();
|
42 |
| - const { blockNumber } = useBlockByTimestamp( |
43 |
| - DEFAULT_CHAIN, |
44 |
| - useMemo(() => getOneWeekAgoTimestamp(), []) |
45 |
| - ); |
| 42 | + const [oneWeekAgoBlockNumber, setOneWeekAgoBlockNumber] = useState<number>(); |
| 43 | + const currentBlockNumber = useBlockNumber({ chainId: DEFAULT_CHAIN }); |
46 | 44 |
|
47 |
| - const { data: relData } = useHomePageBlockQuery(blockNumber!); |
| 45 | + useEffect(() => { |
| 46 | + if (currentBlockNumber?.data) { |
| 47 | + const oneWeekInBlocks = Math.floor((7 * 24 * 3600) / averageBlockTimeInSeconds[DEFAULT_CHAIN]); |
| 48 | + setOneWeekAgoBlockNumber(Number(currentBlockNumber.data) - oneWeekInBlocks); |
| 49 | + } |
| 50 | + }, [DEFAULT_CHAIN, currentBlockNumber]); |
| 51 | + |
| 52 | + const { data: relData } = useHomePageBlockQuery(oneWeekAgoBlockNumber!); |
48 | 53 |
|
49 | 54 | const HighestDrawingChance = useMemo(() => {
|
50 | 55 | return data ? getCourtWithMaxChance(data.courts).name ?? null : null;
|
|
0 commit comments