Skip to content

Commit

Permalink
Merge pull request #14055 from brave/fix-price-history-continuously-l…
Browse files Browse the repository at this point in the history
…oading

fix(wallet): Price History Continuously Loading
  • Loading branch information
Douglashdaniel authored Jul 7, 2022
2 parents 38f1b5f + 6aac60d commit 9ff7720
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import {
WalletState,
PageState,
SupportedTestNetworks,
WalletRoutes
WalletRoutes,
PriceDataObjectType
} from '../../../../constants/types'
import { getLocale } from '../../../../../common/locale'

// Utils
import { getTokensCoinType } from '../../../../utils/network-utils'
import { formatAsDouble } from '../../../../utils/string-utils'
import Amount from '../../../../utils/amount'

// Options
Expand Down Expand Up @@ -123,7 +125,7 @@ export const PortfolioOverview = () => {
}, [userVisibleTokensInfo, selectedNetworkFilter, fullAssetBalance, networkList])

// This will scrape all of the user's accounts and combine the fiat value for every asset
const fullPortfolioFiatBalance = React.useMemo(() => {
const fullPortfolioFiatBalance = React.useMemo((): string => {
const visibleAssetOptions = userAssetList
.filter((token) =>
token.asset.visible &&
Expand All @@ -143,14 +145,19 @@ export const PortfolioOverview = () => {
return a.plus(b)
})
return grandTotal.formatAsFiat(defaultCurrencies.fiat)
}, [userAssetList, computeFiatAmount])
}, [userAssetList, defaultCurrencies, computeFiatAmount])

const priceHistory = React.useMemo(() => {
if (parseFloat(fullPortfolioFiatBalance) === 0) {
const isZeroBalance = React.useMemo((): boolean => {
// In some cases we need to check if the balance is zero
return parseFloat(formatAsDouble(fullPortfolioFiatBalance)) === 0
}, [fullPortfolioFiatBalance])

const priceHistory = React.useMemo((): PriceDataObjectType[] => {
if (isZeroBalance) {
return []
}
return portfolioPriceHistory
}, [portfolioPriceHistory, fullPortfolioFiatBalance])
}, [portfolioPriceHistory, isZeroBalance])

// state
const [filteredAssetList, setfilteredAssetList] = React.useState<UserAssetInfoType[]>(userAssetList)
Expand Down Expand Up @@ -226,8 +233,8 @@ export const PortfolioOverview = () => {
isAsset={false}
priceData={priceHistory}
onUpdateBalance={onUpdateBalance}
isLoading={parseFloat(fullPortfolioFiatBalance) === 0 ? false : isFetchingPortfolioPriceHistory}
isDisabled={parseFloat(fullPortfolioFiatBalance) === 0}
isLoading={isZeroBalance ? false : isFetchingPortfolioPriceHistory}
isDisabled={isZeroBalance}
/>

<TokenLists
Expand Down
11 changes: 10 additions & 1 deletion components/brave_wallet_ui/utils/string-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isRemoteImageURL, isValidIconExtension } from './string-utils'
import { isRemoteImageURL, isValidIconExtension, formatAsDouble } from './string-utils'

describe('Checking URL is remote image or not', () => {
test('HTTP URL should return true', () => {
Expand Down Expand Up @@ -43,3 +43,12 @@ describe('Checking URL ends with a valid icon extension', () => {
expect(isValidIconExtension('https://test.com/')).toEqual(false)
})
})

describe('Check toDouble values', () => {
test('Value with a USD symbol, should remove the USD symbol', () => {
expect(formatAsDouble('$1,689.16')).toEqual('1,689.16')
})
test('Value with a Euro symbol, should remove the Euro symbol', () => {
expect(formatAsDouble('689,16€')).toEqual('689,16')
})
})
4 changes: 4 additions & 0 deletions components/brave_wallet_ui/utils/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ export const getRampNetworkPrefix = (chainId: string) => {
return ''
}
}

export const formatAsDouble = (value: string): string =>
// Removes all characters except numbers, commas and decimals
value.replace(/[^0-9.,]+/g, '')

0 comments on commit 9ff7720

Please sign in to comment.