Skip to content

Commit

Permalink
fix: unformat to raw fiat value for sorting purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
gambinish committed Sep 25, 2024
1 parent a0ad07a commit a6d153a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ const AssetListControlBar = ({
windowType !== ENVIRONMENT_TYPE_POPUP;

useEffect(() => {
// this swap is needed when toggling primary currency type for native token in order to sort by tokenFiatAmount only
if (useNativeCurrencyAsPrimaryCurrency) {
[nativeTokenWithBalance.string, nativeTokenWithBalance.tokenFiatAmount] =
[nativeTokenWithBalance.tokenFiatAmount, nativeTokenWithBalance.string];
}

const sortedTokenList = sortAssets(
[nativeTokenWithBalance, ...tokensWithBalances],
tokenSortConfig || {
Expand Down
1 change: 1 addition & 0 deletions ui/components/app/assets/asset-list/asset-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type TokenWithBalance = {
symbol: string;
string?: string;
image: string;
secondary?: string;
tokenFiatAmount?: string;
isNative?: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const NativeToken = ({ onClickAsset }: AssetListProps) => {
const balance = useSelector(getMultichainSelectedAccountCachedBalance);
const balanceIsLoading = !balance;

const { string, symbol, tokenFiatAmount } = useNativeTokenBalance();
const { string, symbol, secondary } = useNativeTokenBalance();

const primaryTokenImage = useSelector(getMultichainCurrencyImage);

Expand All @@ -47,7 +47,7 @@ const NativeToken = ({ onClickAsset }: AssetListProps) => {
// TODO: rename this primary/secondary concept here to be more intuitive, regardless of setting
primary={string}
tokenSymbol={symbol}
secondary={tokenFiatAmount}
secondary={secondary}
tokenImage={balanceIsLoading ? null : primaryTokenImage}
isOriginalTokenSymbol={isOriginalNativeSymbol}
isNativeCurrency
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import currencyFormatter from 'currency-formatter';
import { useSelector } from 'react-redux';
import {
showPrimaryCurrency,
Expand All @@ -9,7 +10,7 @@ import {
getMultichainSelectedAccountCachedBalance,
getMultichainShouldShowFiat,
} from '../../../../../selectors/multichain';
import { getPreferences } from '../../../../../selectors';
import { getCurrentCurrency, getPreferences } from '../../../../../selectors';
import { useIsOriginalNativeTokenSymbol } from '../../../../../hooks/useIsOriginalNativeTokenSymbol';
import { PRIMARY, SECONDARY } from '../../../../../helpers/constants/common';
import { useUserPreferencedCurrency } from '../../../../../hooks/useUserPreferencedCurrency';
Expand All @@ -30,7 +31,7 @@ export const useNativeTokenBalance = () => {
rpcUrl,
);
const balance = useSelector(getMultichainSelectedAccountCachedBalance);

const currentCurrency = useSelector(getCurrentCurrency);
const {
currency: primaryCurrency,
numberOfDecimals: primaryNumberOfDecimals,
Expand Down Expand Up @@ -72,12 +73,26 @@ export const useNativeTokenBalance = () => {
? primaryCurrencyProperties.suffix
: secondaryCurrencyProperties.suffix;

const unformattedTokenFiatAmount = useNativeCurrencyAsPrimaryCurrency
? secondaryCurrencyDisplay.toString()
: primaryCurrencyDisplay.toString();

// useCurrencyDisplay passes along the symbol and formatting into the value here
// for sorting we need the raw value, without the currency and it should be decimal
// this is the easiest way to do this without extensive refactoring of useCurrencyDisplay
const tokenFiatAmount = currencyFormatter
.unformat(unformattedTokenFiatAmount, {
code: currentCurrency.toUpperCase(),
})
.toString();

const nativeTokenWithBalance: TokenWithBalance = {
address: '',
symbol: tokenSymbol || '',
string: primaryBalance,
image: primaryTokenImage,
tokenFiatAmount: secondaryBalance,
secondary: secondaryBalance,
tokenFiatAmount,
isNative: true,
};

Expand Down

0 comments on commit a6d153a

Please sign in to comment.