2022-05-09 19:47:06 +02:00
|
|
|
import React, { useContext, useState } from 'react';
|
2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useSelector } from 'react-redux';
|
2021-09-10 20:03:42 +02:00
|
|
|
import ImportTokenLink from '../import-token-link';
|
2021-02-04 19:15:23 +01:00
|
|
|
import TokenList from '../token-list';
|
|
|
|
import AssetListItem from '../asset-list-item';
|
|
|
|
import { PRIMARY, SECONDARY } from '../../../helpers/constants/common';
|
|
|
|
import { useUserPreferencedCurrency } from '../../../hooks/useUserPreferencedCurrency';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
2022-06-13 21:39:48 +02:00
|
|
|
getSelectedAccountCachedBalance,
|
2020-11-03 00:41:28 +01:00
|
|
|
getShouldShowFiat,
|
2021-04-02 00:57:00 +02:00
|
|
|
getNativeCurrencyImage,
|
2022-05-09 19:47:06 +02:00
|
|
|
getDetectedTokensInCurrentNetwork,
|
2022-09-19 20:11:48 +02:00
|
|
|
getIstokenDetectionInactiveOnNonMainnetSupportedNetwork,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../selectors';
|
2021-06-08 18:03:59 +02:00
|
|
|
import { getNativeCurrency } from '../../../ducks/metamask/metamask';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { useCurrencyDisplay } from '../../../hooks/useCurrencyDisplay';
|
2021-09-10 20:03:42 +02:00
|
|
|
import Typography from '../../ui/typography/typography';
|
|
|
|
import Box from '../../ui/box/box';
|
|
|
|
import {
|
2023-02-02 21:15:26 +01:00
|
|
|
Color,
|
|
|
|
TypographyVariant,
|
2021-09-10 20:03:42 +02:00
|
|
|
FONT_WEIGHT,
|
2023-02-02 21:15:26 +01:00
|
|
|
JustifyContent,
|
2021-09-10 20:03:42 +02:00
|
|
|
} from '../../../helpers/constants/design-system';
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
2022-04-01 21:11:12 +02:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
2022-08-16 18:39:23 +02:00
|
|
|
import { EVENT, EVENT_NAMES } from '../../../../shared/constants/metametrics';
|
2022-05-09 19:47:06 +02:00
|
|
|
import DetectedToken from '../detected-token/detected-token';
|
|
|
|
import DetectedTokensLink from './detetcted-tokens-link/detected-tokens-link';
|
2020-05-14 05:16:30 +02:00
|
|
|
|
2020-06-01 19:54:32 +02:00
|
|
|
const AssetList = ({ onClickAsset }) => {
|
2021-09-10 20:03:42 +02:00
|
|
|
const t = useI18nContext();
|
2022-05-09 19:47:06 +02:00
|
|
|
|
|
|
|
const [showDetectedTokens, setShowDetectedTokens] = useState(false);
|
|
|
|
|
2022-06-13 21:39:48 +02:00
|
|
|
const selectedAccountBalance = useSelector(getSelectedAccountCachedBalance);
|
2021-02-04 19:15:23 +01:00
|
|
|
const nativeCurrency = useSelector(getNativeCurrency);
|
|
|
|
const showFiat = useSelector(getShouldShowFiat);
|
2022-03-14 19:12:38 +01:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2022-07-01 15:08:42 +02:00
|
|
|
const balance = useSelector(getSelectedAccountCachedBalance);
|
|
|
|
const balanceIsLoading = !balance;
|
2020-05-14 05:16:30 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
currency: primaryCurrency,
|
|
|
|
numberOfDecimals: primaryNumberOfDecimals,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = useUserPreferencedCurrency(PRIMARY, { ethNumberOfDecimals: 4 });
|
2020-05-14 05:16:30 +02:00
|
|
|
const {
|
|
|
|
currency: secondaryCurrency,
|
|
|
|
numberOfDecimals: secondaryNumberOfDecimals,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = useUserPreferencedCurrency(SECONDARY, { ethNumberOfDecimals: 4 });
|
2020-05-14 05:16:30 +02:00
|
|
|
|
2020-11-05 23:16:24 +01:00
|
|
|
const [, primaryCurrencyProperties] = useCurrencyDisplay(
|
|
|
|
selectedAccountBalance,
|
|
|
|
{
|
|
|
|
numberOfDecimals: primaryNumberOfDecimals,
|
|
|
|
currency: primaryCurrency,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-06-03 19:50:12 +02:00
|
|
|
|
2022-07-31 20:26:40 +02:00
|
|
|
const [secondaryCurrencyDisplay, secondaryCurrencyProperties] =
|
|
|
|
useCurrencyDisplay(selectedAccountBalance, {
|
|
|
|
numberOfDecimals: secondaryNumberOfDecimals,
|
|
|
|
currency: secondaryCurrency,
|
|
|
|
});
|
2020-06-03 19:50:12 +02:00
|
|
|
|
2021-04-02 00:57:00 +02:00
|
|
|
const primaryTokenImage = useSelector(getNativeCurrencyImage);
|
2022-05-09 19:47:06 +02:00
|
|
|
const detectedTokens = useSelector(getDetectedTokensInCurrentNetwork) || [];
|
2022-09-19 20:11:48 +02:00
|
|
|
const istokenDetectionInactiveOnNonMainnetSupportedNetwork = useSelector(
|
|
|
|
getIstokenDetectionInactiveOnNonMainnetSupportedNetwork,
|
|
|
|
);
|
2021-04-02 00:57:00 +02:00
|
|
|
|
2020-05-14 05:16:30 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<AssetListItem
|
2020-06-01 19:54:32 +02:00
|
|
|
onClick={() => onClickAsset(nativeCurrency)}
|
2020-05-14 05:16:30 +02:00
|
|
|
data-testid="wallet-balance"
|
2021-06-24 01:28:49 +02:00
|
|
|
primary={
|
|
|
|
primaryCurrencyProperties.value ?? secondaryCurrencyProperties.value
|
|
|
|
}
|
2020-11-05 23:16:24 +01:00
|
|
|
tokenSymbol={primaryCurrencyProperties.suffix}
|
2020-06-06 00:06:15 +02:00
|
|
|
secondary={showFiat ? secondaryCurrencyDisplay : undefined}
|
2022-07-01 15:08:42 +02:00
|
|
|
tokenImage={balanceIsLoading ? null : primaryTokenImage}
|
2021-04-02 00:57:00 +02:00
|
|
|
identiconBorder
|
2020-06-03 19:50:12 +02:00
|
|
|
/>
|
2020-05-14 05:16:30 +02:00
|
|
|
<TokenList
|
|
|
|
onTokenClick={(tokenAddress) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
onClickAsset(tokenAddress);
|
2022-03-14 19:12:38 +01:00
|
|
|
trackEvent({
|
2022-08-16 18:39:23 +02:00
|
|
|
event: EVENT_NAMES.TOKEN_SCREEN_OPENED,
|
2022-04-22 18:09:10 +02:00
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
2022-08-16 18:39:23 +02:00
|
|
|
token_symbol: primaryCurrencyProperties.suffix,
|
|
|
|
location: 'Home',
|
2022-03-14 19:12:38 +01:00
|
|
|
},
|
|
|
|
});
|
2020-05-14 05:16:30 +02:00
|
|
|
}}
|
|
|
|
/>
|
2022-09-19 20:11:48 +02:00
|
|
|
{detectedTokens.length > 0 &&
|
|
|
|
!istokenDetectionInactiveOnNonMainnetSupportedNetwork && (
|
|
|
|
<DetectedTokensLink setShowDetectedTokens={setShowDetectedTokens} />
|
|
|
|
)}
|
2022-05-09 19:47:06 +02:00
|
|
|
<Box marginTop={detectedTokens.length > 0 ? 0 : 4}>
|
2023-02-02 21:15:26 +01:00
|
|
|
<Box justifyContent={JustifyContent.center}>
|
2021-09-10 20:03:42 +02:00
|
|
|
<Typography
|
2023-02-02 21:15:26 +01:00
|
|
|
color={Color.textAlternative}
|
|
|
|
variant={TypographyVariant.H6}
|
2021-09-10 20:03:42 +02:00
|
|
|
fontWeight={FONT_WEIGHT.NORMAL}
|
|
|
|
>
|
|
|
|
{t('missingToken')}
|
|
|
|
</Typography>
|
|
|
|
</Box>
|
2022-05-09 19:47:06 +02:00
|
|
|
<ImportTokenLink />
|
2021-09-10 20:03:42 +02:00
|
|
|
</Box>
|
2022-05-09 19:47:06 +02:00
|
|
|
{showDetectedTokens && (
|
|
|
|
<DetectedToken setShowDetectedTokens={setShowDetectedTokens} />
|
|
|
|
)}
|
2020-05-14 05:16:30 +02:00
|
|
|
</>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2020-05-14 05:16:30 +02:00
|
|
|
|
2020-06-01 19:54:32 +02:00
|
|
|
AssetList.propTypes = {
|
|
|
|
onClickAsset: PropTypes.func.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-06-01 19:54:32 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export default AssetList;
|