1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 15:50:28 +01:00
metamask-extension/ui/components/app/detected-token/detected-token-values/detected-token-values.js
Harsh Shukla 98cb3381e6
Part of #17670: Replace Typography with Text component in: detected-token-values.js (#18977)
* Update detected-token-values.js

* Update ui/components/app/detected-token/detected-token-values/detected-token-values.js

Co-authored-by: George Marshall <georgewrmarshall@gmail.com>

---------

Co-authored-by: George Marshall <georgewrmarshall@gmail.com>
2023-05-03 08:17:49 -05:00

82 lines
2.5 KiB
JavaScript

import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import Box from '../../../ui/box';
import CheckBox from '../../../ui/check-box';
import {
DISPLAY,
TextColor,
TextVariant,
} from '../../../../helpers/constants/design-system';
import { useTokenTracker } from '../../../../hooks/useTokenTracker';
import { useTokenFiatAmount } from '../../../../hooks/useTokenFiatAmount';
import { getUseCurrencyRateCheck } from '../../../../selectors';
import { Text } from '../../../component-library';
const DetectedTokenValues = ({
token,
handleTokenSelection,
tokensListDetected,
}) => {
const [tokenSelection, setTokenSelection] = useState(() => {
return tokensListDetected[token.address]?.selected;
});
const { tokensWithBalances } = useTokenTracker([token]);
const balanceString = tokensWithBalances[0]?.string;
const formattedFiatBalance = useTokenFiatAmount(
token.address,
balanceString,
token.symbol,
);
const useCurrencyRateCheck = useSelector(getUseCurrencyRateCheck);
useEffect(() => {
setTokenSelection(tokensListDetected[token.address]?.selected);
}, [tokensListDetected, token.address, tokenSelection, setTokenSelection]);
const handleCheckBoxSelection = () => {
setTokenSelection(!tokenSelection);
handleTokenSelection(token);
};
return (
<Box display={DISPLAY.INLINE_FLEX} className="detected-token-values">
<Box marginBottom={1}>
<Text variant={TextVariant.bodyLgMedium} as="h4">
{`${balanceString || '0'} ${token.symbol}`}
</Text>
<Text
variant={TextVariant.bodySm}
as="h6"
color={TextColor.textAlternative}
>
{useCurrencyRateCheck
? formattedFiatBalance || '$0' // since formattedFiatBalance will be when teh conversion rate is not obtained, should be replace the `$0` with `N/A`
: formattedFiatBalance}
</Text>
</Box>
<Box className="detected-token-values__checkbox">
<CheckBox checked={tokenSelection} onClick={handleCheckBoxSelection} />
</Box>
</Box>
);
};
DetectedTokenValues.propTypes = {
token: PropTypes.shape({
address: PropTypes.string.isRequired,
decimals: PropTypes.number,
symbol: PropTypes.string,
iconUrl: PropTypes.string,
aggregators: PropTypes.array,
}),
handleTokenSelection: PropTypes.func.isRequired,
tokensListDetected: PropTypes.object,
};
export default DetectedTokenValues;