2023-11-19 20:24:56 +00:00
|
|
|
import React, { useContext, useEffect, useMemo, useState } from 'react'
|
2023-01-27 23:20:33 +00:00
|
|
|
import { useQuery } from '@apollo/client'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { fixedDecimal } from '@/lib/format'
|
2022-09-12 23:42:09 +00:00
|
|
|
import { useMe } from './me'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { PRICE } from '@/fragments/price'
|
|
|
|
import { CURRENCY_SYMBOLS } from '@/lib/currency'
|
|
|
|
import { SSR } from '@/lib/constants'
|
2023-09-12 15:56:02 +00:00
|
|
|
import { useBlockHeight } from './block-height'
|
2023-12-20 22:06:22 +00:00
|
|
|
import { useChainFee } from './chain-fee'
|
2021-04-12 18:05:09 +00:00
|
|
|
|
2021-11-28 17:29:17 +00:00
|
|
|
export const PriceContext = React.createContext({
|
2023-01-27 23:20:33 +00:00
|
|
|
price: null,
|
|
|
|
fiatSymbol: null
|
2021-11-28 17:29:17 +00:00
|
|
|
})
|
|
|
|
|
2023-01-27 23:20:33 +00:00
|
|
|
export function usePrice () {
|
|
|
|
return useContext(PriceContext)
|
2021-11-28 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function PriceProvider ({ price, children }) {
|
2022-09-18 01:07:14 +00:00
|
|
|
const me = useMe()
|
2023-11-20 15:04:38 +00:00
|
|
|
const fiatCurrency = me?.privates?.fiatCurrency
|
2023-07-23 15:08:43 +00:00
|
|
|
const { data } = useQuery(PRICE, {
|
|
|
|
variables: { fiatCurrency },
|
2023-07-31 19:54:30 +00:00
|
|
|
...(SSR
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
pollInterval: 30000,
|
|
|
|
nextFetchPolicy: 'cache-and-network'
|
|
|
|
})
|
2023-07-23 15:08:43 +00:00
|
|
|
})
|
2021-04-12 18:05:09 +00:00
|
|
|
|
2023-11-19 20:24:56 +00:00
|
|
|
const contextValue = useMemo(() => ({
|
2023-01-27 23:20:33 +00:00
|
|
|
price: data?.price || price,
|
|
|
|
fiatSymbol: CURRENCY_SYMBOLS[fiatCurrency] || '$'
|
2023-11-20 15:04:38 +00:00
|
|
|
}), [data?.price, price, me?.privates?.fiatCurrency])
|
2021-11-28 17:29:17 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<PriceContext.Provider value={contextValue}>
|
|
|
|
{children}
|
|
|
|
</PriceContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-01 20:58:30 +00:00
|
|
|
export default function Price ({ className }) {
|
2021-11-28 17:29:17 +00:00
|
|
|
const [asSats, setAsSats] = useState(undefined)
|
2021-11-11 22:14:42 +00:00
|
|
|
useEffect(() => {
|
2023-11-19 21:01:12 +00:00
|
|
|
const satSelection = window.localStorage.getItem('asSats')
|
|
|
|
setAsSats(satSelection ?? 'fiat')
|
2021-11-28 17:29:17 +00:00
|
|
|
}, [])
|
2023-12-20 22:06:22 +00:00
|
|
|
|
2023-01-27 23:20:33 +00:00
|
|
|
const { price, fiatSymbol } = usePrice()
|
2023-09-12 15:56:02 +00:00
|
|
|
const { height: blockHeight } = useBlockHeight()
|
2023-12-20 22:06:22 +00:00
|
|
|
const { fee: chainFee } = useChainFee()
|
2021-11-11 22:14:42 +00:00
|
|
|
|
2023-12-20 22:06:22 +00:00
|
|
|
if (!price || price < 0 || blockHeight <= 0 || chainFee <= 0) return null
|
2021-04-12 18:05:09 +00:00
|
|
|
|
2023-09-12 15:56:02 +00:00
|
|
|
// Options: yep, 1btc, blockHeight, undefined
|
2023-12-20 22:06:22 +00:00
|
|
|
// yep -> 1btc -> blockHeight -> chainFee -> undefined -> yep
|
2021-04-29 18:43:17 +00:00
|
|
|
const handleClick = () => {
|
2021-12-05 17:37:55 +00:00
|
|
|
if (asSats === 'yep') {
|
2023-07-25 14:14:45 +00:00
|
|
|
window.localStorage.setItem('asSats', '1btc')
|
2021-12-05 17:37:55 +00:00
|
|
|
setAsSats('1btc')
|
|
|
|
} else if (asSats === '1btc') {
|
2023-09-12 15:56:02 +00:00
|
|
|
window.localStorage.setItem('asSats', 'blockHeight')
|
|
|
|
setAsSats('blockHeight')
|
|
|
|
} else if (asSats === 'blockHeight') {
|
2023-12-20 22:06:22 +00:00
|
|
|
window.localStorage.setItem('asSats', 'chainFee')
|
|
|
|
setAsSats('chainFee')
|
|
|
|
} else if (asSats === 'chainFee') {
|
2023-07-25 14:14:45 +00:00
|
|
|
window.localStorage.removeItem('asSats')
|
2023-11-19 21:01:12 +00:00
|
|
|
setAsSats('fiat')
|
2021-04-29 18:43:17 +00:00
|
|
|
} else {
|
2023-07-25 14:14:45 +00:00
|
|
|
window.localStorage.setItem('asSats', 'yep')
|
2021-04-29 18:43:17 +00:00
|
|
|
setAsSats('yep')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-01 20:58:30 +00:00
|
|
|
const compClassName = (className || '') + ' text-reset pointer'
|
|
|
|
|
2021-12-05 17:37:55 +00:00
|
|
|
if (asSats === 'yep') {
|
2021-04-29 15:56:28 +00:00
|
|
|
return (
|
2023-05-01 20:58:30 +00:00
|
|
|
<div className={compClassName} onClick={handleClick} variant='link'>
|
2022-09-12 23:42:09 +00:00
|
|
|
{fixedDecimal(100000000 / price, 0) + ` sats/${fiatSymbol}`}
|
2023-05-01 20:58:30 +00:00
|
|
|
</div>
|
2021-04-29 15:56:28 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:37:55 +00:00
|
|
|
if (asSats === '1btc') {
|
|
|
|
return (
|
2023-05-01 20:58:30 +00:00
|
|
|
<div className={compClassName} onClick={handleClick} variant='link'>
|
2021-12-05 17:37:55 +00:00
|
|
|
1sat=1sat
|
2023-05-01 20:58:30 +00:00
|
|
|
</div>
|
2021-12-05 17:37:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-12 15:56:02 +00:00
|
|
|
if (asSats === 'blockHeight') {
|
|
|
|
return (
|
|
|
|
<div className={compClassName} onClick={handleClick} variant='link'>
|
2023-09-12 21:36:26 +00:00
|
|
|
{blockHeight}
|
2023-09-12 15:56:02 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-12-20 22:06:22 +00:00
|
|
|
if (asSats === 'chainFee') {
|
|
|
|
return (
|
|
|
|
<div className={compClassName} onClick={handleClick} variant='link'>
|
|
|
|
{chainFee} sat/vB
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-11-19 21:01:12 +00:00
|
|
|
if (asSats === 'fiat') {
|
|
|
|
return (
|
|
|
|
<div className={compClassName} onClick={handleClick} variant='link'>
|
|
|
|
{fiatSymbol + fixedDecimal(price, 0)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2021-04-12 18:05:09 +00:00
|
|
|
}
|