2021-04-29 18:43:17 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2021-04-29 15:56:28 +00:00
|
|
|
import { Button } from 'react-bootstrap'
|
2021-04-12 18:05:09 +00:00
|
|
|
import useSWR from 'swr'
|
|
|
|
|
|
|
|
const fetcher = url => fetch(url).then(res => res.json())
|
|
|
|
|
|
|
|
export default function Price () {
|
2021-04-29 18:43:17 +00:00
|
|
|
const [asSats, setAsSats] = useState(undefined)
|
|
|
|
useEffect(() => {
|
|
|
|
setAsSats(localStorage.getItem('asSats'))
|
|
|
|
}, [])
|
2021-04-29 15:56:28 +00:00
|
|
|
|
2021-04-28 19:30:14 +00:00
|
|
|
const { data } = useSWR(
|
|
|
|
'https://api.coinbase.com/v2/prices/BTC-USD/spot',
|
|
|
|
fetcher,
|
|
|
|
{
|
|
|
|
refreshInterval: 30000
|
|
|
|
})
|
2021-04-12 18:05:09 +00:00
|
|
|
|
2021-06-17 18:51:55 +00:00
|
|
|
if (!data || !data.data) return null
|
2021-04-12 18:05:09 +00:00
|
|
|
|
2021-04-29 18:43:17 +00:00
|
|
|
const fixed = (n, f) => Number.parseFloat(n).toFixed(f)
|
|
|
|
const handleClick = () => {
|
|
|
|
if (asSats) {
|
|
|
|
localStorage.removeItem('asSats')
|
|
|
|
setAsSats(undefined)
|
|
|
|
} else {
|
|
|
|
localStorage.setItem('asSats', 'yep')
|
|
|
|
setAsSats('yep')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 15:56:28 +00:00
|
|
|
if (asSats) {
|
|
|
|
return (
|
|
|
|
<Button className='text-reset' onClick={handleClick} variant='link'>
|
2021-04-29 18:43:17 +00:00
|
|
|
{fixed(100000000 / data.data.amount, 0) + ' sats/$'}
|
2021-04-29 15:56:28 +00:00
|
|
|
</Button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Button className='text-reset' onClick={handleClick} variant='link'>
|
2021-04-29 18:43:17 +00:00
|
|
|
{'$' + fixed(data.data.amount, 2)}
|
2021-04-29 15:56:28 +00:00
|
|
|
</Button>
|
|
|
|
)
|
2021-04-12 18:05:09 +00:00
|
|
|
}
|