Skip to content

Commit a9b43da

Browse files
authored
Add toggle to auto refresh on validator line chart page (#47)
1 parent 474562a commit a9b43da

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/frontend/api/useStats.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ export const useStats = (query: {
2020
fetchCoinStats?: boolean
2121
transactionResponseType?: string | undefined
2222
validatorResponseType?: string | undefined
23+
refreshEnabled?: boolean
2324
}): StatsResult => {
2425
const {
2526
validatorStatsCount,
2627
transactionStatsCount,
2728
fetchCoinStats,
2829
transactionResponseType,
2930
validatorResponseType,
31+
refreshEnabled,
3032
} = query
3133

3234
// set query paths to `null` if we shouldn't fetch them
@@ -38,13 +40,28 @@ export const useStats = (query: {
3840
: null
3941
const coinStatsQuery = fetchCoinStats ? `${PATHS.STATS_COIN}` : null
4042

43+
const swrOptions = {
44+
refreshInterval: !refreshEnabled ? 0 : undefined,
45+
revalidateOnFocus: refreshEnabled,
46+
revalidateOnReconnect: refreshEnabled,
47+
}
48+
4149
// get responses
42-
const validatorStatsResponse = useSWR<{ validatorStats: ValidatorStats[] }>(validatorStatsQuery, fetcher)
50+
const validatorStatsResponse = useSWR<{ validatorStats: ValidatorStats[] }>(
51+
validatorStatsQuery,
52+
fetcher,
53+
swrOptions
54+
)
4355
const transactionStatsResponse = useSWR<{ transactionStats: TransactionStats[] }>(
4456
transactionStatsQuery,
45-
fetcher
57+
fetcher,
58+
swrOptions
59+
)
60+
const coinStatsResponse = useSWR<{ totalSupply: number; totalStaked: number }>(
61+
coinStatsQuery,
62+
fetcher,
63+
swrOptions
4664
)
47-
const coinStatsResponse = useSWR<{ totalSupply: number; totalStaked: number }>(coinStatsQuery, fetcher)
4865

4966
// get values
5067
const validatorStats =

src/frontend/validator-line-chart/ValidatorLineChart.tsx

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useState } from 'react'
22

33
import { ContentLayout, StackedLineStockChart } from '../components'
44

@@ -7,15 +7,31 @@ import { useStats } from '../api'
77
import { convertValidatorStatsToSeriesData } from '../utils/transformChartData'
88

99
export const ValidatorLineChart: React.FC = () => {
10+
const [refreshEnabled, setRefreshDisabled] = useState(true)
1011
const height = 600
1112

1213
const { validatorStats, loading } = useStats({
1314
validatorStatsCount: 10000000,
15+
refreshEnabled,
1416
})
1517

18+
const toggleNoRefresh = (): void => setRefreshDisabled((prev) => !prev)
19+
1620
return (
1721
<div className={styles.ValidatorLineChart}>
1822
<ContentLayout title="Active Validators per Cycle Chart">
23+
<div style={{ marginBottom: '1rem' }}>
24+
<label>
25+
<input
26+
type="checkbox"
27+
checked={refreshEnabled}
28+
onChange={toggleNoRefresh}
29+
style={{ appearance: 'auto' }}
30+
/>
31+
<span> Auto Refresh</span>
32+
</label>
33+
</div>
34+
1935
{loading ? (
2036
<div>Loading...</div>
2137
) : (

0 commit comments

Comments
 (0)