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 Typography from '../../ui/typography'; import { ALIGN_ITEMS, COLORS, DISPLAY, FLEX_DIRECTION, FONT_WEIGHT, TYPOGRAPHY, JUSTIFY_CONTENT, SIZES, } 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 { getFrequentRpcListDetail, getUnapprovedConfirmations, } from '../../../selectors'; import { ENVIRONMENT_TYPE_FULLSCREEN, ENVIRONMENT_TYPE_POPUP, MESSAGE_TYPE, } 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'; const AddNetwork = () => { const t = useContext(I18nContext); const dispatch = useDispatch(); const history = useHistory(); const frequentRpcList = useSelector(getFrequentRpcListDetail); const frequentRpcListChainIds = Object.values(frequentRpcList).map( (net) => net.chainId, ); const infuraRegex = /infura.io/u; const nets = FEATURED_RPCS.sort((a, b) => a.ticker > b.ticker ? 1 : -1, ).slice(0, FEATURED_RPCS.length); const notFrequentRpcNetworks = nets.filter( (net) => frequentRpcListChainIds.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(notFrequentRpcNetworks).length === 0 ? ( {t('youHaveAddedAll', [ {t('here')}. , , ])} ) : ( {getEnvironmentType() === ENVIRONMENT_TYPE_FULLSCREEN && ( {t('networks')} {' > '} {t('addANetwork')} )} {t('addFromAListOfPopularNetworks')} {t('popularCustomNetworks')} {notFrequentRpcNetworks.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;