Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cherry pick accountListItem aggregated fiat balance into v12.14.0 #30786

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions ui/components/multichain/account-list-item/account-list-item.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { useContext, useEffect, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta)
import { BigNumber } from 'bignumber.js';
///: END:ONLY_INCLUDE_IF
import { useSelector } from 'react-redux';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { getSnapName, shortenAddress } from '../../../helpers/utils/util';
Expand Down Expand Up @@ -55,7 +57,9 @@ import {
getChainIdsToPoll,
getSnapsMetadata,
} from '../../../selectors';
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta)
import { getIntlLocale } from '../../../ducks/locale/locale';
///: END:ONLY_INCLUDE_IF
import {
getMultichainIsTestnet,
getMultichainNativeCurrency,
Expand All @@ -72,7 +76,12 @@ import { useMultichainSelector } from '../../../hooks/useMultichainSelector';
import { useGetFormattedTokensPerChain } from '../../../hooks/useGetFormattedTokensPerChain';
import { useAccountTotalCrossChainFiatBalance } from '../../../hooks/useAccountTotalCrossChainFiatBalance';
import { getAccountLabel } from '../../../helpers/utils/accounts';
///: BEGIN:ONLY_INCLUDE_IF(multichain)
import { getMultichainAggregatedBalance } from '../../../selectors/assets';
///: END:ONLY_INCLUDE_IF
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta)
import { formatWithThreshold } from '../../app/assets/util/formatWithThreshold';
///: END:ONLY_INCLUDE_IF
import { AccountListItemMenuTypes } from './account-list-item.types';

const MAXIMUM_CURRENCY_DECIMALS = 3;
Expand All @@ -96,7 +105,9 @@ const AccountListItem = ({
privacyMode = false,
}) => {
const t = useI18nContext();
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta)
const locale = useSelector(getIntlLocale);
///: END:ONLY_INCLUDE_IF
const [accountOptionsMenuOpen, setAccountOptionsMenuOpen] = useState(false);
const [accountListItemMenuElement, setAccountListItemMenuElement] =
useState();
Expand Down Expand Up @@ -126,6 +137,13 @@ const AccountListItem = ({
shouldShowFiat && (isMainnet || (isTestnet && showFiatInTestnets));
const accountTotalFiatBalances =
useMultichainAccountTotalFiatBalance(account);

///: BEGIN:ONLY_INCLUDE_IF(multichain)
const multichainAggregatedBalance = useSelector((state) =>
getMultichainAggregatedBalance(state, account),
);
///: END:ONLY_INCLUDE_IF

// cross chain agg balance
const shouldHideZeroBalanceTokens = useSelector(
getShouldHideZeroBalanceTokens,
Expand Down Expand Up @@ -157,6 +175,10 @@ const AccountListItem = ({
? account.balance
: totalFiatBalance;
} else {
///: BEGIN:ONLY_INCLUDE_IF(multichain)
balanceToTranslate = multichainAggregatedBalance;
///: END:ONLY_INCLUDE_IF
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta)
const balanceOrFallback = accountTotalFiatBalances?.totalBalance ?? 0;
const bnBalance = new BigNumber(balanceOrFallback);
const formattedBalanceToTranslate = formatWithThreshold(
Expand All @@ -169,6 +191,7 @@ const AccountListItem = ({
},
);
balanceToTranslate = formattedBalanceToTranslate;
///: END:ONLY_INCLUDE_IF
}

// If this is the selected item in the Account menu,
Expand Down Expand Up @@ -196,6 +219,31 @@ const AccountListItem = ({
currentTabOrigin && currentTabIsConnectedToSelectedAddress;
const isSingleAccount = accountsCount === 1;

const getIsAggregatedFiatOverviewBalanceProp = () => {
let isAggregatedFiatOverviewBalance;
///: BEGIN:ONLY_INCLUDE_IF(multichain)
isAggregatedFiatOverviewBalance =
(!isTestnet && process.env.PORTFOLIO_VIEW && shouldShowFiat) ||
!isEvmNetwork;
///: END:ONLY_INCLUDE_IF
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta)
isAggregatedFiatOverviewBalance =
!isTestnet && process.env.PORTFOLIO_VIEW && shouldShowFiat;
///: END:ONLY_INCLUDE_IF
return isAggregatedFiatOverviewBalance;
};

const getPreferredCurrencyValue = () => {
let value;
///: BEGIN:ONLY_INCLUDE_IF(multichain)
value = account.balance;
///: END:ONLY_INCLUDE_IF
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta)
value = isEvmNetwork ? account.balance : balanceToTranslate;
///: END:ONLY_INCLUDE_IF
return value;
};

return (
<Box
display={Display.Flex}
Expand Down Expand Up @@ -330,9 +378,7 @@ const AccountListItem = ({
value={balanceToTranslate}
type={PRIMARY}
showFiat={showFiat}
isAggregatedFiatOverviewBalance={
!isTestnet && process.env.PORTFOLIO_VIEW && shouldShowFiat
}
isAggregatedFiatOverviewBalance={getIsAggregatedFiatOverviewBalanceProp()}
data-testid="first-currency-display"
privacyMode={privacyMode}
/>
Expand Down Expand Up @@ -377,7 +423,7 @@ const AccountListItem = ({
<UserPreferencedCurrencyDisplay
account={account}
ethNumberOfDecimals={MAXIMUM_CURRENCY_DECIMALS}
value={isEvmNetwork ? account.balance : balanceToTranslate}
value={getPreferredCurrencyValue()}
type={SECONDARY}
showNative
data-testid="second-currency-display"
Expand Down
2 changes: 1 addition & 1 deletion ui/hooks/useCurrencyDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export function useCurrencyDisplay(
return displayValue;
}

if (!isEvm) {
if (!isEvm && !isAggregatedFiatOverviewBalance) {
return formatNonEvmAssetCurrencyDisplay({
tokenSymbol: nativeCurrency,
isNativeCurrency,
Expand Down
2 changes: 1 addition & 1 deletion ui/hooks/useMultichainAccountTotalFiatBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const useMultichainAccountTotalFiatBalance = (
ticker as keyof typeof MULTICHAIN_NATIVE_CURRENCY_TO_CAIP19
];

if (!balances[account.id]?.[asset]) {
if (!balances?.[account.id]?.[asset]) {
// FIXME: We might try to get the balance for a created account, but the
// MultichainBalancesController might not have updated it yet!
return EMPTY_VALUES;
Expand Down
Loading