101 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-11-28 11:29:17 -06:00
import React, { useContext, useEffect, useState } from 'react'
2021-04-29 10:56:28 -05:00
import { Button } from 'react-bootstrap'
2021-04-12 13:05:09 -05:00
import useSWR from 'swr'
2022-07-30 08:25:46 -05:00
import { fixedDecimal } from '../lib/format'
2022-09-13 01:42:09 +02:00
import { useMe } from './me'
2021-04-12 13:05:09 -05:00
2022-07-12 11:40:44 -05:00
const fetcher = url => fetch(url).then(res => res.json()).catch()
2021-04-12 13:05:09 -05:00
2021-11-28 11:29:17 -06:00
export const PriceContext = React.createContext({
price: null
})
2022-09-13 01:42:09 +02:00
export const CURRENCY_SYMBOLS = {
AUD: '$',
CAD: '$',
EUR: '€',
GBP: '£',
USD: '$',
2022-10-21 16:28:49 +02:00
NZD: '$',
ZAR: 'R '
2022-09-13 01:42:09 +02:00
}
2022-09-18 03:07:14 +02:00
const endpoint = (fiat) => `https://api.coinbase.com/v2/prices/BTC-${fiat ?? 'USD'}/spot`
2021-04-29 10:56:28 -05:00
2022-09-13 01:42:09 +02:00
export async function getPrice (fiat) {
const data = await fetcher(endpoint(fiat))
2021-11-28 11:29:17 -06:00
return data?.data?.amount
}
export function PriceProvider ({ price, children }) {
2022-09-18 03:07:14 +02:00
const me = useMe()
2021-04-28 14:30:14 -05:00
const { data } = useSWR(
2022-09-18 03:07:14 +02:00
endpoint(me?.fiatCurrency),
2021-04-28 14:30:14 -05:00
fetcher,
{
refreshInterval: 30000
})
2021-04-12 13:05:09 -05:00
2021-11-28 11:29:17 -06:00
const contextValue = {
price: data?.data?.amount || price
}
return (
<PriceContext.Provider value={contextValue}>
{children}
</PriceContext.Provider>
)
}
export function usePrice () {
const { price } = useContext(PriceContext)
return price
}
export default function Price () {
const [asSats, setAsSats] = useState(undefined)
useEffect(() => {
2021-11-28 11:29:17 -06:00
setAsSats(localStorage.getItem('asSats'))
}, [])
const price = usePrice()
2022-09-18 03:07:14 +02:00
const me = useMe()
const fiatSymbol = CURRENCY_SYMBOLS[me?.fiatCurrency || 'USD']
2021-11-28 11:29:17 -06:00
if (!price) return null
2021-04-12 13:05:09 -05:00
2021-04-29 13:43:17 -05:00
const handleClick = () => {
2021-12-05 11:37:55 -06:00
if (asSats === 'yep') {
localStorage.setItem('asSats', '1btc')
setAsSats('1btc')
} else if (asSats === '1btc') {
2021-04-29 13:43:17 -05:00
localStorage.removeItem('asSats')
setAsSats(undefined)
} else {
localStorage.setItem('asSats', 'yep')
setAsSats('yep')
}
}
2021-12-05 11:37:55 -06:00
if (asSats === 'yep') {
2021-04-29 10:56:28 -05:00
return (
2022-03-08 14:51:06 -06:00
<Button className='text-reset p-0' onClick={handleClick} variant='link'>
2022-09-13 01:42:09 +02:00
{fixedDecimal(100000000 / price, 0) + ` sats/${fiatSymbol}`}
2021-04-29 10:56:28 -05:00
</Button>
)
}
2021-12-05 11:37:55 -06:00
if (asSats === '1btc') {
return (
2022-03-08 14:51:06 -06:00
<Button className='text-reset p-0' onClick={handleClick} variant='link'>
2021-12-05 11:37:55 -06:00
1sat=1sat
</Button>
)
}
2021-04-29 10:56:28 -05:00
return (
2022-03-08 14:51:06 -06:00
<Button className='text-reset p-0' onClick={handleClick} variant='link'>
2022-09-13 01:42:09 +02:00
{fiatSymbol + fixedDecimal(price, 0)}
2021-04-29 10:56:28 -05:00
</Button>
)
2021-04-12 13:05:09 -05:00
}