1- import React , { useContext , useEffect , useMemo , useState } from 'react'
1+ import React , { useContext , useMemo } from 'react'
22import { useQuery } from '@apollo/client'
33import { fixedDecimal } from '@/lib/format'
44import { useMe } from './me'
@@ -8,6 +8,7 @@ import { NORMAL_POLL_INTERVAL, SSR } from '@/lib/constants'
88import { useBlockHeight } from './block-height'
99import { useChainFee } from './chain-fee'
1010import { CompactLongCountdown } from './countdown'
11+ import { useCarousel } from './nav/carousel'
1112
1213export const PriceContext = React . createContext ( {
1314 price : null ,
@@ -43,43 +44,16 @@ export function PriceProvider ({ price, children }) {
4344 )
4445}
4546
46- const STORAGE_KEY = 'asSats'
47- const DEFAULT_SELECTION = 'fiat'
48-
49- const carousel = [
50- 'fiat' ,
51- 'yep' ,
52- '1btc' ,
53- 'blockHeight' ,
54- 'chainFee' ,
55- 'halving'
56- ]
57-
5847export default function Price ( { className } ) {
59- const [ asSats , setAsSats ] = useState ( undefined )
60- const [ pos , setPos ] = useState ( 0 )
61-
62- useEffect ( ( ) => {
63- const selection = window . localStorage . getItem ( STORAGE_KEY ) ?? DEFAULT_SELECTION
64- setAsSats ( selection )
65- setPos ( carousel . findIndex ( ( item ) => item === selection ) )
66- } , [ ] )
48+ const [ selection , handleClick ] = useCarousel ( )
6749
6850 const { price, fiatSymbol } = usePrice ( )
6951 const { height : blockHeight , halving } = useBlockHeight ( )
7052 const { fee : chainFee } = useChainFee ( )
7153
72- const handleClick = ( ) => {
73- const nextPos = ( pos + 1 ) % carousel . length
74-
75- window . localStorage . setItem ( STORAGE_KEY , carousel [ nextPos ] )
76- setAsSats ( carousel [ nextPos ] )
77- setPos ( nextPos )
78- }
79-
8054 const compClassName = ( className || '' ) + ' text-reset pointer'
8155
82- if ( asSats === 'yep' ) {
56+ if ( selection === 'yep' ) {
8357 if ( ! price || price < 0 ) return null
8458 return (
8559 < div className = { compClassName } onClick = { handleClick } variant = 'link' >
@@ -88,15 +62,15 @@ export default function Price ({ className }) {
8862 )
8963 }
9064
91- if ( asSats === '1btc' ) {
65+ if ( selection === '1btc' ) {
9266 return (
9367 < div className = { compClassName } onClick = { handleClick } variant = 'link' >
9468 1sat=1sat
9569 </ div >
9670 )
9771 }
9872
99- if ( asSats === 'blockHeight' ) {
73+ if ( selection === 'blockHeight' ) {
10074 if ( blockHeight <= 0 ) return null
10175 return (
10276 < div className = { compClassName } onClick = { handleClick } variant = 'link' >
@@ -105,7 +79,7 @@ export default function Price ({ className }) {
10579 )
10680 }
10781
108- if ( asSats === 'halving' ) {
82+ if ( selection === 'halving' ) {
10983 if ( ! halving ) return null
11084 return (
11185 < div className = { compClassName } onClick = { handleClick } variant = 'link' >
@@ -114,7 +88,7 @@ export default function Price ({ className }) {
11488 )
11589 }
11690
117- if ( asSats === 'chainFee' ) {
91+ if ( selection === 'chainFee' ) {
11892 if ( chainFee <= 0 ) return null
11993 return (
12094 < div className = { compClassName } onClick = { handleClick } variant = 'link' >
@@ -123,7 +97,7 @@ export default function Price ({ className }) {
12397 )
12498 }
12599
126- if ( asSats === 'fiat' ) {
100+ if ( selection === 'fiat' ) {
127101 if ( ! price || price < 0 ) return null
128102 return (
129103 < div className = { compClassName } onClick = { handleClick } variant = 'link' >
0 commit comments