2022-05-11 22:27:58 +02:00
|
|
|
import React, { useState, useContext } from 'react';
|
2022-05-09 19:47:06 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useSelector, useDispatch } from 'react-redux';
|
|
|
|
import { chain } from 'lodash';
|
|
|
|
|
|
|
|
import {
|
2022-07-18 16:43:30 +02:00
|
|
|
addImportedTokens,
|
2022-05-09 19:47:06 +02:00
|
|
|
ignoreTokens,
|
|
|
|
setNewTokensImported,
|
|
|
|
} from '../../../store/actions';
|
|
|
|
import { getDetectedTokensInCurrentNetwork } from '../../../selectors';
|
2022-05-11 22:27:58 +02:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
2022-05-09 19:47:06 +02:00
|
|
|
|
2022-07-19 18:07:13 +02:00
|
|
|
import {
|
|
|
|
ASSET_TYPES,
|
|
|
|
TOKEN_STANDARDS,
|
|
|
|
} from '../../../../shared/constants/transaction';
|
2022-05-11 22:27:58 +02:00
|
|
|
import { EVENT, EVENT_NAMES } from '../../../../shared/constants/metametrics';
|
2022-05-09 19:47:06 +02:00
|
|
|
import DetectedTokenSelectionPopover from './detected-token-selection-popover/detected-token-selection-popover';
|
|
|
|
import DetectedTokenIgnoredPopover from './detected-token-ignored-popover/detected-token-ignored-popover';
|
|
|
|
|
|
|
|
const sortingBasedOnTokenSelection = (tokensDetected) => {
|
|
|
|
return (
|
|
|
|
chain(tokensDetected)
|
|
|
|
// get the values
|
|
|
|
.values()
|
|
|
|
// create a new object with keys 'selected', 'deselected' and group the tokens
|
|
|
|
.groupBy((token) => (token.selected ? 'selected' : 'deselected'))
|
|
|
|
// ditch the 'selected' property and get just the tokens'
|
2022-08-10 03:26:25 +02:00
|
|
|
.mapValues((group) =>
|
|
|
|
group.map(({ token }) => {
|
|
|
|
const { address, symbol, decimals, aggregators } = token;
|
|
|
|
return { address, symbol, decimals, aggregators };
|
|
|
|
}),
|
|
|
|
)
|
2022-05-09 19:47:06 +02:00
|
|
|
// Exit the chain and get the underlying value, an object.
|
|
|
|
.value()
|
|
|
|
);
|
|
|
|
};
|
2022-05-11 22:27:58 +02:00
|
|
|
|
2022-05-09 19:47:06 +02:00
|
|
|
const DetectedToken = ({ setShowDetectedTokens }) => {
|
|
|
|
const dispatch = useDispatch();
|
2022-05-11 22:27:58 +02:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2022-05-09 19:47:06 +02:00
|
|
|
|
|
|
|
const detectedTokens = useSelector(getDetectedTokensInCurrentNetwork);
|
|
|
|
|
|
|
|
const [tokensListDetected, setTokensListDetected] = useState(() =>
|
|
|
|
detectedTokens.reduce((tokenObj, token) => {
|
|
|
|
tokenObj[token.address] = { token, selected: true };
|
|
|
|
return tokenObj;
|
|
|
|
}, {}),
|
|
|
|
);
|
2022-07-31 20:26:40 +02:00
|
|
|
const [showDetectedTokenIgnoredPopover, setShowDetectedTokenIgnoredPopover] =
|
|
|
|
useState(false);
|
2022-08-10 03:26:25 +02:00
|
|
|
const [partiallyIgnoreDetectedTokens, setPartiallyIgnoreDetectedTokens] =
|
|
|
|
useState(false);
|
2022-05-09 19:47:06 +02:00
|
|
|
|
2022-05-11 22:27:58 +02:00
|
|
|
const importSelectedTokens = async (selectedTokens) => {
|
|
|
|
selectedTokens.forEach((importedToken) => {
|
|
|
|
trackEvent({
|
|
|
|
event: EVENT_NAMES.TOKEN_ADDED,
|
|
|
|
category: EVENT.CATEGORIES.WALLET,
|
|
|
|
sensitiveProperties: {
|
|
|
|
token_symbol: importedToken.symbol,
|
|
|
|
token_contract_address: importedToken.address,
|
|
|
|
token_decimal_precision: importedToken.decimals,
|
|
|
|
source: EVENT.SOURCE.TOKEN.DETECTED,
|
|
|
|
token_standard: TOKEN_STANDARDS.ERC20,
|
|
|
|
asset_type: ASSET_TYPES.TOKEN,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2022-07-18 16:43:30 +02:00
|
|
|
await dispatch(addImportedTokens(selectedTokens));
|
2022-05-11 22:27:58 +02:00
|
|
|
const tokenSymbols = selectedTokens.map(({ symbol }) => symbol);
|
|
|
|
dispatch(setNewTokensImported(tokenSymbols.join(', ')));
|
|
|
|
};
|
|
|
|
|
2022-05-09 19:47:06 +02:00
|
|
|
const handleClearTokensSelection = async () => {
|
2022-07-31 20:26:40 +02:00
|
|
|
const { selected: selectedTokens = [], deselected: deSelectedTokens = [] } =
|
|
|
|
sortingBasedOnTokenSelection(tokensListDetected);
|
2022-05-09 19:47:06 +02:00
|
|
|
|
|
|
|
if (deSelectedTokens.length < detectedTokens.length) {
|
2022-05-11 22:27:58 +02:00
|
|
|
await importSelectedTokens(selectedTokens);
|
2022-05-09 19:47:06 +02:00
|
|
|
}
|
2022-05-11 22:27:58 +02:00
|
|
|
const tokensDetailsList = deSelectedTokens.map(
|
|
|
|
({ symbol, address }) => `${symbol} - ${address}`,
|
|
|
|
);
|
|
|
|
trackEvent({
|
|
|
|
event: EVENT_NAMES.TOKEN_HIDDEN,
|
|
|
|
category: EVENT.CATEGORIES.WALLET,
|
|
|
|
sensitiveProperties: {
|
|
|
|
tokens: tokensDetailsList,
|
|
|
|
location: EVENT.LOCATION.TOKEN_DETECTION,
|
|
|
|
token_standard: TOKEN_STANDARDS.ERC20,
|
|
|
|
asset_type: ASSET_TYPES.TOKEN,
|
|
|
|
},
|
|
|
|
});
|
2022-07-18 16:43:30 +02:00
|
|
|
const deSelectedTokensAddresses = deSelectedTokens.map(
|
|
|
|
({ address }) => address,
|
|
|
|
);
|
|
|
|
await dispatch(
|
|
|
|
ignoreTokens({
|
|
|
|
tokensToIgnore: deSelectedTokensAddresses,
|
|
|
|
dontShowLoadingIndicator: true,
|
|
|
|
}),
|
|
|
|
);
|
2022-05-09 19:47:06 +02:00
|
|
|
setShowDetectedTokens(false);
|
2022-08-10 03:26:25 +02:00
|
|
|
setPartiallyIgnoreDetectedTokens(false);
|
2022-05-09 19:47:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleTokenSelection = (token) => {
|
|
|
|
setTokensListDetected((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
[token.address]: {
|
|
|
|
...prevState[token.address],
|
|
|
|
selected: !prevState[token.address].selected,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
const onImport = async () => {
|
2022-07-31 20:26:40 +02:00
|
|
|
const { selected: selectedTokens = [] } =
|
|
|
|
sortingBasedOnTokenSelection(tokensListDetected);
|
2022-05-09 19:47:06 +02:00
|
|
|
|
|
|
|
if (selectedTokens.length < detectedTokens.length) {
|
|
|
|
setShowDetectedTokenIgnoredPopover(true);
|
2022-08-10 03:26:25 +02:00
|
|
|
setPartiallyIgnoreDetectedTokens(true);
|
2022-05-09 19:47:06 +02:00
|
|
|
} else {
|
2022-05-11 22:27:58 +02:00
|
|
|
await importSelectedTokens(selectedTokens);
|
2022-05-09 19:47:06 +02:00
|
|
|
setShowDetectedTokens(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onIgnoreAll = () => {
|
|
|
|
const newTokensListDetected = { ...tokensListDetected };
|
|
|
|
for (const tokenAddress of Object.keys(tokensListDetected)) {
|
|
|
|
newTokensListDetected[tokenAddress].selected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTokensListDetected(newTokensListDetected);
|
|
|
|
setShowDetectedTokenIgnoredPopover(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onCancelIgnore = () => {
|
|
|
|
setShowDetectedTokenIgnoredPopover(false);
|
2022-08-10 03:26:25 +02:00
|
|
|
setPartiallyIgnoreDetectedTokens(false);
|
2022-05-09 19:47:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{showDetectedTokenIgnoredPopover && (
|
|
|
|
<DetectedTokenIgnoredPopover
|
|
|
|
onCancelIgnore={onCancelIgnore}
|
|
|
|
handleClearTokensSelection={handleClearTokensSelection}
|
2022-08-10 03:26:25 +02:00
|
|
|
partiallyIgnoreDetectedTokens={partiallyIgnoreDetectedTokens}
|
2022-05-09 19:47:06 +02:00
|
|
|
/>
|
|
|
|
)}
|
2022-09-19 19:40:05 +02:00
|
|
|
{detectedTokens.length > 0 && (
|
|
|
|
<DetectedTokenSelectionPopover
|
|
|
|
detectedTokens={detectedTokens}
|
|
|
|
tokensListDetected={tokensListDetected}
|
|
|
|
handleTokenSelection={handleTokenSelection}
|
|
|
|
onImport={onImport}
|
|
|
|
onIgnoreAll={onIgnoreAll}
|
|
|
|
setShowDetectedTokens={setShowDetectedTokens}
|
|
|
|
sortingBasedOnTokenSelection={sortingBasedOnTokenSelection}
|
|
|
|
/>
|
|
|
|
)}
|
2022-05-09 19:47:06 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DetectedToken.propTypes = {
|
|
|
|
setShowDetectedTokens: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DetectedToken;
|