Skip to content

Commit c5563fd

Browse files
committed
feat: implement stats box with data fetching
1 parent 109c1d7 commit c5563fd

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

src/pages/what-is-ethereum.tsx

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { GetStaticProps } from "next"
1+
import { GetStaticProps, InferGetStaticPropsType } from "next"
22
import { useRouter } from "next/router"
33
import { SSRConfig, useTranslation } from "next-i18next"
44
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
5+
import { MdInfoOutline } from "react-icons/md"
56
import {
67
Box,
78
type BoxProps,
@@ -10,19 +11,21 @@ import {
1011
type FlexProps,
1112
Heading,
1213
type HeadingProps,
14+
Icon,
1315
ListItem,
1416
UnorderedList,
1517
} from "@chakra-ui/react"
1618

17-
// import { GATSBY_FUNCTIONS_PATH } from "@/lib/constants" // TODO!
18-
// import useFetchStat, {
19-
// defaultFormatter,
20-
// IFetchStat,
21-
// } from "../hooks/useFetchStat" // TODO!
22-
import type { ChildOnlyProp, Lang } from "@/lib/types"
19+
import type { ChildOnlyProp, Lang, MetricReturnData } from "@/lib/types"
2320

2421
import AdoptionChart from "@/components/AdoptionChart"
25-
import { Banner, BannerImage } from "@/components/BannerGrid"
22+
import {
23+
Banner,
24+
BannerBody,
25+
BannerGrid,
26+
BannerGridCell,
27+
BannerImage,
28+
} from "@/components/BannerGrid"
2629
import Button from "@/components/Buttons/Button"
2730
import ButtonLink from "@/components/Buttons/ButtonLink"
2831
import Callout from "@/components/Callout"
@@ -36,16 +39,20 @@ import Text from "@/components/OldText"
3639
import PageMetadata from "@/components/PageMetadata"
3740
import { StandaloneQuizWidget } from "@/components/Quiz/QuizWidget"
3841
import Slider, { EmblaSlide } from "@/components/Slider"
42+
import StatErrorMessage from "@/components/StatErrorMessage"
3943
import Tabs from "@/components/Tabs"
44+
import Tooltip from "@/components/Tooltip"
4045
import Translation from "@/components/Translation"
4146

4247
import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
4348
import { trackCustomEvent } from "@/lib/utils/matomo"
49+
import { runOnlyOnce } from "@/lib/utils/runOnlyOnce"
4450
import {
4551
getLocaleForNumberFormat,
4652
getRequiredNamespacesForPage,
4753
} from "@/lib/utils/translations"
4854

55+
import { fetchTxCount } from "@/lib/api/fetchTxCount"
4956
import developers from "@/public/developers-eth-blocks.png"
5057
import community from "@/public/enterprise.png"
5158
import diffEthAndBtc from "@/public/eth.png"
@@ -163,18 +170,6 @@ const ButtonRow = (props: ChildOnlyProp) => (
163170
<Flex align="center" mt={4} mb={6} wrap="wrap" gap={4} {...props} />
164171
)
165172

166-
// const Stat: React.FC<{ stat: IFetchStat }> = ({ stat }) => {
167-
// const isLoading = !stat.value
168-
169-
// return stat.hasError ? (
170-
// <StatErrorMessage fontSize="1rem" />
171-
// ) : isLoading ? (
172-
// <StatLoadingMessage fontSize="1rem" />
173-
// ) : (
174-
// <>{stat.formattedValue}</>
175-
// )
176-
// }
177-
178173
const NoWrapText = (props: ChildOnlyProp) => (
179174
<Text as="span" whiteSpace="nowrap" {...props} />
180175
)
@@ -183,9 +178,17 @@ const Image400 = ({ src }: Pick<ImageProps, "src">) => (
183178
<Image src={src} alt="" width={400} />
184179
)
185180

181+
const cachedFetchTxCount = runOnlyOnce(fetchTxCount)
182+
183+
type Props = SSRConfig & {
184+
data: MetricReturnData
185+
}
186+
186187
export const getStaticProps = (async (context) => {
187188
const { locale } = context
188189

190+
const data = await cachedFetchTxCount()
191+
189192
// load i18n required namespaces for the given page
190193
const requiredNamespaces = getRequiredNamespacesForPage("/what-is-ethereum")
191194
const lastDeployDate = getLastDeployDate()
@@ -194,32 +197,27 @@ export const getStaticProps = (async (context) => {
194197
props: {
195198
...(await serverSideTranslations(locale!, requiredNamespaces)),
196199
lastDeployDate,
200+
data,
197201
},
198202
}
199-
}) satisfies GetStaticProps<SSRConfig>
203+
}) satisfies GetStaticProps<Props>
200204

201-
const WhatIsEthereumPage = () => {
205+
const WhatIsEthereumPage = ({
206+
data,
207+
}: InferGetStaticPropsType<typeof getStaticProps>) => {
202208
const { t } = useTranslation(["page-what-is-ethereum", "learn-quizzes"])
203209

204210
const { locale } = useRouter()
211+
const localeForNumberFormat = getLocaleForNumberFormat(locale! as Lang)
212+
213+
const formatNumber = (value: number) =>
214+
new Intl.NumberFormat(localeForNumberFormat, {
215+
notation: "compact",
216+
minimumSignificantDigits: 3,
217+
maximumSignificantDigits: 4,
218+
}).format(value)
205219

206-
const localeForStatsBoxNumbers = getLocaleForNumberFormat(locale! as Lang)
207-
208-
// TODO! Fetch txCount from etherscan API
209-
// const txCount = useFetchStat<
210-
// Array<{ unixTimeStamp: string; transactionCount: number }>
211-
// >(
212-
// `${GATSBY_FUNCTIONS_PATH}/txs`,
213-
// (response) => {
214-
// return response
215-
// .map(({ unixTimeStamp, transactionCount }) => ({
216-
// timestamp: parseInt(unixTimeStamp) * 1000, // unix milliseconds
217-
// value: transactionCount,
218-
// }))
219-
// .sort((a, b) => a.timestamp - b.timestamp)
220-
// },
221-
// (value) => defaultFormatter(value, localeForStatsBoxNumbers)
222-
// )
220+
const txStat = "error" in data ? "" : formatNumber(data.value)
223221

224222
const cards = [
225223
{
@@ -451,11 +449,8 @@ const WhatIsEthereumPage = () => {
451449

452450
<Section>
453451
<Banner>
454-
{/* TODO: Update stats box */}
455-
{/* <BannerBody>
456-
<H2>
457-
{t("page-what-is-ethereum-ethereum-in-numbers-title")}
458-
</H2>
452+
<BannerBody>
453+
<H2>{t("page-what-is-ethereum-ethereum-in-numbers-title")}</H2>
459454
<BannerGrid>
460455
<BannerGridCell>
461456
<StatPrimary>4k+</StatPrimary>
@@ -554,8 +549,9 @@ const WhatIsEthereumPage = () => {
554549
</BannerGridCell>
555550
<BannerGridCell>
556551
<StatPrimary>
557-
<Stat stat={txCount} />
552+
{txStat || <StatErrorMessage fontSize="1rem" />}
558553
</StatPrimary>
554+
{/* TODO: Extract strings for translation */}
559555
<StatDescription>
560556
Number of transactions{" "}
561557
<NoWrapText>
@@ -574,7 +570,7 @@ const WhatIsEthereumPage = () => {
574570
</StatDescription>
575571
</BannerGridCell>
576572
</BannerGrid>
577-
</BannerBody> */}
573+
</BannerBody>
578574
<BannerImage>
579575
<Image400 src={stats} />
580576
</BannerImage>

0 commit comments

Comments
 (0)