Skip to content

Commit

Permalink
perf: reduce call some APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tien Nam Dao committed Feb 22, 2023
1 parent 60b46e9 commit b20c7c7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pages/charts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Charts: React.FC<NextPage> = _ => {
</Head>
<Container>
<div className="col md-wrap" style={{ paddingTop: '150px' }}>
<Overview />
<Overview hasFetchLatestBlock />
<Transactions />
<Address />
<Gas />
Expand Down
8 changes: 7 additions & 1 deletion slices/commonSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { RootState } from 'store'

// Define a type for the slice state
export interface CommonState {
latestBlock: number
astraSummary: AstraSummary | undefined
validatorSummary: ValidatorData[] | []
}
// Define the initial state using that type
const initialState: CommonState = {
latestBlock: 0,
astraSummary: undefined,
validatorSummary: []
}
Expand All @@ -16,6 +18,9 @@ export const commonSlice = createSlice({
name: 'common',
initialState,
reducers: {
setLatestBlock: (state, action: PayloadAction<number>) => {
state.latestBlock = action.payload
},
setAstraSummary: (state, action: PayloadAction<AstraSummary>) => {
state.astraSummary = action.payload
},
Expand All @@ -26,10 +31,11 @@ export const commonSlice = createSlice({
}
})

export const getLatestBlock = (state: RootState): number => state['common'].latestBlock
export const getAstraSummary = (state: RootState): AstraSummary => state['common'].astraSummary
export const getValidatorSummary = (state: RootState): ValidatorData[] => state['common'].validatorSummary

// Action creators are generated for each case reducer function
export const { setAstraSummary, setValidatorSummary } = commonSlice.actions
export const { setLatestBlock, setAstraSummary, setValidatorSummary } = commonSlice.actions

export default commonSlice.reducer
7 changes: 7 additions & 0 deletions views/block/hook/useBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import API_LIST from 'api/api_list'
import usePagination from 'hooks/usePagination'
import { differenceWith, isEmpty } from 'lodash'
import { useCallback, useEffect, useState } from 'react'
import { useDispatch } from 'react-redux'
import { setLatestBlock } from 'slices/commonSlice'
import useSWR from 'swr'

export default function useBlock() {
const dispatch = useDispatch()
const [_items, _setBlockItem] = useState<BlockItem[]>()
const { pagination, setPagination } = usePagination('/blocks')
const _fetchCondition = () => {
Expand Down Expand Up @@ -33,6 +36,10 @@ export default function useBlock() {

useEffect(() => {
if (data?.result) {
// Dispatch latest block for cache
// Do not call directly get eth-block-number -> It dumps BE
dispatch(setLatestBlock(data?.result?.[0].blockHeight + 1))

if (pagination.page === 1 && isEmpty(_items)) {
_setBlockItem(data?.result)
setPagination({
Expand Down
13 changes: 10 additions & 3 deletions views/homepage/MarketStatistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import API_LIST from 'api/api_list'
import clsx from 'clsx'
import StaticsCard from 'components/Card/Layout/StaticsCard'
import numeral from 'numeral'
import { getLatestBlock } from 'slices/commonSlice'
import useSWR from 'swr'
import { Icon } from 'utils/enum'
import { useAppSelector } from '../../store/hooks'

interface Props {
classes?: string
estimateCountedData: EstimateCountedInfo
commonStatsData: CommonStats
hasFetchLatestBlock?: boolean
}

function getLastestBlock(latestBlock: LatestBlock) {
return latestBlock ? parseInt(latestBlock.result.result) : undefined
}

const MarketStatistics = ({ classes, commonStatsData, estimateCountedData }: Props) => {
const MarketStatistics = ({ classes, hasFetchLatestBlock, commonStatsData, estimateCountedData }: Props) => {
const { isMobile } = useMobileLayout('small')
const _fetchCondition = key => {
switch (key) {
Expand All @@ -27,10 +30,14 @@ const MarketStatistics = ({ classes, commonStatsData, estimateCountedData }: Pro
const { data: latestBlockRaw, error: latestBlockError } = useSWR<LatestBlock>(_fetchCondition('latest_block'), {
refreshInterval: 2000
})
const latestBlock = getLastestBlock(latestBlockRaw)

let latestBlock = useAppSelector(getLatestBlock)
if (hasFetchLatestBlock) {
latestBlock = getLastestBlock(latestBlockRaw)
}

return (
<div>
<div className={classes}>
<div className={clsx('row md-wrap', !isMobile && 'margin-bottom-2xl')}>
<StaticsCard
classes={isMobile ? 'margin-bottom-lg' : 'margin-right-xl'}
Expand Down
18 changes: 13 additions & 5 deletions views/homepage/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function getGasAvgData(gasAvgData): GasTracker {
return gasAvgData
}

const Overview = () => {
const Overview = ({ hasFetchLatestBlock = false }) => {
const { isMobile } = useMobileLayout(1112)

const _fetchCondition = key => {
Expand All @@ -40,12 +40,16 @@ const Overview = () => {
}
}
const { data: estimateCountedDataRaw, error: estimateCountedError } = useSWR<EstimateCountedResponse>(
_fetchCondition('estimate_counted_info')
_fetchCondition('estimate_counted_info'),
{ refreshInterval: 60000 }
)
const { data: commonStatsDataRaw, error: commonStatsError } = useSWR<CommonStatsResponse>(
_fetchCondition('common-stats')
_fetchCondition('common-stats'),
{ refreshInterval: 60000 }
)
const { data: gasAvgRaw, error: gasAvgError } = useSWR<GasTrackerResponse>(_fetchCondition('gas_avg'))
const { data: gasAvgRaw, error: gasAvgError } = useSWR<GasTrackerResponse>(_fetchCondition('gas_avg'), {
refreshInterval: 600000
})

const estimateCountedData = getEstimateCountedData(estimateCountedDataRaw?.result)
const commonStatsData = getCommonStatsData(commonStatsDataRaw?.result)
Expand All @@ -69,7 +73,11 @@ const Overview = () => {
<Price />
</Row>
<div className={styles.separateBlock} />
<MarketStatistics estimateCountedData={estimateCountedData} commonStatsData={commonStatsData} />
<MarketStatistics
hasFetchLatestBlock={hasFetchLatestBlock}
estimateCountedData={estimateCountedData}
commonStatsData={commonStatsData}
/>
</BackgroundCard>
{!isMobile && (
<div className={clsx('col', styles.centerBlock)}>
Expand Down

0 comments on commit b20c7c7

Please sign in to comment.