From f6f219f022cda8fa29638be3e5b6eb06cbc47530 Mon Sep 17 00:00:00 2001 From: Fuxing Loh Date: Fri, 5 Jul 2024 17:23:18 +0800 Subject: [PATCH] chore: move SupplyStats logic to client side and remove state refresh --- src/components/index/StatCard.tsx | 39 -------- src/components/index/SupplyStats.tsx | 130 ++++++++++++++++----------- src/pages/index.page.tsx | 28 ++---- 3 files changed, 86 insertions(+), 111 deletions(-) delete mode 100644 src/components/index/StatCard.tsx diff --git a/src/components/index/StatCard.tsx b/src/components/index/StatCard.tsx deleted file mode 100644 index 7462e57fd..000000000 --- a/src/components/index/StatCard.tsx +++ /dev/null @@ -1,39 +0,0 @@ -import { DFI as DfiIcon } from "@components/icons/assets/tokens/DFI"; -import { NumericFormat } from "react-number-format"; - -export function StatPriceCard(props: { - usd: number | undefined; - updatedAt: string | undefined; -}): JSX.Element { - return ( -
-
-
- -
$DFI Price
-
-
- -
-
-
- {props.updatedAt !== undefined && `Updated at ${props.updatedAt}`} -
-
- ); -} diff --git a/src/components/index/SupplyStats.tsx b/src/components/index/SupplyStats.tsx index 42175b671..74e1cc0da 100644 --- a/src/components/index/SupplyStats.tsx +++ b/src/components/index/SupplyStats.tsx @@ -2,55 +2,49 @@ import { PropsWithChildren, useEffect, useState } from "react"; import { InfoHoverPopover } from "@components/commons/popover/InfoHoverPopover"; import { NumericFormat } from "react-number-format"; import { CollapsibleSection } from "@components/commons/sections/CollapsibleSection"; -import { RootState } from "@store/index"; -import { StatPriceCard } from "@components/index/StatCard"; -import { useSelector } from "react-redux"; import { StatsData, SupplyData, } from "@defichain/whale-api-client/dist/api/stats"; -import { StatsState } from "@store/stats"; +import { useWhaleApiClient } from "@contexts/WhaleContext"; +import { DFI as DfiIcon } from "@components/icons/assets/tokens/DFI"; import { CalculatePercentage } from "../../utils/index/CalculatePercentage"; -interface SupplyStatsProps { - stats: StatsData; - supply: SupplyData; -} - interface SupplyStatsStateI { - stats: StatsData | StatsState; - supply: SupplyData; + stats: StatsData | null; + supply: SupplyData | null; } -export function SupplyStats(props: SupplyStatsProps): JSX.Element { - const statsState = useSelector((state: RootState) => state.stats); - const supplyState = useSelector((state: RootState) => state.supply); +export function SupplyStats(): JSX.Element { + const apiClient = useWhaleApiClient(); const [data, setData] = useState({ - stats: props.stats, - supply: props.supply, + stats: null, + supply: null, }); useEffect(() => { - if (statsState?.price?.usd !== undefined && supplyState?.total !== 0) { - setData({ - stats: statsState, - supply: supplyState, - }); - } - }, [statsState, supplyState]); + apiClient.stats.get().then((stats) => { + setData((prev) => ({ + ...prev, + stats, + })); + }); + + apiClient.stats.getSupply().then((supply) => { + setData((prev) => ({ + ...prev, + supply, + })); + }); + }, []); return (
- +
- {CalculatePercentage(data.supply.total, data.supply.max)} + {CalculatePercentage(data.supply?.total, data.supply?.max)} of max supply
@@ -73,15 +67,15 @@ export function SupplyStats(props: SupplyStatsProps): JSX.Element {
{CalculatePercentage( - data.supply.circulating, - data.supply.total + data.supply?.circulating, + data.supply?.total, )} of total minted @@ -90,7 +84,7 @@ export function SupplyStats(props: SupplyStatsProps): JSX.Element { @@ -99,8 +93,8 @@ export function SupplyStats(props: SupplyStatsProps): JSX.Element { DEX: {CalculatePercentage( - data.stats.tvl.dex, - data.stats.tvl.total + data.stats?.tvl.dex, + data.stats?.tvl.total, )}
@@ -109,8 +103,8 @@ export function SupplyStats(props: SupplyStatsProps): JSX.Element { MN: {CalculatePercentage( - data.stats.tvl.masternodes, - data.stats.tvl.total + data.stats?.tvl.masternodes, + data.stats?.tvl.total, )}
@@ -118,8 +112,8 @@ export function SupplyStats(props: SupplyStatsProps): JSX.Element { Vaults: {CalculatePercentage( - data.stats.tvl.loan, - data.stats.tvl.total + data.stats?.tvl.loan, + data.stats?.tvl.total, )}
@@ -128,12 +122,12 @@ export function SupplyStats(props: SupplyStatsProps): JSX.Element {
- {CalculatePercentage(data.supply.burned, data.supply.total)} + {CalculatePercentage(data.supply?.burned, data.supply?.total)} of total minted
@@ -153,7 +147,7 @@ function StatCard( prefix?: string; suffix?: string; testId: string; - }> + }>, ): JSX.Element { return (
@@ -165,14 +159,18 @@ function StatCard(
- + {props.stat !== undefined ? ( + + ) : ( + "..." + )} {props.suffix !== undefined && ( {props.suffix} )} @@ -182,3 +180,31 @@ function StatCard(
); } + +function StatPriceCard(props: { usd: number | undefined }): JSX.Element { + return ( +
+
+
+ +
$DFI Price
+
+
+ +
+
+
+ ); +} diff --git a/src/pages/index.page.tsx b/src/pages/index.page.tsx index ef374d329..1ea97c74d 100644 --- a/src/pages/index.page.tsx +++ b/src/pages/index.page.tsx @@ -14,15 +14,9 @@ import { BlocksList } from "@components/index/BlocksList"; import { TransactionsList } from "@components/index/TransactionsList"; import { useEffect, useState } from "react"; import { SupplyStats } from "@components/index/SupplyStats"; -import { - StatsData, - SupplyData, -} from "@defichain/whale-api-client/dist/api/stats"; interface HomePageProps { blocks: Block[]; - stats: StatsData; - supply: SupplyData; transactions: Transaction[]; liquidityPools: PoolPairData[]; } @@ -34,7 +28,7 @@ interface HomePageStateI { } export default function HomePage( - props: InferGetServerSidePropsType + props: InferGetServerSidePropsType, ): JSX.Element { const api = useWhaleApiClient(); const [data, setData] = useState({ @@ -58,8 +52,8 @@ export default function HomePage( async (block) => await api.blocks.getTransactions(block.id, 8).then((results) => { return results; - }) - ) + }), + ), ).then((results) => { results.map((result) => transactions.push(...result)); }); @@ -78,7 +72,7 @@ export default function HomePage( <> - +
@@ -94,15 +88,11 @@ export default function HomePage( } export async function getServerSideProps( - context: GetServerSidePropsContext + context: GetServerSidePropsContext, ): Promise> { const api = getWhaleApiClient(context); - const [blocks, supply, stats] = await Promise.all([ - api.blocks.list(8), - api.stats.getSupply(), - api.stats.get(), - ]); + const blocks = await api.blocks.list(8); let transactions: Transaction[] = []; await Promise.all( @@ -110,8 +100,8 @@ export async function getServerSideProps( async (block) => await api.blocks.getTransactions(block.id, 8).then((results) => { return results; - }) - ) + }), + ), ).then((results) => { results.map((result) => transactions.push(...result)); }); @@ -122,8 +112,6 @@ export async function getServerSideProps( return { props: { blocks, - stats, - supply, transactions, liquidityPools, },