@ -1,62 +1,33 @@
import React , { useContext } from 'react'
import React from 'react'
import PropTypes from 'prop-types'
import PropTypes from 'prop-types'
import BigNumber from 'bignumber.js'
import BigNumber from 'bignumber.js'
import classnames from 'classnames'
import { I18nContext } from '../../../contexts/i18n'
import { calcTokenAmount } from '../../../helpers/utils/token-util'
import { calcTokenAmount } from '../../../helpers/utils/token-util'
import { toPrecisionWithoutTrailingZeros } from '../../../helpers/utils/util'
import { toPrecisionWithoutTrailingZeros } from '../../../helpers/utils/util'
import Tooltip from '../../../components/ui/tooltip'
import Tooltip from '../../../components/ui/tooltip'
import SunCheckIcon from '../../../components/ui/icon/sun-check-icon.component '
import UrlIcon from '../../../components/ui/url-icon '
import ExchangeRateDisplay from '../exchange-rate-display'
import ExchangeRateDisplay from '../exchange-rate-display'
import { formatSwapsValueForDisplay } from '../swaps.util'
import { formatSwapsValueForDisplay } from '../swaps.util'
import QuoteBackdrop from './quote-backdrop'
function getFontSizes ( fontSizeScore ) {
function getFontSizesAndLineHeights ( fontSizeScore ) {
if ( fontSizeScore <= 11 ) {
if ( fontSizeScore <= 9 ) {
return [ 40 , 32 ]
return [ 60 , 48 ]
}
if ( fontSizeScore <= 16 ) {
return [ 30 , 24 ]
}
return [ 24 , 14 ]
}
function getLineHeight ( fontSizeScore ) {
if ( fontSizeScore <= 11 ) {
return 32
}
}
if ( fontSizeScore <= 16 ) {
if ( fontSizeScore <= 13 ) {
return 26
return [ 40 , 32 ]
}
}
return 18
return [ 26 , 15 ]
}
// Returns a numerical value based on the length of the two passed strings: amount and symbol.
// The returned value equals the number of digits in the amount string plus a value calculated
// from the length of the symbol string. The returned number will be passed to the getFontSizes function
// to determine the font size to apply to the amount and symbol strings when rendered. The
// desired maximum digits and letters to show in the ultimately rendered string is 20, and in
// such cases there can also be ellipsis shown and a decimal, combinding for a rendered "string"
// length of ~22. As the symbol will always have a smaller font size than the amount, the
// additive value of the symbol length to the font size score is corrected based on the total
// number of alphanumeric characters in both strings and the desired rendered length of 22.
function getFontSizeScore ( amount , symbol ) {
const amountLength = amount . match ( /\d+/gu ) . join ( '' ) . length
const symbolModifier = Math . min ( ( amountLength + symbol . length ) / 22 , 1 )
return amountLength + symbol . length * symbolModifier
}
}
export default function MainQuoteSummary ( {
export default function MainQuoteSummary ( {
isBestQuote ,
sourceValue ,
sourceValue ,
sourceSymbol ,
sourceSymbol ,
sourceDecimals ,
sourceDecimals ,
sourceIconUrl ,
destinationValue ,
destinationValue ,
destinationSymbol ,
destinationSymbol ,
destinationDecimals ,
destinationDecimals ,
destinationIconUrl ,
} ) {
} ) {
const t = useContext ( I18nContext )
const sourceAmount = toPrecisionWithoutTrailingZeros (
const sourceAmount = toPrecisionWithoutTrailingZeros (
calcTokenAmount ( sourceValue , sourceDecimals ) . toString ( 10 ) ,
calcTokenAmount ( sourceValue , sourceDecimals ) . toString ( 10 ) ,
12 ,
12 ,
@ -67,43 +38,54 @@ export default function MainQuoteSummary({
)
)
const amountToDisplay = formatSwapsValueForDisplay ( destinationAmount )
const amountToDisplay = formatSwapsValueForDisplay ( destinationAmount )
const fontSizeScore = getFontSizeScore ( amountToDisplay , destinationSymbol )
const amountDigitLength = amountToDisplay . match ( /\d+/gu ) . join ( '' ) . length
const [ numberFontSize , symbolFontSize ] = getFontSizes ( fontSizeScore )
const [ numberFontSize , lineHeight ] = getFontSizesAndLineHeights (
const lineHeight = getLineHeight ( fontSizeScore )
amountDigitLength ,
)
let ellipsedAmountToDisplay = amountToDisplay
let ellipsedAmountToDisplay = amountToDisplay
if ( fontSizeScore > 20 ) {
if ( amountDigitLength > 20 ) {
ellipsedAmountToDisplay = ` ${ amountToDisplay . slice (
ellipsedAmountToDisplay = ` ${ amountToDisplay . slice ( 0 , 20 ) } ... `
0 ,
amountToDisplay . length - ( fontSizeScore - 20 ) ,
) } ... `
}
}
return (
return (
< div className = "main-quote-summary" >
< div className = "main-quote-summary" >
< div
className = { classnames ( 'main-quote-summary__quote-backdrop' , {
'main-quote-summary__quote-backdrop-with-top-tab' : isBestQuote ,
} ) }
>
< QuoteBackdrop withTopTab = { isBestQuote } / >
< / d i v >
< div className = "main-quote-summary__best-quote" >
{ isBestQuote && < SunCheckIcon / > }
< span > { isBestQuote && t ( 'swapsBestQuote' ) } < / s p a n >
< / d i v >
< div className = "main-quote-summary__details" >
< div className = "main-quote-summary__details" >
< div className = "main-quote-summary__quote-details-top" >
< div className = "main-quote-summary__quote-details-top" >
< span className = "main-quote-summary__quote-small-white" >
< div className = "main-quote-summary__source-row" >
{ t ( 'swapsConvertToAbout' , [
< span
< span
className = "main-quote-summary__bold "
className = "main-quote-summary__source-row-value"
key = "main-quote-summary-bold-1"
title = { formatSwapsValueForDisplay ( sourceAmount ) }
>
>
{ ` ${ sourceAmount } ${ sourceSymbol } ` }
{ formatSwapsValueForDisplay ( sourceAmount ) }
< / s p a n > ,
] ) }
< / s p a n >
< / s p a n >
< UrlIcon
url = { sourceIconUrl }
className = "main-quote-summary__icon"
name = { sourceSymbol }
fallbackClassName = "main-quote-summary__icon-fallback"
/ >
< span
className = "main-quote-summary__source-row-symbol"
title = { sourceSymbol }
>
{ sourceSymbol }
< / s p a n >
< / d i v >
< img
className = "main-quote-summary__down-arrow"
src = "images/down-arrow-grey.svg"
/ >
< div className = "main-quote-summary__destination-row" >
< UrlIcon
url = { destinationIconUrl }
className = "main-quote-summary__icon"
name = { destinationSymbol }
fallbackClassName = "main-quote-summary__icon-fallback"
/ >
< span className = "main-quote-summary__destination-row-symbol" >
{ destinationSymbol }
< / s p a n >
< / d i v >
< div className = "main-quote-summary__quote-large" >
< div className = "main-quote-summary__quote-large" >
< Tooltip
< Tooltip
interactive
interactive
@ -122,15 +104,6 @@ export default function MainQuoteSummary({
{ ` ${ ellipsedAmountToDisplay } ` }
{ ` ${ ellipsedAmountToDisplay } ` }
< / s p a n >
< / s p a n >
< / T o o l t i p >
< / T o o l t i p >
< span
className = "main-quote-summary__quote-large-symbol"
style = { {
fontSize : symbolFontSize ,
lineHeight : ` ${ lineHeight } px ` ,
} }
>
{ ` ${ destinationSymbol } ` }
< / s p a n >
< / d i v >
< / d i v >
< / d i v >
< / d i v >
< div className = "main-quote-summary__exchange-rate-container" >
< div className = "main-quote-summary__exchange-rate-container" >
@ -141,8 +114,9 @@ export default function MainQuoteSummary({
secondaryTokenValue = { destinationValue }
secondaryTokenValue = { destinationValue }
secondaryTokenDecimals = { destinationDecimals }
secondaryTokenDecimals = { destinationDecimals }
secondaryTokenSymbol = { destinationSymbol }
secondaryTokenSymbol = { destinationSymbol }
className = "exchange-rate-display--white"
arrowColor = "#037DD6"
arrowColor = "white"
boldSymbols = { false }
className = "main-quote-summary__exchange-rate-display"
/ >
/ >
< / d i v >
< / d i v >
< / d i v >
< / d i v >
@ -151,7 +125,6 @@ export default function MainQuoteSummary({
}
}
MainQuoteSummary . propTypes = {
MainQuoteSummary . propTypes = {
isBestQuote : PropTypes . bool ,
sourceValue : PropTypes . oneOfType ( [
sourceValue : PropTypes . oneOfType ( [
PropTypes . string ,
PropTypes . string ,
PropTypes . instanceOf ( BigNumber ) ,
PropTypes . instanceOf ( BigNumber ) ,
@ -167,4 +140,6 @@ MainQuoteSummary.propTypes = {
PropTypes . number ,
PropTypes . number ,
] ) ,
] ) ,
destinationSymbol : PropTypes . string . isRequired ,
destinationSymbol : PropTypes . string . isRequired ,
sourceIconUrl : PropTypes . string ,
destinationIconUrl : PropTypes . string ,
}
}