import type { BoxProps } from '@chakra-ui/react'; import { Box, chakra } from '@chakra-ui/react'; import React from 'react'; import { Tag } from 'toolkit/chakra/tag'; import { thinsp } from 'toolkit/utils/htmlEntities'; import IconSvg from 'ui/shared/IconSvg'; import type { Params as CalculateUsdValueParams } from './calculateUsdValue'; import calculateUsdValue from './calculateUsdValue'; import SimpleValue from './SimpleValue'; import { DEFAULT_ACCURACY, DEFAULT_ACCURACY_USD } from './utils'; export interface Props extends Omit, Omit { amount: string | null | undefined; asset?: React.ReactNode; startElement?: React.ReactNode; endElement?: React.ReactNode; noTooltip?: boolean; loading?: boolean; layout?: 'horizontal' | 'vertical'; tooltipContent?: React.ReactNode; historicalExchangeRate?: string | null; hasExchangeRateToggle?: boolean; } const AssetValue = ({ amount, asset, decimals, accuracy = DEFAULT_ACCURACY, accuracyUsd = DEFAULT_ACCURACY_USD, startElement, endElement, noTooltip, loading, exchangeRate, historicalExchangeRate, hasExchangeRateToggle, layout = 'horizontal', tooltipContent, ...rest }: Props) => { const hasHistorical = Boolean(historicalExchangeRate); const hasCurrent = Boolean(exchangeRate); const hasToggle = hasHistorical && hasCurrent && hasExchangeRateToggle && amount !== '0'; const [ showHistorical, setShowHistorical ] = React.useState(hasHistorical); React.useEffect(() => { setShowHistorical(hasHistorical); }, [ hasHistorical ]); const activeExchangeRate = showHistorical ? historicalExchangeRate : exchangeRate; const handleToggle = React.useCallback(() => { if (hasToggle) { setShowHistorical(prev => !prev); } }, [ hasToggle ]); if (amount === null || amount === undefined) { return -; } const { valueBn, usdBn } = calculateUsdValue({ amount, decimals, accuracy, accuracyUsd, exchangeRate: activeExchangeRate }); if (!activeExchangeRate) { return ( ); } const nativeValue = ( ); const clockIcon = showHistorical ? ( ) : undefined; const tooltipContentBefore = (() => { if (!amount || amount === '0') { return undefined; } // for values where historic exchange rate doesn't exist, we show current value without tooltip // for recent transactions, historicExchangeRate can be null, in this case we show current value with tooltip if (historicalExchangeRate === undefined) { return undefined; } return { showHistorical ? 'Estimated value on day of txn' : 'Current value' }; })(); const usdValue = hasToggle ? ( ) : ( ( : undefined } prefix="$" endElement={ layout === 'horizontal' ? ) : undefined } tooltipContentBefore={ tooltipContentBefore } loading={ loading } color="text.secondary" /> ); return ( { nativeValue } { usdValue } ); }; export default React.memo(AssetValue);