/** * Copyright (c) TonTech. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import type { FC, ComponentProps } from 'react'; import { calcFiatValue, formatLargeValue } from '@ton/appkit'; import clsx from 'clsx'; import { Logo } from '../../ui/logo'; import { TonIconCircle } from '../../ui/icons'; import type { AppkitUIToken } from '../../../types/appkit-ui-token'; import { getDisplayAmount } from '../../../features/swap/utils/get-display-amount'; import styles from './amount-preview.module.css'; export interface AmountPreviewProps extends ComponentProps<'div'> { /** Raw token amount to display (decimal string). */ amount: string; /** Token whose logo and symbol are shown alongside the amount. */ token?: AppkitUIToken; /** Fiat currency symbol, e.g. "$". */ fiatSymbol?: string; /** * Relative fiat delta to render after the fiat value, e.g. -0.0025 → "(-0.25%)". * Typically computed by a parent that knows both legs of a flow. */ fiatDelta?: number; } const formatFiatDelta = (delta: number): string => { const sign = delta > 0 ? '+' : ''; return `(${sign}${(delta * 100).toFixed(2)}%)`; }; export const AmountPreview: FC = ({ amount, token, fiatSymbol = '$', fiatDelta, className, ...props }) => { const displayAmount = getDisplayAmount(amount, token?.decimals); const fiatValue = token?.rate ? formatLargeValue(calcFiatValue(amount || '0', token.rate), 2, 2) : null; return (
{displayAmount} {token && ( {token.address === 'ton' ? ( ) : ( )} {token.symbol} )}
{fiatValue !== null && (
{fiatSymbol} {fiatValue} {fiatDelta !== undefined && {formatFiatDelta(fiatDelta)}}
)}
); };