stacker.news/components/block-height.js
ekzyis d237861ff5
Use module path aliases (#938)
* Use module path aliases

* fix broken refactor

* path mapping for svgs, style, and remaining places (bonus: lose babel dep)

---------

Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
2024-03-19 19:37:31 -05:00

30 lines
808 B
JavaScript

import { createContext, useContext, useMemo } from 'react'
import { useQuery } from '@apollo/client'
import { 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: 30000,
nextFetchPolicy: 'cache-and-network'
})
})
const value = useMemo(() => ({
height: data?.blockHeight ?? blockHeight ?? 0
}), [data?.blockHeight, blockHeight])
return (
<BlockHeightContext.Provider value={value}>
{children}
</BlockHeightContext.Provider>
)
}