import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { connect } from 'react-redux'; import { withRouter } from 'react-router-dom'; import { compose } from 'redux'; import Button from '../../ui/button'; import * as actions from '../../../store/actions'; import { openAlert as displayInvalidCustomNetworkAlert } from '../../../ducks/alerts/invalid-custom-network'; import { LOCALHOST_RPC_URL, NETWORK_TYPES, } from '../../../../shared/constants/network'; import { isPrefixedFormattedHexString } from '../../../../shared/modules/network.utils'; import ColorIndicator from '../../ui/color-indicator'; import { COLORS, SIZES } from '../../../helpers/constants/design-system'; import { getShowTestNetworks } from '../../../selectors'; import { getEnvironmentType } from '../../../../app/scripts/lib/util'; import { ENVIRONMENT_TYPE_POPUP } from '../../../../shared/constants/app'; import { EVENT, EVENT_NAMES } from '../../../../shared/constants/metametrics'; import { ADD_POPULAR_CUSTOM_NETWORK, ADVANCED_ROUTE, } from '../../../helpers/constants/routes'; import IconCheck from '../../ui/icon/icon-check'; import { Dropdown, DropdownMenuItem } from './dropdown'; // classes from nodes of the toggle element. const notToggleElementClassnames = [ 'menu-icon', 'network-name', 'network-indicator', 'network-caret', 'network-component', 'modal-container__footer-button', ]; const DROP_DOWN_MENU_ITEM_STYLE = { fontSize: '16px', lineHeight: '20px', padding: '16px', }; function mapStateToProps(state) { return { provider: state.metamask.provider, shouldShowTestNetworks: getShowTestNetworks(state), frequentRpcListDetail: state.metamask.frequentRpcListDetail || [], networkDropdownOpen: state.appState.networkDropdownOpen, showTestnetMessageInDropdown: state.metamask.showTestnetMessageInDropdown, }; } function mapDispatchToProps(dispatch) { return { setProviderType: (type) => { dispatch(actions.setProviderType(type)); }, setRpcTarget: (target, chainId, ticker, nickname) => { dispatch(actions.setRpcTarget(target, chainId, ticker, nickname)); }, hideNetworkDropdown: () => dispatch(actions.hideNetworkDropdown()), displayInvalidCustomNetworkAlert: (networkName) => { dispatch(displayInvalidCustomNetworkAlert(networkName)); }, showConfirmDeleteNetworkModal: ({ target, onConfirm }) => { return dispatch( actions.showModal({ name: 'CONFIRM_DELETE_NETWORK', target, onConfirm, }), ); }, hideTestNetMessage: () => actions.hideTestNetMessage(), }; } class NetworkDropdown extends Component { static contextTypes = { t: PropTypes.func, trackEvent: PropTypes.func, }; static propTypes = { provider: PropTypes.shape({ nickname: PropTypes.string, rpcUrl: PropTypes.string, type: PropTypes.string, ticker: PropTypes.string, }).isRequired, setProviderType: PropTypes.func.isRequired, setRpcTarget: PropTypes.func.isRequired, hideNetworkDropdown: PropTypes.func.isRequired, frequentRpcListDetail: PropTypes.array.isRequired, shouldShowTestNetworks: PropTypes.bool, networkDropdownOpen: PropTypes.bool.isRequired, displayInvalidCustomNetworkAlert: PropTypes.func.isRequired, showConfirmDeleteNetworkModal: PropTypes.func.isRequired, showTestnetMessageInDropdown: PropTypes.bool.isRequired, hideTestNetMessage: PropTypes.func.isRequired, history: PropTypes.object, dropdownStyles: PropTypes.object, hideElementsForOnboarding: PropTypes.bool, onAddClick: PropTypes.func, }; handleClick(newProviderType) { const { provider: { type: providerType }, setProviderType, } = this.props; const { trackEvent } = this.context; trackEvent({ category: EVENT.CATEGORIES.NAVIGATION, event: EVENT_NAMES.NAV_NETWORK_SWITCHED, properties: { from_network: providerType, to_network: newProviderType, }, }); setProviderType(newProviderType); } renderAddCustomButton() { const { onAddClick } = this.props; return (