stacker.news/components/block-height.js
SatsAllDay 91a0d1ccd7
env vars for polling intervals (#1038)
* env vars for polling intervals

add env vars for 4 different common polling intervals,
fast (1000), normal (30000), long (60000), extra long (300000)

use env vars in all `pollInterval` params to `useQuery`

* replace `setInterval`'s interval with `FAST_POLL_INTERVAL`
2024-04-08 09:13:12 -05:00

30 lines
845 B
JavaScript

import { createContext, useContext, useMemo } from 'react'
import { useQuery } from '@apollo/client'
import { NORMAL_POLL_INTERVAL, SSR } from '@/lib/constants'
import { BLOCK_HEIGHT } from '@/fragments/blockHeight'
export const BlockHeightContext = createContext({
height: 0
})
export const useBlockHeight = () => useContext(BlockHeightContext)
export const BlockHeightProvider = ({ blockHeight, children }) => {
const { data } = useQuery(BLOCK_HEIGHT, {
...(SSR
? {}
: {
pollInterval: NORMAL_POLL_INTERVAL,
nextFetchPolicy: 'cache-and-network'
})
})
const value = useMemo(() => ({
height: data?.blockHeight ?? blockHeight ?? 0
}), [data?.blockHeight, blockHeight])
return (
<BlockHeightContext.Provider value={value}>
{children}
</BlockHeightContext.Provider>
)
}