1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-04 23:14:56 +01:00
metamask-extension/ui/components/app/detected-token/detected-token-values/detected-token-values.js

72 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-05-09 19:47:06 +02:00
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import Box from '../../../ui/box';
import Typography from '../../../ui/typography';
import CheckBox from '../../../ui/check-box';
import {
COLORS,
DISPLAY,
TYPOGRAPHY,
} from '../../../../helpers/constants/design-system';
import { useTokenTracker } from '../../../../hooks/useTokenTracker';
import { useTokenFiatAmount } from '../../../../hooks/useTokenFiatAmount';
2022-05-09 19:47:06 +02:00
const DetectedTokenValues = ({
token,
handleTokenSelection,
tokensListDetected,
}) => {
const [tokenSelection, setTokenSelection] = useState(() => {
return tokensListDetected[token.address]?.selected;
});
const { tokensWithBalances } = useTokenTracker([token]);
2022-05-09 19:47:06 +02:00
const balanceString = tokensWithBalances[0]?.string;
const formattedFiatBalance = useTokenFiatAmount(
token.address,
2022-05-09 19:47:06 +02:00
balanceString,
token.symbol,
);
2022-05-09 19:47:06 +02:00
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}>
<Typography variant={TYPOGRAPHY.H4}>
2022-05-09 19:47:06 +02:00
{`${balanceString || '0'} ${token.symbol}`}
</Typography>
<Typography variant={TYPOGRAPHY.H7} color={COLORS.TEXT_ALTERNATIVE}>
{formattedFiatBalance || '$0'}
</Typography>
</Box>
<Box className="detected-token-values__checkbox">
2022-05-09 19:47:06 +02:00
<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,
}),
2022-05-09 19:47:06 +02:00
handleTokenSelection: PropTypes.func.isRequired,
tokensListDetected: PropTypes.object,
};
export default DetectedTokenValues;