Skip to content

Commit

Permalink
Refactored chart cache from localstroage-based to http-based (#135)
Browse files Browse the repository at this point in the history
* refactor: remove the local caching mechanism for the chart

* refactor: remove the unnecessary hook useChartQueryWithCache

* refactor: use a safer key for the chart query
  • Loading branch information
WhiteMinds authored Nov 10, 2023
1 parent 01495d0 commit 8cf08ed
Show file tree
Hide file tree
Showing 28 changed files with 45 additions and 120 deletions.
14 changes: 8 additions & 6 deletions src/pages/Home/AverageBlockTimeChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import 'echarts/lib/chart/line'
import 'echarts/lib/component/title'
import echarts from 'echarts/lib/echarts'
import { useTranslation } from 'react-i18next'
import { useQuery } from '@tanstack/react-query'
import { parseDateNoTime } from '../../../utils/date'
import { localeNumberString } from '../../../utils/number'
import SmallLoading from '../../../components/Loading/SmallLoading'
import { HomeChartLink, ChartLoadingPanel } from './styled'
import ChartNoDataImage from '../../../assets/chart_no_data_white.png'
import { useChartQueryWithCache, useIsLGScreen } from '../../../utils/hook'
import { useIsLGScreen } from '../../../utils/hook'
import { ChartItem, explorerService } from '../../../services/ExplorerService'
import { ReactChartCore } from '../../StatisticsChart/common'
import { AverageBlockTimeCacheKey } from '../../StatisticsChart/block/AverageBlockTime'

const useOption = () => {
const { t } = useTranslation()
Expand Down Expand Up @@ -109,10 +109,12 @@ export default memo(() => {
const isLG = useIsLGScreen()
const parseOption = useOption()

const query = useChartQueryWithCache(
explorerService.api.fetchStatisticAverageBlockTimes,
AverageBlockTimeCacheKey,
'date',
const query = useQuery(
['fetchStatisticAverageBlockTimes'],
() => explorerService.api.fetchStatisticAverageBlockTimes(),
{
refetchOnWindowFocus: false,
},
)
const fullStatisticAverageBlockTimes = useMemo(() => query.data ?? [], [query.data])

Expand Down
8 changes: 5 additions & 3 deletions src/pages/Home/HashRateChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import 'echarts/lib/chart/line'
import 'echarts/lib/component/title'
import echarts from 'echarts/lib/echarts'
import { useTranslation } from 'react-i18next'
import { useQuery } from '@tanstack/react-query'
import { handleAxis } from '../../../utils/chart'
import { parseDateNoTime } from '../../../utils/date'
import SmallLoading from '../../../components/Loading/SmallLoading'
import { HomeChartLink, ChartLoadingPanel } from './styled'
import ChartNoDataImage from '../../../assets/chart_no_data_white.png'
import { useChartQueryWithCache, useIsLGScreen } from '../../../utils/hook'
import { useIsLGScreen } from '../../../utils/hook'
import { ChartItem, explorerService } from '../../../services/ExplorerService'
import { ReactChartCore } from '../../StatisticsChart/common'
import { HashRateCacheKey } from '../../StatisticsChart/mining/HashRate'

const useOption = () => {
const { t } = useTranslation()
Expand Down Expand Up @@ -103,7 +103,9 @@ const useOption = () => {
}
export default memo(() => {
const isLG = useIsLGScreen()
const query = useChartQueryWithCache(explorerService.api.fetchStatisticHashRate, HashRateCacheKey, 'date')
const query = useQuery(['fetchStatisticHashRate'], () => explorerService.api.fetchStatisticHashRate(), {
refetchOnWindowFocus: false,
})
const fullStatisticHashRates = useMemo(() => query.data ?? [], [query.data])
const parseOption = useOption()

Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/activities/AddressBalanceRank.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ export const AddressBalanceRankChart = ({ isThumbnail = false }: { isThumbnail?:
onFetched={setStatisticAddressBalanceRanks}
getEChartOption={getEChartOption}
toCSV={toCSV}
cacheKey="AddressBalanceRank"
cacheMode="date"
queryKey="fetchStatisticAddressBalanceRank"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/activities/AddressCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ export const AddressCountChart = ({ isThumbnail = false }: { isThumbnail?: boole
fetchData={explorerService.api.fetchStatisticAddressCount}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="AddressCount"
cacheMode="date"
queryKey="fetchStatisticAddressCount"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/activities/BalanceDistribution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ export const BalanceDistributionChart = ({ isThumbnail = false }: { isThumbnail?
fetchData={explorerService.api.fetchStatisticBalanceDistribution}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="BalanceDistribution"
cacheMode="date"
queryKey="fetchStatisticBalanceDistribution"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/activities/CellCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ export const CellCountChart = ({ isThumbnail = false }: { isThumbnail?: boolean
fetchData={explorerService.api.fetchStatisticCellCount}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="CellCount"
cacheMode="date"
queryKey="fetchStatisticCellCount"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/activities/TransactionCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ export const TransactionCountChart = ({ isThumbnail = false }: { isThumbnail?: b
fetchData={explorerService.api.fetchStatisticTransactionCount}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="TransactionCount"
cacheMode="date"
queryKey="fetchStatisticTransactionCount"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/activities/TxFeeHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ export const TxFeeHistoryChart = ({ isThumbnail = false }: { isThumbnail?: boole
fetchData={explorerService.api.fetchStatisticTxFeeHistory}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="TransactionFee"
cacheMode="date"
queryKey="fetchStatisticTxFeeHistory"
/>
)
}
Expand Down
5 changes: 1 addition & 4 deletions src/pages/StatisticsChart/block/AverageBlockTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { ChartItem, explorerService } from '../../../services/ExplorerService'
import { useCurrentLanguage } from '../../../utils/i18n'
import { ChartColorConfig } from '../../../constants/common'

export const AverageBlockTimeCacheKey = 'AverageBlockTime'

const useOption = (
statisticAverageBlockTimes: ChartItem.AverageBlockTime[],
chartColor: ChartColorConfig,
Expand Down Expand Up @@ -183,8 +181,7 @@ export const AverageBlockTimeChart = ({ isThumbnail = false }: { isThumbnail?: b
fetchData={explorerService.api.fetchStatisticAverageBlockTimes}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey={AverageBlockTimeCacheKey}
cacheMode="date"
queryKey="fetchStatisticAverageBlockTimes"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/block/BlockTimeDistribution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ export const BlockTimeDistributionChart = ({ isThumbnail = false }: { isThumbnai
fetchData={explorerService.api.fetchStatisticBlockTimeDistribution}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="BlockTimeDistribution"
cacheMode="date"
queryKey="fetchStatisticBlockTimeDistribution"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/block/EpochTimeDistribution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ export const EpochTimeDistributionChart = ({ isThumbnail = false }: { isThumbnai
fetchData={explorerService.api.fetchStatisticEpochTimeDistribution}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="EpochTimeDistribution"
cacheMode="date"
queryKey="fetchStatisticEpochTimeDistribution"
/>
)
}
Expand Down
14 changes: 6 additions & 8 deletions src/pages/StatisticsChart/common/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import 'echarts/lib/component/brush'
import echarts from 'echarts/lib/echarts'
import { EChartOption, ECharts } from 'echarts'
import { useTranslation } from 'react-i18next'
import { useQuery } from '@tanstack/react-query'
import { LoadingPanel, ChartNoDataPanel, ChartDetailTitle, ChartDetailPanel, ChartNotePanel } from './styled'
import Loading from '../../../components/Loading'
import ChartNoDataImage from '../../../assets/chart_no_data.png'
import ChartNoDataAggronImage from '../../../assets/chart_no_data_aggron.png'
import { isMainnet } from '../../../utils/chain'
import SmallLoading from '../../../components/Loading/SmallLoading'
import Content from '../../../components/Content'
import { useChartQueryWithCache, useIsMobile, usePrevious, useWindowResize } from '../../../utils/hook'
import { useIsMobile, usePrevious, useWindowResize } from '../../../utils/hook'
import { isDeepEqual } from '../../../utils/util'
import { HelpTip } from '../../../components/HelpTip'
import { ChartColor, ChartColorConfig } from '../../../constants/common'
import { Response } from '../../../services/ExplorerService'

const LoadingComp = ({ isThumbnail }: { isThumbnail?: boolean }) => (isThumbnail ? <SmallLoading /> : <Loading show />)

Expand Down Expand Up @@ -161,7 +161,7 @@ export interface SmartChartPageProps<T> {
note?: string
isThumbnail?: boolean
chartProps?: Partial<ComponentProps<typeof ReactChartCore>>
fetchData: () => Promise<T[] | Response.Response<Response.Wrapper<T>[]>>
fetchData: () => Promise<T[]>
onFetched?: (dataList: T[]) => void
getEChartOption: (
dataList: T[],
Expand All @@ -170,8 +170,7 @@ export interface SmartChartPageProps<T> {
isThumbnail?: boolean,
) => echarts.EChartOption
toCSV: (dataList: T[]) => (string | number)[][]
cacheKey?: string
cacheMode?: 'forever' | 'date' | 'epoch'
queryKey?: string
}

export function SmartChartPage<T>({
Expand All @@ -184,12 +183,11 @@ export function SmartChartPage<T>({
onFetched,
getEChartOption,
toCSV,
cacheKey,
cacheMode = 'forever',
queryKey,
}: SmartChartPageProps<T>): ReactElement {
const isMobile = useIsMobile()

const query = useChartQueryWithCache(fetchData, cacheKey, cacheMode)
const query = useQuery(['SmartChartPage', queryKey], () => fetchData(), { refetchOnWindowFocus: false })
const dataList = useMemo(() => query.data ?? [], [query.data])
useEffect(() => {
if (onFetched && query.data) {
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/mining/Difficulty.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ export const DifficultyChart = ({ isThumbnail = false }: { isThumbnail?: boolean
fetchData={explorerService.api.fetchStatisticDifficulty}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="Difficulty"
cacheMode="date"
queryKey="fetchStatisticDifficulty"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/mining/DifficultyHashRate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ export const DifficultyHashRateChart = ({ isThumbnail = false }: { isThumbnail?:
fetchData={explorerService.api.fetchStatisticDifficultyHashRate}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="DifficultyHashRate"
cacheMode="epoch"
queryKey="fetchStatisticDifficultyHashRate"
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ export const DifficultyUncleRateEpochChart: FC<{ isThumbnail?: boolean }> = ({ i
fetchData={explorerService.api.fetchStatisticDifficultyUncleRateEpoch}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="DifficultyUncleRateEpoch"
cacheMode="epoch"
queryKey="fetchStatisticDifficultyUncleRateEpoch"
/>
)
}
Expand Down
5 changes: 1 addition & 4 deletions src/pages/StatisticsChart/mining/HashRate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { ChartItem, explorerService } from '../../../services/ExplorerService'
import { useCurrentLanguage } from '../../../utils/i18n'
import { ChartColorConfig } from '../../../constants/common'

export const HashRateCacheKey = 'HashRate'

const useOption = (
statisticHashRates: ChartItem.HashRate[],
chartColor: ChartColorConfig,
Expand Down Expand Up @@ -109,8 +107,7 @@ export const HashRateChart = ({ isThumbnail = false }: { isThumbnail?: boolean }
fetchData={explorerService.api.fetchStatisticHashRate}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey={HashRateCacheKey}
cacheMode="date"
queryKey="fetchStatisticHashRate"
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ export const MinerAddressDistributionChart = ({ isThumbnail = false }: { isThumb
fetchData={explorerService.api.fetchStatisticMinerAddressDistribution}
getEChartOption={getEChartOption}
toCSV={toCSV}
cacheKey="MinerAddressDistribution"
cacheMode="date"
queryKey="fetchStatisticMinerAddressDistribution"
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ export const MinerVersionDistributionChart = ({ isThumbnail = false }: { isThumb
fetchData={fetchData}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="MinerVersionDistribution"
cacheMode="date"
queryKey="fetchStatisticMinerVersionDistribution"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/mining/UncleRate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ export const UncleRateChart = ({ isThumbnail = false }: { isThumbnail?: boolean
fetchData={explorerService.api.fetchStatisticUncleRate}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="UncleRate"
cacheMode="date"
queryKey="fetchStatisticUncleRate"
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ export const AnnualPercentageCompensationChart = ({ isThumbnail = false }: { isT
fetchData={explorerService.api.fetchStatisticAnnualPercentageCompensation}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="APC"
cacheMode="forever"
queryKey="fetchStatisticAnnualPercentageCompensation"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/monetary/InflationRate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ export const InflationRateChart = ({ isThumbnail = false }: { isThumbnail?: bool
fetchData={explorerService.api.fetchStatisticInflationRate}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="InflationRate"
cacheMode="forever"
queryKey="fetchStatisticInflationRate"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/monetary/Liquidity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ export const LiquidityChart = ({ isThumbnail = false }: { isThumbnail?: boolean
fetchData={explorerService.api.fetchStatisticLiquidity}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="Liquidity"
cacheMode="date"
queryKey="fetchStatisticLiquidity"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/monetary/SecondaryIssuance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ export const SecondaryIssuanceChart = ({ isThumbnail = false }: { isThumbnail?:
fetchData={explorerService.api.fetchStatisticSecondaryIssuance}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="SecondaryIssuance"
cacheMode="date"
queryKey="fetchStatisticSecondaryIssuance"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/monetary/TotalSupply.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ export const TotalSupplyChart = ({ isThumbnail = false }: { isThumbnail?: boolea
fetchData={explorerService.api.fetchStatisticTotalSupply}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="TotalSupply"
cacheMode="date"
queryKey="fetchStatisticTotalSupply"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/nervosDao/CirculationRatio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ export const CirculationRatioChart = ({ isThumbnail = false }: { isThumbnail?: b
fetchData={explorerService.api.fetchStatisticCirculationRatio}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="DepositCirculationRatio"
cacheMode="date"
queryKey="fetchStatisticCirculationRatio"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/nervosDao/NewDaoDeposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ export const NewDaoDepositChart = ({ isThumbnail = false }: { isThumbnail?: bool
fetchData={explorerService.api.fetchStatisticNewDaoDeposit}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="DailyDeposit"
cacheMode="date"
queryKey="fetchStatisticNewDaoDeposit"
/>
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/StatisticsChart/nervosDao/TotalDaoDeposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ export const TotalDaoDepositChart = ({ isThumbnail = false }: { isThumbnail?: bo
fetchData={explorerService.api.fetchStatisticTotalDaoDeposit}
getEChartOption={useOption}
toCSV={toCSV}
cacheKey="TotalDeposit"
cacheMode="date"
queryKey="fetchStatisticTotalDaoDeposit"
/>
)
}
Expand Down
Loading

1 comment on commit 8cf08ed

@vercel
Copy link

@vercel vercel bot commented on 8cf08ed Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.