stacker.news/components/block-height.js
SatsAllDay 16a6f93708
Add block height to price carousel (#484)
* Add block height to price carousel

source block height from mempool.space API
https://mempool.space/docs/api/rest#get-block-tip-height

* Add block height to SSR, clean up fragment query

* Cache block height for 1 minute, not 30 seconds

use `numWithUnits` for block height label

* Replace mempool.space API with LND API call

---------

Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2023-09-12 10:56:02 -05:00

30 lines
750 B
JavaScript

import { createContext, useContext } 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 = {
height: data?.blockHeight ?? blockHeight ?? 0
}
return (
<BlockHeightContext.Provider value={value}>
{children}
</BlockHeightContext.Provider>
)
}