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