stacker.news/components/price.js

135 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

import React, { useContext, useEffect, useMemo, useState } from 'react'
import { useQuery } from '@apollo/client'
import { fixedDecimal } from '@/lib/format'
2022-09-12 23:42:09 +00:00
import { useMe } from './me'
import { PRICE } from '@/fragments/price'
import { CURRENCY_SYMBOLS } from '@/lib/currency'
import { NORMAL_POLL_INTERVAL, SSR } from '@/lib/constants'
import { useBlockHeight } from './block-height'
import { useChainFee } from './chain-fee'
2024-04-16 22:58:26 +00:00
import { CompactLongCountdown } from './countdown'
2021-04-12 18:05:09 +00:00
2021-11-28 17:29:17 +00:00
export const PriceContext = React.createContext({
price: null,
fiatSymbol: null
2021-11-28 17:29:17 +00:00
})
export function usePrice () {
return useContext(PriceContext)
2021-11-28 17:29:17 +00:00
}
export function PriceProvider ({ price, children }) {
Account Switching (#644) * WIP: Account switching * Fix empty USER query ANON_USER_ID was undefined and thus the query for @anon had no variables. * Apply multiAuthMiddleware in /api/graphql * Fix 'you must be logged in' query error on switch to anon * Add smart 'switch account' button "smart" means that it only shows if there are accounts to which one can switch * Fix multiAuth not set in backend * Comment fixes, minor changes * Use fw-bold instead of 'selected' * Close dropdown and offcanvas Inside a dropdown, we can rely on autoClose but need to wrap the buttons with <Dropdown.Item> for that to work. For the offcanvas, we need to pass down handleClose. * Use button to add account * Some pages require hard reload on account switch * Reinit settings form on account switch * Also don't refetch WalletHistory * Formatting * Use width: fit-content for standalone SignUpButton * Remove unused className * Use fw-bold and text-underline on selected * Fix inconsistent padding of login buttons * Fix duplicate redirect from /settings on anon switch * Never throw during refetch * Throw errors which extend GraphQLError * Only use meAnonSats if logged out * Use reactive variable for meAnonSats The previous commit broke the UI update after anon zaps because we actually updated item.meSats in the cache and not item.meAnonSats. Updating item.meAnonSats was not possible because it's a local field. For that, one needs to use reactive variables. We do this now and thus also don't need the useEffect hack in item-info.js anymore. * Switch to new user * Fix missing cleanup during logout If we logged in but never switched to any other account, the 'multi_auth.user-id' cookie was not set. This meant that during logout, the other 'multi_auth.*' cookies were not deleted. This broke the account switch modal. This is fixed by setting the 'multi_auth.user-id' cookie on login. Additionally, we now cleanup if cookie pointer OR session is set (instead of only if both are set). * Fix comments in middleware * Remove unnecessary effect dependencies setState is stable and thus only noise in effect dependencies * Show but disable unavailable auth methods * make signup button consistent with others * Always reload page on switch * refine account switch styling * logout barrier --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: k00b <k00b@stacker.news>
2024-09-12 18:05:11 +00:00
const { me } = useMe()
const fiatCurrency = me?.privates?.fiatCurrency
const { data } = useQuery(PRICE, {
variables: { fiatCurrency },
...(SSR
? {}
: {
pollInterval: NORMAL_POLL_INTERVAL,
nextFetchPolicy: 'cache-and-network'
})
})
2021-04-12 18:05:09 +00:00
const contextValue = useMemo(() => ({
price: data?.price || price,
fiatSymbol: CURRENCY_SYMBOLS[fiatCurrency] || '$'
}), [data?.price, price, me?.privates?.fiatCurrency])
2021-11-28 17:29:17 +00:00
return (
<PriceContext.Provider value={contextValue}>
{children}
</PriceContext.Provider>
)
}
const STORAGE_KEY = 'asSats'
const DEFAULT_SELECTION = 'fiat'
const carousel = [
'fiat',
'yep',
'1btc',
'blockHeight',
'chainFee',
'halving'
]
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)
const [pos, setPos] = useState(0)
useEffect(() => {
const selection = window.localStorage.getItem(STORAGE_KEY) ?? DEFAULT_SELECTION
setAsSats(selection)
setPos(carousel.findIndex((item) => item === selection))
2021-11-28 17:29:17 +00:00
}, [])
const { price, fiatSymbol } = usePrice()
2024-04-16 22:58:26 +00:00
const { height: blockHeight, halving } = useBlockHeight()
const { fee: chainFee } = useChainFee()
2021-04-29 18:43:17 +00:00
const handleClick = () => {
const nextPos = (pos + 1) % carousel.length
window.localStorage.setItem(STORAGE_KEY, carousel[nextPos])
setAsSats(carousel[nextPos])
setPos(nextPos)
2021-04-29 18:43:17 +00:00
}
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') {
2024-04-16 22:58:26 +00:00
if (!price || price < 0) return null
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
)
}
if (asSats === 'blockHeight') {
2024-04-16 22:58:26 +00:00
if (blockHeight <= 0) return null
return (
<div className={compClassName} onClick={handleClick} variant='link'>
2023-09-12 21:36:26 +00:00
{blockHeight}
</div>
)
}
2024-04-16 22:58:26 +00:00
if (asSats === 'halving') {
if (!halving) return null
return (
<div className={compClassName} onClick={handleClick} variant='link'>
<CompactLongCountdown date={halving} />
</div>
)
}
if (asSats === 'chainFee') {
2024-04-16 22:58:26 +00:00
if (chainFee <= 0) return null
return (
<div className={compClassName} onClick={handleClick} variant='link'>
{chainFee} sat/vB
</div>
)
}
if (asSats === 'fiat') {
2024-04-16 22:58:26 +00:00
if (!price || price < 0) return null
return (
<div className={compClassName} onClick={handleClick} variant='link'>
{fiatSymbol + fixedDecimal(price, 0)}
</div>
)
}
2021-04-12 18:05:09 +00:00
}