Commit 93e6b65c authored by lynn's avatar lynn Committed by GitHub

fix: swap quote loading spinner (#5569)

* init TEMP

* it's working now but the loading spinner is being redesigned by fred

* working with fred new design

* undo unnecesary changes

* undo unnecessary change

* rspond to jordan

* more fixes

* remove %
parent 965a745d
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
// eslint-disable-next-line no-restricted-imports // eslint-disable-next-line no-restricted-imports
import { t } from '@lingui/macro' import { t } from '@lingui/macro'
import { formatCurrencyAmount, formatPriceImpact, NumberType } from '@uniswap/conedison/format'
import { Currency, CurrencyAmount, Percent } from '@uniswap/sdk-core' import { Currency, CurrencyAmount, Percent } from '@uniswap/sdk-core'
import { useMemo } from 'react' import { LoadingBubble } from 'components/Tokens/loading'
import { useTheme } from 'styled-components/macro' import { MouseoverTooltip } from 'components/Tooltip'
import { useEffect, useMemo, useState } from 'react'
import styled, { useTheme } from 'styled-components/macro'
import { ThemedText } from '../../theme' import { ThemedText } from '../../theme'
import { warningSeverity } from '../../utils/prices' import { warningSeverity } from '../../utils/prices'
import { MouseoverTooltip } from '../Tooltip'
const FiatLoadingBubble = styled(LoadingBubble)`
border-radius: 4px;
width: 4rem;
height: 1rem;
`
export function FiatValue({ export function FiatValue({
fiatValue, fiatValue,
priceImpact, priceImpact,
isLoading = false,
}: { }: {
fiatValue: CurrencyAmount<Currency> | null | undefined fiatValue: CurrencyAmount<Currency> | null | undefined
priceImpact?: Percent priceImpact?: Percent
isLoading?: boolean
}) { }) {
const theme = useTheme() const theme = useTheme()
const [showLoadingPlaceholder, setShowLoadingPlaceholder] = useState(false)
const priceImpactColor = useMemo(() => { const priceImpactColor = useMemo(() => {
if (!priceImpact) return undefined if (!priceImpact) return undefined
if (priceImpact.lessThan('0')) return theme.deprecated_green1 if (priceImpact.lessThan('0')) return theme.deprecated_green1
...@@ -26,20 +37,36 @@ export function FiatValue({ ...@@ -26,20 +37,36 @@ export function FiatValue({
return theme.deprecated_red1 return theme.deprecated_red1
}, [priceImpact, theme.deprecated_green1, theme.deprecated_red1, theme.deprecated_text3, theme.deprecated_yellow1]) }, [priceImpact, theme.deprecated_green1, theme.deprecated_red1, theme.deprecated_text3, theme.deprecated_yellow1])
const p = Number(fiatValue?.toFixed()) useEffect(() => {
const visibleDecimalPlaces = p < 1.05 ? 4 : 2 const stale = false
let timeoutId = 0
if (isLoading && !fiatValue) {
timeoutId = setTimeout(() => {
if (!stale) setShowLoadingPlaceholder(true)
}, 200) as unknown as number
} else {
setShowLoadingPlaceholder(false)
}
return () => clearTimeout(timeoutId)
}, [isLoading, fiatValue])
return ( return (
<ThemedText.DeprecatedBody fontSize={14} color={theme.textSecondary}> <ThemedText.DeprecatedBody fontSize={14} color={theme.textSecondary}>
{fiatValue && <>${fiatValue?.toFixed(visibleDecimalPlaces, { groupSeparator: ',' })}</>} {showLoadingPlaceholder ? (
{priceImpact ? ( <FiatLoadingBubble />
) : (
<div>
{fiatValue && <>{formatCurrencyAmount(fiatValue, NumberType.FiatTokenPrice)}</>}
{priceImpact && (
<span style={{ color: priceImpactColor }}> <span style={{ color: priceImpactColor }}>
{' '} {' '}
<MouseoverTooltip text={t`The estimated difference between the USD values of input and output amounts.`}> <MouseoverTooltip text={t`The estimated difference between the USD values of input and output amounts.`}>
(<Trans>{priceImpact.multiply(-1).toSignificant(3)}%</Trans>) (<Trans>{formatPriceImpact(priceImpact)}</Trans>)
</MouseoverTooltip> </MouseoverTooltip>
</span> </span>
) : null} )}
</div>
)}
</ThemedText.DeprecatedBody> </ThemedText.DeprecatedBody>
) )
} }
...@@ -9,7 +9,7 @@ import { LoadingOpacityContainer, loadingOpacityMixin } from 'components/Loader/ ...@@ -9,7 +9,7 @@ import { LoadingOpacityContainer, loadingOpacityMixin } from 'components/Loader/
import CurrencyLogo from 'components/Logo/CurrencyLogo' import CurrencyLogo from 'components/Logo/CurrencyLogo'
import { isSupportedChain } from 'constants/chains' import { isSupportedChain } from 'constants/chains'
import { darken } from 'polished' import { darken } from 'polished'
import { ReactNode, useCallback, useState } from 'react' import { ReactNode, useCallback, useEffect, useState } from 'react'
import { Lock } from 'react-feather' import { Lock } from 'react-feather'
import styled, { useTheme } from 'styled-components/macro' import styled, { useTheme } from 'styled-components/macro'
import { flexColumnNoWrap, flexRowNoWrap } from 'theme/styles' import { flexColumnNoWrap, flexRowNoWrap } from 'theme/styles'
...@@ -229,6 +229,7 @@ export default function SwapCurrencyInputPanel({ ...@@ -229,6 +229,7 @@ export default function SwapCurrencyInputPanel({
...rest ...rest
}: SwapCurrencyInputPanelProps) { }: SwapCurrencyInputPanelProps) {
const [modalOpen, setModalOpen] = useState(false) const [modalOpen, setModalOpen] = useState(false)
const [fiatValueIsLoading, setFiatValueIsLoading] = useState(false)
const { account, chainId } = useWeb3React() const { account, chainId } = useWeb3React()
const selectedCurrencyBalance = useCurrencyBalance(account ?? undefined, currency ?? undefined) const selectedCurrencyBalance = useCurrencyBalance(account ?? undefined, currency ?? undefined)
const theme = useTheme() const theme = useTheme()
...@@ -239,6 +240,10 @@ export default function SwapCurrencyInputPanel({ ...@@ -239,6 +240,10 @@ export default function SwapCurrencyInputPanel({
const chainAllowed = isSupportedChain(chainId) const chainAllowed = isSupportedChain(chainId)
useEffect(() => {
!!value && !fiatValue ? setFiatValueIsLoading(true) : setFiatValueIsLoading(false)
}, [fiatValueIsLoading, value, fiatValue])
return ( return (
<InputPanel id={id} hideInput={hideInput} {...rest}> <InputPanel id={id} hideInput={hideInput} {...rest}>
{locked && ( {locked && (
...@@ -306,7 +311,7 @@ export default function SwapCurrencyInputPanel({ ...@@ -306,7 +311,7 @@ export default function SwapCurrencyInputPanel({
<FiatRow> <FiatRow>
<RowBetween> <RowBetween>
<LoadingOpacityContainer $loading={loading}> <LoadingOpacityContainer $loading={loading}>
<FiatValue fiatValue={fiatValue} priceImpact={priceImpact} /> <FiatValue fiatValue={fiatValue} priceImpact={priceImpact} isLoading={fiatValueIsLoading} />
</LoadingOpacityContainer> </LoadingOpacityContainer>
{account ? ( {account ? (
<RowFixed style={{ height: '17px' }}> <RowFixed style={{ height: '17px' }}>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment