-
Notifications
You must be signed in to change notification settings - Fork 22
/
useAutoRefreshData.ts
48 lines (42 loc) · 1.34 KB
/
useAutoRefreshData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { useQueryClient } from '@tanstack/react-query'
import { useEffect } from 'react'
import { useSetRecoilState } from 'recoil'
import { chainQueries } from '@dao-dao/state/query'
import {
refreshBlockHeightAtom,
refreshIndexerUpStatusAtom,
refreshTokenUsdcPriceAtom,
} from '@dao-dao/state/recoil'
/**
* Refresh data every minute:
* - block height
* - prices
* - indexer status
* - dynamic gas prices
*/
export const useAutoRefreshData = () => {
const setRefreshBlockHeight = useSetRecoilState(refreshBlockHeightAtom)
const setRefreshUsdcPrices = useSetRecoilState(refreshTokenUsdcPriceAtom(''))
const setRefreshIndexerStatus = useSetRecoilState(refreshIndexerUpStatusAtom)
const queryClient = useQueryClient()
useEffect(() => {
const interval = setInterval(() => {
setRefreshBlockHeight((id) => id + 1)
setRefreshUsdcPrices((id) => id + 1)
setRefreshIndexerStatus((id) => id + 1)
queryClient.refetchQueries({
queryKey: chainQueries
.dynamicGasPrice({ chainId: '' })
// Remove the final parameter in the key (options) so we match the
// query key for all chains.
.queryKey.slice(0, -1),
})
}, 60 * 1000)
return () => clearInterval(interval)
}, [
queryClient,
setRefreshBlockHeight,
setRefreshIndexerStatus,
setRefreshUsdcPrices,
])
}