1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Fix and simplify account switching in Swaps (#9549)

* Ensure rendered swap from token balance updates when token balance changes

* Disable account switching on all swaps routes other than build quote
This commit is contained in:
Dan J Miller 2020-10-12 14:21:01 -02:30 committed by GitHub
parent dc5edb5431
commit a9d156e611
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 11 deletions

View File

@ -1,4 +1,4 @@
import { useMemo, useState } from 'react' import { useMemo } from 'react'
import { useSelector } from 'react-redux' import { useSelector } from 'react-redux'
import contractMap from 'eth-contract-metadata' import contractMap from 'eth-contract-metadata'
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
@ -64,34 +64,34 @@ export function useTokensToSearch ({
const memoizedUsersToken = useEqualityCheck(usersTokens) const memoizedUsersToken = useEqualityCheck(usersTokens)
const swapsEthToken = useSwapsEthToken() const swapsEthToken = useSwapsEthToken()
const [ethToken] = useState(() => getRenderableTokenData( const ethToken = getRenderableTokenData(
swapsEthToken, swapsEthToken,
tokenConversionRates, tokenConversionRates,
conversionRate, conversionRate,
currentCurrency, currentCurrency,
)) )
const memoizedEthToken = useEqualityCheck(ethToken)
const swapsTokens = useSelector(getSwapsTokens) || [] const swapsTokens = useSelector(getSwapsTokens) || []
let tokensToSearch let tokensToSearch
if (onlyEth) { if (onlyEth) {
tokensToSearch = [ethToken] tokensToSearch = [memoizedEthToken]
} else if (singleToken) { } else if (singleToken) {
tokensToSearch = providedTokens tokensToSearch = providedTokens
} else if (providedTokens) { } else if (providedTokens) {
tokensToSearch = [ethToken, ...providedTokens] tokensToSearch = [memoizedEthToken, ...providedTokens]
} else if (swapsTokens.length) { } else if (swapsTokens.length) {
tokensToSearch = [ethToken, ...swapsTokens] tokensToSearch = [memoizedEthToken, ...swapsTokens]
} else { } else {
tokensToSearch = [ethToken, ...tokenList] tokensToSearch = [memoizedEthToken, ...tokenList]
} }
const memoizedTokensToSearch = useEqualityCheck(tokensToSearch) const memoizedTokensToSearch = useEqualityCheck(tokensToSearch)
return useMemo(() => { return useMemo(() => {
const usersTokensAddressMap = memoizedUsersToken.reduce((acc, token) => ({ ...acc, [token.address]: token }), {}) const usersTokensAddressMap = memoizedUsersToken.reduce((acc, token) => ({ ...acc, [token.address]: token }), {})
const tokensToSearchBuckets = { const tokensToSearchBuckets = {
owned: singleToken ? [] : [ethToken], owned: singleToken ? [] : [memoizedEthToken],
top: [], top: [],
others: [], others: [],
} }
@ -116,5 +116,5 @@ export function useTokensToSearch ({
...tokensToSearchBuckets.top, ...tokensToSearchBuckets.top,
...tokensToSearchBuckets.others, ...tokensToSearchBuckets.others,
] ]
}, [memoizedTokensToSearch, memoizedUsersToken, tokenConversionRates, conversionRate, currentCurrency, memoizedTopTokens, ethToken, singleToken]) }, [memoizedTokensToSearch, memoizedUsersToken, tokenConversionRates, conversionRate, currentCurrency, memoizedTopTokens, memoizedEthToken, singleToken])
} }

View File

@ -52,6 +52,7 @@ import {
SWAPS_ROUTE, SWAPS_ROUTE,
SETTINGS_ROUTE, SETTINGS_ROUTE,
UNLOCK_ROUTE, UNLOCK_ROUTE,
BUILD_QUOTE_ROUTE,
} from '../../helpers/constants/routes' } from '../../helpers/constants/routes'
import { ENVIRONMENT_TYPE_NOTIFICATION, ENVIRONMENT_TYPE_POPUP } from '../../../../app/scripts/lib/enums' import { ENVIRONMENT_TYPE_NOTIFICATION, ENVIRONMENT_TYPE_POPUP } from '../../../../app/scripts/lib/enums'
@ -161,6 +162,11 @@ export default class Routes extends Component {
return Boolean(matchPath(location.pathname, { path: SWAPS_ROUTE, exact: false })) return Boolean(matchPath(location.pathname, { path: SWAPS_ROUTE, exact: false }))
} }
onSwapsBuildQuotePage () {
const { location } = this.props
return Boolean(matchPath(location.pathname, { path: BUILD_QUOTE_ROUTE, exact: false }))
}
hideAppHeader () { hideAppHeader () {
const { location } = this.props const { location } = this.props
@ -248,7 +254,7 @@ export default class Routes extends Component {
} }
}} }}
disabled={this.onConfirmPage()} disabled={this.onConfirmPage() || (this.onSwapsPage() && !this.onSwapsBuildQuotePage())}
/> />
) } ) }
<Sidebar <Sidebar

View File

@ -153,6 +153,18 @@ export default function BuildQuote ({
const hideDropdownItemIf = useCallback((item) => item.address === fromTokenAddress, [fromTokenAddress]) const hideDropdownItemIf = useCallback((item) => item.address === fromTokenAddress, [fromTokenAddress])
const tokensWithBalancesFromToken = tokensWithBalances.find((token) => token.address === fromToken?.address)
const previousTokensWithBalancesFromToken = usePrevious(tokensWithBalancesFromToken)
useEffect(() => {
const notEth = tokensWithBalancesFromToken?.address !== ETH_SWAPS_TOKEN_OBJECT.address
const addressesAreTheSame = tokensWithBalancesFromToken?.address === previousTokensWithBalancesFromToken?.address
const balanceHasChanged = tokensWithBalancesFromToken?.balance !== previousTokensWithBalancesFromToken?.balance
if (notEth && addressesAreTheSame && balanceHasChanged) {
dispatch(setSwapsFromToken({ ...fromToken, balance: tokensWithBalancesFromToken?.balance, string: tokensWithBalancesFromToken?.string }))
}
}, [dispatch, tokensWithBalancesFromToken, previousTokensWithBalancesFromToken, fromToken])
// If the eth balance changes while on build quote, we update the selected from token // If the eth balance changes while on build quote, we update the selected from token
useEffect(() => { useEffect(() => {
if (fromToken?.address === ETH_SWAPS_TOKEN_OBJECT.address && (fromToken?.balance !== ethBalance)) { if (fromToken?.address === ETH_SWAPS_TOKEN_OBJECT.address && (fromToken?.balance !== ethBalance)) {