import React, { useContext, useEffect, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { useHistory } from 'react-router-dom'; import { I18nContext } from '../../../contexts/i18n'; import Box from '../../ui/box'; import { AlignItems, DISPLAY, FLEX_DIRECTION, TextVariant, JustifyContent, BorderRadius, BackgroundColor, TextColor, IconColor, } from '../../../helpers/constants/design-system'; import Button from '../../ui/button'; import Tooltip from '../../ui/tooltip'; import IconWithFallback from '../../ui/icon-with-fallback'; import IconBorder from '../../ui/icon-border'; import { getNetworkConfigurations, getUnapprovedConfirmations, } from '../../../selectors'; import { ENVIRONMENT_TYPE_FULLSCREEN, ENVIRONMENT_TYPE_POPUP, MESSAGE_TYPE, ORIGIN_METAMASK, } from '../../../../shared/constants/app'; import { requestUserApproval } from '../../../store/actions'; import Popover from '../../ui/popover'; import ConfirmationPage from '../../../pages/confirmation/confirmation'; import { FEATURED_RPCS } from '../../../../shared/constants/network'; import { ADD_NETWORK_ROUTE } from '../../../helpers/constants/routes'; import { getEnvironmentType } from '../../../../app/scripts/lib/util'; import ZENDESK_URLS from '../../../helpers/constants/zendesk-url'; import { Text } from '../../component-library'; import { Icon, ICON_NAMES, ICON_SIZES, } from '../../component-library/icon/deprecated'; import { MetaMetricsNetworkEventSource } from '../../../../shared/constants/metametrics'; const AddNetwork = () => { const t = useContext(I18nContext); const dispatch = useDispatch(); const history = useHistory(); const networkConfigurations = useSelector(getNetworkConfigurations); const networkConfigurationChainIds = Object.values(networkConfigurations).map( (net) => net.chainId, ); const infuraRegex = /infura.io/u; const nets = FEATURED_RPCS.sort((a, b) => a.nickname > b.nickname ? 1 : -1, ).slice(0, FEATURED_RPCS.length); const notExistingNetworkConfigurations = nets.filter( (net) => networkConfigurationChainIds.indexOf(net.chainId) === -1, ); const unapprovedConfirmations = useSelector(getUnapprovedConfirmations); const [showPopover, setShowPopover] = useState(false); useEffect(() => { const anAddNetworkConfirmationFromMetaMaskExists = unapprovedConfirmations?.find((confirmation) => { return ( confirmation.origin === 'metamask' && confirmation.type === MESSAGE_TYPE.ADD_ETHEREUM_CHAIN ); }); if (!showPopover && anAddNetworkConfirmationFromMetaMaskExists) { setShowPopover(true); } if (showPopover && !anAddNetworkConfirmationFromMetaMaskExists) { setShowPopover(false); } }, [unapprovedConfirmations, showPopover]); return ( <> {Object.keys(notExistingNetworkConfigurations).length === 0 ? ( {t('youHaveAddedAll', [ {t('here')}. , , ])} ) : ( {getEnvironmentType() === ENVIRONMENT_TYPE_FULLSCREEN && ( {t('networks')} {' > '} {t('addANetwork')} )} {t('addFromAListOfPopularNetworks')} {t('popularCustomNetworks')} {notExistingNetworkConfigurations.map((item, index) => ( {item.nickname} { // Warning for the networks that doesn't use infura.io as the RPC !infuraRegex.test(item.rpcUrl) && ( {t('addNetworkTooltipWarning', [ {t('learnMoreUpperCase')} , ])} } trigger="mouseenter" > ) } ))} )} {showPopover && ( )} ); }; export default AddNetwork;