mars-v2-frontend/src/components/FormattedNumber.tsx
Linkie Link 6efa380a98
Ux and details update (#77)
* fix: added the metadata base

* tidy: refactor the config structure

* fix: fixed the denom and fetching shenanigans

* fix: get rid of injective

* fix: removed logs

* tidy: updated dependencies

* fix: replaced static fallbacks with networkConfig.asset.base

* fix: remove memo

* tidy: added TODO comment

* fix: fixed the accountDetails

* fix: fixed the wallet connector suffix
2022-12-23 12:23:00 +01:00

67 lines
1.7 KiB
TypeScript

import classNames from 'classnames'
import React, { useEffect, useRef } from 'react'
import { animated, useSpring } from 'react-spring'
import { useSettingsStore } from 'stores'
import { formatValue } from 'utils/formatters'
export const FormattedNumber = React.memo(
({
amount,
animate = false,
className,
minDecimals = 2,
maxDecimals = 2,
thousandSeparator = true,
prefix = false,
suffix = false,
rounded = false,
abbreviated = false,
}: FormattedNumberProps) => {
const enableAnimations = useSettingsStore((s) => s.enableAnimations)
const prevAmountRef = useRef<number>(0)
useEffect(() => {
if (prevAmountRef.current !== Number(amount)) prevAmountRef.current = Number(amount)
}, [amount])
const springAmount = useSpring({
number: Number(amount),
from: { number: prevAmountRef.current },
config: { duration: 1000 },
})
return (prevAmountRef.current === amount && amount === 0) || !animate || !enableAnimations ? (
<span className={classNames('number', className)}>
{formatValue(
amount,
minDecimals,
maxDecimals,
thousandSeparator,
prefix,
suffix,
rounded,
abbreviated,
)}
</span>
) : (
<animated.span className={classNames('number', className)}>
{springAmount.number.to((num) =>
formatValue(
num,
minDecimals,
maxDecimals,
thousandSeparator,
prefix,
suffix,
rounded,
abbreviated,
),
)}
</animated.span>
)
},
)
FormattedNumber.displayName = 'FormattedNumber'