Skip to content

Commit bb15ffa

Browse files
committed
Clean up Render function value props
1 parent 0a2e00d commit bb15ffa

File tree

6 files changed

+60
-57
lines changed

6 files changed

+60
-57
lines changed

src/components/content/gastracker-page/gastracker-card.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export default function GastrackerCard({ title, pricesData }: {
1313
title: string,
1414
pricesData: DataState<any>,
1515
}) {
16-
let prices: Prices, gasPrice;
16+
let prices: Prices;
17+
let gasPrice: number | undefined;
18+
let gasPriceGwei: string, gasPriceUsd: string;
1719
if (pricesData.value) {
1820
prices = {
1921
ethPrice: +pricesData.value.coin_price,
@@ -23,6 +25,10 @@ export default function GastrackerCard({ title, pricesData }: {
2325
}
2426
gasPrice = Object.entries(prices).find( // find returns a key-value pair (value = [1])
2527
([key]) => key.startsWith(title.toLowerCase()) )?.[1]; // OR undefined => `?.` optional chaining required
28+
if (gasPrice) {
29+
gasPriceGwei = getGasPriceGwei(gasPrice);
30+
gasPriceUsd = getGasPriceUsd(gasPrice, prices.ethPrice);
31+
}
2632
}
2733

2834
let smiley, smileyLabel, colorClass;
@@ -55,11 +61,11 @@ export default function GastrackerCard({ title, pricesData }: {
5561
</p>
5662
<div className={`text-lg tracking-wide ${colorClass}`}>
5763
<p>
58-
<pricesData.Render value={ () => getGasPriceGwei(gasPrice!) } />
64+
<pricesData.Render value={ () => gasPriceGwei } />
5965
</p>
6066
<p className='text-sm'>
6167
<pricesData.Render showFallback={false}
62-
value={ () => getGasPriceUsd(gasPrice!, prices.ethPrice) } />
68+
value={ () => gasPriceUsd } />
6369
</p>
6470
</div>
6571
</div>

src/components/content/home-page/blocks/block-reward.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ export default function BlockReward(props: {
1919
args: [getBlockRewardUrl(props.network, props.blockNumber)],
2020
});
2121

22-
let blockReward = '';
23-
if (blockRewardData.value) blockReward = ${getEtherValueFromWei(blockRewardData.value.blockReward, 4)}`;
22+
const blockReward = blockRewardData.value ? ${getEtherValueFromWei(blockRewardData.value.blockReward, 4)}` : '';
2423

2524
let errorFallback = <ErrorWithRefetch refetch={blockRewardData.fetch} />;
2625
// Latest Block often doesn't have a reward yet:

src/components/content/home-page/blocks/block.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { useDataState, DataState } from '@/lib/data-state';
1212
import Link from 'next/link';
1313
import PopoverLink from '../../../common/popover-link';
1414
import LoadingPulseStatic from '@/components/common/indicators/loading-pulse-static';
15-
import ErrorWithRefetch from '@/components/common/indicators/error-with-refetch';
1615
import BlockReward from './block-reward';
1716

1817

@@ -29,6 +28,9 @@ export default function Block(props: {
2928
args: [alchemy, blockNumber],
3029
});
3130

31+
const timestamp = blockData.value ? `(${getBlockAgeFromSecs(getSecsFromUnixSecs(blockData.value.timestamp))} ago)` : undefined;
32+
const transactions = blockData.value ? `${blockData.value.transactions.length} transactions` : undefined;
33+
3234
return (
3335
<div className='min-h-[8.5rem] md:min-h-[5.8rem] p-2 md:p-3 border-b border-(--border-color) last:border-0'>
3436
<div className='flex flex-col md:flex-row'>
@@ -46,7 +48,7 @@ export default function Block(props: {
4648
</span>
4749
<span className='text-sm text-(--grey-fg-color)'>
4850
<blockData.Render
49-
value={() => `(${getBlockAgeFromSecs(getSecsFromUnixSecs(blockData.value!.timestamp))} ago)`}
51+
value={() => timestamp}
5052
className='w-[6em]'
5153
/>
5254
</span>
@@ -61,7 +63,7 @@ export default function Block(props: {
6163
/>
6264
<span className='px-2 md:px-4 leading-5'>
6365
<blockData.Render
64-
value={() => `${blockData.value!.transactions.length} transactions`}
66+
value={() => transactions}
6567
className='text-(--grey-fg-color) w-[8rem]'
6668
/>
6769
</span>
@@ -75,7 +77,7 @@ export default function Block(props: {
7577
<blockData.Render
7678
value={() =>
7779
<PopoverLink
78-
href={`/${props.network}/address?hash=${blockData.value!.miner}`}
80+
href={`/${props.network}/address?hash=${blockData.value?.miner}`}
7981
content={truncateAddress(blockData.value!.miner, 20)}
8082
popover={blockData.value!.miner}
8183
className='left-[-37%] top-[-2.6rem] w-78 py-1.5 px-2.5'

src/components/content/home-page/stats/index.tsx

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,59 @@ export default function Stats() {
3030
args: ['https://eth.blockscout.com/api/v2/stats/charts/market'],
3131
});
3232
const ethSupply = ethSupplyData.value ? +ethSupplyData.value.available_supply : undefined;
33+
const ethSupplyFormatted = ethSupply ?
34+
${ethSupply.toLocaleString('en-US', { maximumFractionDigits: 2, })}` : undefined;
3335

3436
const pricesAndTxsData = useDataState<PricesAndTxsData, [string]>({
3537
args: ['https://eth.blockscout.com/api/v2/stats'],
3638
});
3739

38-
let ethPrice: number | undefined = undefined;
39-
let averageGasPrice, transactionsToday, totalTransactions;
40+
let ethPrice: number | undefined;
41+
let ethPriceFormatted: string;
42+
let averageGasPrice: number;
43+
let averageGasPriceFormatted: string;
44+
let transactionsToday: string;
45+
let totalTransactions: string;
4046

4147
if (pricesAndTxsData.value) {
4248
ethPrice = +pricesAndTxsData.value.coin_price;
49+
ethPriceFormatted = ethPrice.toLocaleString('en-US', {
50+
style: 'currency',
51+
currency: 'USD',
52+
})
4353
averageGasPrice = +pricesAndTxsData.value.gas_prices.average;
54+
averageGasPriceFormatted = `${getGasPriceGwei(averageGasPrice)} ${getGasPriceUsd(averageGasPrice, ethPrice)}`
4455
transactionsToday = (+pricesAndTxsData.value.transactions_today).toLocaleString('en-US');
4556
totalTransactions = (+pricesAndTxsData.value.total_transactions).toLocaleString('en-US');
4657
}
4758

48-
// Since ethMarketCap is dependent on both fetches / DataStates, we need a new
59+
const ethMarketCapFormatted = (ethPrice && ethSupply) ?
60+
(ethPrice * ethSupply).toLocaleString('en-US', {
61+
style: 'currency',
62+
currency: 'USD',
63+
}) : undefined;
64+
65+
// Since ethMarketCapFormatted is dependent on both fetches / DataStates, we need a new
4966
// DataState for it to correctly render when it is in Error or Loading states.
50-
// Contrary to `useDataState`, `DataState.Init` just creates the (undefined) DataState
51-
// from the fetcher, without actually running the fetcher:
52-
let ethMarketCapData = DataState.useConfig<any>({
67+
// Contrary to `useDataState`, `DataState.useConfig()` just creates the (undefined) DataState
68+
// (LoadingRoot) from the fetcher, without actually running the fetcher:
69+
const ethMarketCapData = DataState.useConfig<any>({
5370
fetcher: async () => await Promise.all([pricesAndTxsData.fetch(), ethSupplyData.fetch()])
5471
});
5572

5673
// Give it a correct value if both fetches have already succeeded or an error if not:
5774
// (This requires `useEffect` because of setValue/setError)
5875
useEffect(() => {
59-
if (ethPrice && ethSupply) {
60-
ethMarketCapData.setValue([ethPrice, ethSupply]);
76+
if (ethMarketCapFormatted) {
77+
ethMarketCapData.setValue(ethMarketCapFormatted);
6178
}
6279
if (pricesAndTxsData.error || ethSupplyData.error) {
6380
ethMarketCapData.setError('Price or supply fetch failed');
6481
}
6582
// Dont include `ethMarketCapData` as a dependency as `react-hooks` says,
6683
// or it'll cause an infinite loop!
6784
// eslint-disable-next-line react-hooks/exhaustive-deps
68-
}, [ethPrice, ethSupply]);
85+
}, [ethMarketCapFormatted]);
6986

7087
return (
7188
<div className='border border-(--border-color) bg-(--comp-bg-color)
@@ -75,35 +92,21 @@ export default function Stats() {
7592
label='ETHER PRICE'
7693
icon={<CurrencyDollarIcon className='w-8 h-8' />}
7794
dataState={pricesAndTxsData}
78-
value={() =>
79-
ethPrice!.toLocaleString('en-US', {
80-
style: 'currency',
81-
currency: 'USD',
82-
})
83-
}
95+
value={() => ethPriceFormatted}
8496
className='md:border-b'
8597
/>
8698
<StatCard
8799
label='ETHER SUPPLY'
88100
icon={<div className='w-8 h-8 bg-(image:--eth-logo-url) bg-contain bg-no-repeat bg-center' />}
89101
dataState={ethSupplyData}
90-
value={() =>
91-
${ethSupply!.toLocaleString('en-US', {
92-
maximumFractionDigits: 2,
93-
})}`
94-
}
102+
value={() => ethSupplyFormatted }
95103
className='md:border-b md:border-x'
96104
/>
97105
<StatCard
98106
label='ETHER MARKET CAP'
99107
icon={<GlobeAltIcon className='w-8 h-8' />}
100108
dataState={ethMarketCapData}
101-
value={() =>
102-
(ethPrice! * ethSupply!).toLocaleString('en-US', {
103-
style: 'currency',
104-
currency: 'USD',
105-
})
106-
}
109+
value={() => ethMarketCapFormatted }
107110
className='md:border-b'
108111
/>
109112
</div>
@@ -116,22 +119,22 @@ export default function Stats() {
116119
value={() =>
117120
<Link href='/mainnet/gastracker'
118121
className='text-(--link-color) hover:text-(--hover-fg-color)'>
119-
{getGasPriceGwei(averageGasPrice!)} {getGasPriceUsd(averageGasPrice!, ethPrice!)}
122+
{averageGasPriceFormatted}
120123
</Link>
121124
}
122125
/>
123126
<StatCard
124127
label='TRANSACTIONS TODAY'
125128
icon={<ClipboardDocumentListIcon className='w-8 h-8' />}
126129
dataState={pricesAndTxsData}
127-
value={() => transactionsToday!}
130+
value={() => transactionsToday}
128131
className='md:border-x'
129132
/>
130133
<StatCard
131134
label='TOTAL TRANSACTIONS'
132135
icon={<Square3Stack3DIcon className='w-8 h-8' />}
133136
dataState={pricesAndTxsData}
134-
value={() => totalTransactions!}
137+
value={() => totalTransactions}
135138
/>
136139
</div>
137140
</div>

src/components/content/home-page/stats/stat-card.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { ReactNode } from 'react';
22
import { DataState } from '@/lib/data-state';
3-
import ErrorWithRefetch from '@/components/common/indicators/error-with-refetch';
4-
import LoadingIndicator from '@/components/common/indicators/loading-indicator';
53

64

75
export default function StatCard<T>(props: {

src/components/content/home-page/transactions/transaction.tsx

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client';
22

33
import { DocumentTextIcon } from '@heroicons/react/24/outline';
4-
import { BlockWithTransactions } from 'alchemy-sdk';
4+
import { BigNumber, BlockWithTransactions } from 'alchemy-sdk';
55
import { truncateAddress, truncateTransaction, getEtherValueFromWei } from '@/lib/utilities';
66
import PopoverLink from '@/components/common/popover-link';
77
import { DataState } from '@/lib/data-state';
@@ -13,13 +13,8 @@ export default function Transaction(props: {
1313
network: string,
1414
blockWithTransactions: DataState<BlockWithTransactions>,
1515
}) {
16-
// Use a callback because the transactions might still be loading:
1716
const transaction = props.blockWithTransactions.value?.transactions[props.id];
18-
19-
const hash = transaction?.hash;
20-
const ethValue = transaction?.value;
21-
const from = transaction?.from;
22-
const to = transaction?.to;
17+
const ethValue = transaction ? ${getEtherValueFromWei(transaction.value, 6)}` : undefined;
2318

2419
return (
2520
<div className='md:min-h-[4.825rem] p-2 md:p-3 border-b border-(--border-color) last:border-0'>
@@ -30,10 +25,10 @@ export default function Transaction(props: {
3025
<span className='px-2 md:px-4'>
3126
<props.blockWithTransactions.Render
3227
value={() =>
33-
<PopoverLink // runtime error: transaction() undefined!?
34-
href={`/${props.network}/transaction?hash=${hash}`}
35-
content={truncateTransaction(hash!, 18)}
36-
popover={hash!}
28+
<PopoverLink
29+
href={`/${props.network}/transaction?hash=${transaction?.hash}`}
30+
content={truncateTransaction(transaction!.hash, 18)}
31+
popover={transaction!.hash}
3732
className='-left-full top-[-2.6rem] w-120 py-1.5 px-2.5'
3833
/>}
3934
className='text-(--link-color) w-[10rem]'
@@ -47,7 +42,7 @@ export default function Transaction(props: {
4742
/>
4843
&nbsp;&nbsp;
4944
<props.blockWithTransactions.Render
50-
value={() => ${getEtherValueFromWei(ethValue!, 6)}`}
45+
value={() => ethValue}
5146
className='text-(--grey-fg-color) w-[3rem]'
5247
/>
5348
</span>
@@ -66,8 +61,8 @@ export default function Transaction(props: {
6661
value={() =>
6762
<PopoverLink
6863
href={`/${props.network}/address?hash=${transaction?.from}`}
69-
content={truncateAddress(transaction?.from!, 21)}
70-
popover={transaction?.from!}
64+
content={truncateAddress(transaction!.from, 21)}
65+
popover={transaction!.from}
7166
className='left-[-35%] top-[-2.6rem] w-78 py-1.5 px-2.5'
7267
/>}
7368
className='text-(--link-color) w-[11.25rem]'
@@ -83,9 +78,9 @@ export default function Transaction(props: {
8378
<props.blockWithTransactions.Render
8479
value={() => transaction?.to ?
8580
<PopoverLink
86-
href={`/${props.network}/address?hash=${transaction?.to}`}
87-
content={truncateAddress(transaction?.to!, 21)}
88-
popover={transaction?.to!}
81+
href={`/${props.network}/address?hash=${transaction.to}`}
82+
content={truncateAddress(transaction.to, 21)}
83+
popover={transaction.to}
8984
className='left-[-35%] top-[-2.6rem] w-78 py-1.5 px-2.5'
9085
/>
9186
: <span>/</span>}

0 commit comments

Comments
 (0)