You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
333 lines
9.9 KiB
333 lines
9.9 KiB
4 years ago
|
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 * as actions from '../../../store/actions';
|
||
|
import { openAlert as displayInvalidCustomNetworkAlert } from '../../../ducks/alerts/invalid-custom-network';
|
||
4 years ago
|
import {
|
||
|
NETWORKS_ROUTE,
|
||
|
NETWORKS_FORM_ROUTE,
|
||
4 years ago
|
} from '../../../helpers/constants/routes';
|
||
4 years ago
|
import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../shared/constants/app';
|
||
|
import { NETWORK_TYPE_RPC } from '../../../../shared/constants/network';
|
||
|
import { isPrefixedFormattedHexString } from '../../../../shared/modules/network.utils';
|
||
|
import { getEnvironmentType } from '../../../../app/scripts/lib/util';
|
||
4 years ago
|
|
||
4 years ago
|
import ColorIndicator from '../../ui/color-indicator';
|
||
|
import { COLORS, SIZES } from '../../../helpers/constants/design-system';
|
||
4 years ago
|
import { Dropdown, DropdownMenuItem } from './dropdown';
|
||
7 years ago
|
|
||
|
// classes from nodes of the toggle element.
|
||
|
const notToggleElementClassnames = [
|
||
|
'menu-icon',
|
||
|
'network-name',
|
||
|
'network-indicator',
|
||
|
'network-caret',
|
||
|
'network-component',
|
||
4 years ago
|
'modal-container__footer-button',
|
||
4 years ago
|
];
|
||
7 years ago
|
|
||
4 years ago
|
const DROP_DOWN_MENU_ITEM_STYLE = {
|
||
|
fontSize: '16px',
|
||
|
lineHeight: '20px',
|
||
|
padding: '12px 0',
|
||
4 years ago
|
};
|
||
4 years ago
|
|
||
4 years ago
|
function mapStateToProps(state) {
|
||
7 years ago
|
return {
|
||
7 years ago
|
provider: state.metamask.provider,
|
||
6 years ago
|
frequentRpcListDetail: state.metamask.frequentRpcListDetail || [],
|
||
7 years ago
|
networkDropdownOpen: state.appState.networkDropdownOpen,
|
||
4 years ago
|
};
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
function mapDispatchToProps(dispatch) {
|
||
7 years ago
|
return {
|
||
7 years ago
|
setProviderType: (type) => {
|
||
4 years ago
|
dispatch(actions.setProviderType(type));
|
||
7 years ago
|
},
|
||
4 years ago
|
setRpcTarget: (target, chainId, ticker, nickname) => {
|
||
4 years ago
|
dispatch(actions.setRpcTarget(target, chainId, ticker, nickname));
|
||
7 years ago
|
},
|
||
7 years ago
|
hideNetworkDropdown: () => dispatch(actions.hideNetworkDropdown()),
|
||
4 years ago
|
setNetworksTabAddMode: (isInAddMode) => {
|
||
4 years ago
|
dispatch(actions.setNetworksTabAddMode(isInAddMode));
|
||
4 years ago
|
},
|
||
4 years ago
|
setSelectedSettingsRpcUrl: (url) => {
|
||
4 years ago
|
dispatch(actions.setSelectedSettingsRpcUrl(url));
|
||
4 years ago
|
},
|
||
4 years ago
|
displayInvalidCustomNetworkAlert: (networkName) => {
|
||
4 years ago
|
dispatch(displayInvalidCustomNetworkAlert(networkName));
|
||
4 years ago
|
},
|
||
4 years ago
|
showConfirmDeleteNetworkModal: ({ target, onConfirm }) => {
|
||
4 years ago
|
return dispatch(
|
||
|
actions.showModal({
|
||
|
name: 'CONFIRM_DELETE_NETWORK',
|
||
|
target,
|
||
|
onConfirm,
|
||
|
}),
|
||
4 years ago
|
);
|
||
4 years ago
|
},
|
||
4 years ago
|
};
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
class NetworkDropdown extends Component {
|
||
|
static contextTypes = {
|
||
|
t: PropTypes.func,
|
||
|
metricsEvent: PropTypes.func,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
static propTypes = {
|
||
|
provider: PropTypes.shape({
|
||
5 years ago
|
nickname: PropTypes.string,
|
||
4 years ago
|
rpcUrl: PropTypes.string,
|
||
5 years ago
|
type: PropTypes.string,
|
||
|
ticker: PropTypes.string,
|
||
|
}).isRequired,
|
||
|
setProviderType: PropTypes.func.isRequired,
|
||
|
setRpcTarget: PropTypes.func.isRequired,
|
||
|
hideNetworkDropdown: PropTypes.func.isRequired,
|
||
|
setNetworksTabAddMode: PropTypes.func.isRequired,
|
||
4 years ago
|
setSelectedSettingsRpcUrl: PropTypes.func.isRequired,
|
||
5 years ago
|
frequentRpcListDetail: PropTypes.array.isRequired,
|
||
|
networkDropdownOpen: PropTypes.bool.isRequired,
|
||
|
history: PropTypes.object.isRequired,
|
||
4 years ago
|
displayInvalidCustomNetworkAlert: PropTypes.func.isRequired,
|
||
4 years ago
|
showConfirmDeleteNetworkModal: PropTypes.func.isRequired,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
4 years ago
|
handleClick(newProviderType) {
|
||
|
const {
|
||
|
provider: { type: providerType },
|
||
|
setProviderType,
|
||
4 years ago
|
} = this.props;
|
||
|
const { metricsEvent } = this.context;
|
||
7 years ago
|
|
||
5 years ago
|
metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Navigation',
|
||
|
action: 'Home',
|
||
|
name: 'Switched Networks',
|
||
|
},
|
||
|
customVariables: {
|
||
|
fromNetwork: providerType,
|
||
|
toNetwork: newProviderType,
|
||
|
},
|
||
4 years ago
|
});
|
||
|
setProviderType(newProviderType);
|
||
5 years ago
|
}
|
||
7 years ago
|
|
||
4 years ago
|
renderCustomRpcList(rpcListDetail, provider) {
|
||
4 years ago
|
const reversedRpcListDetail = rpcListDetail.slice().reverse();
|
||
5 years ago
|
|
||
5 years ago
|
return reversedRpcListDetail.map((entry) => {
|
||
4 years ago
|
const { rpcUrl, chainId, ticker = 'ETH', nickname = '' } = entry;
|
||
4 years ago
|
const isCurrentRpcTarget =
|
||
4 years ago
|
provider.type === NETWORK_TYPE_RPC && rpcUrl === provider.rpcUrl;
|
||
7 years ago
|
|
||
4 years ago
|
return (
|
||
|
<DropdownMenuItem
|
||
4 years ago
|
key={`common${rpcUrl}`}
|
||
4 years ago
|
closeMenu={() => this.props.hideNetworkDropdown()}
|
||
4 years ago
|
onClick={() => {
|
||
|
if (isPrefixedFormattedHexString(chainId)) {
|
||
4 years ago
|
this.props.setRpcTarget(rpcUrl, chainId, ticker, nickname);
|
||
4 years ago
|
} else {
|
||
4 years ago
|
this.props.displayInvalidCustomNetworkAlert(nickname || rpcUrl);
|
||
4 years ago
|
}
|
||
|
}}
|
||
4 years ago
|
style={{
|
||
|
fontSize: '16px',
|
||
|
lineHeight: '20px',
|
||
|
padding: '12px 0',
|
||
|
}}
|
||
|
>
|
||
4 years ago
|
{isCurrentRpcTarget ? (
|
||
|
<i className="fa fa-check" />
|
||
|
) : (
|
||
|
<div className="network-check__transparent">✓</div>
|
||
|
)}
|
||
4 years ago
|
<ColorIndicator
|
||
|
color={COLORS.UI2}
|
||
4 years ago
|
size={SIZES.LG}
|
||
4 years ago
|
type={ColorIndicator.TYPES.FILLED}
|
||
|
borderColor={isCurrentRpcTarget ? COLORS.WHITE : COLORS.UI2}
|
||
4 years ago
|
/>
|
||
4 years ago
|
<span
|
||
|
className="network-name-item"
|
||
5 years ago
|
style={{
|
||
4 years ago
|
color: isCurrentRpcTarget ? '#ffffff' : '#9b9b9b',
|
||
5 years ago
|
}}
|
||
|
>
|
||
4 years ago
|
{nickname || rpcUrl}
|
||
4 years ago
|
</span>
|
||
4 years ago
|
{isCurrentRpcTarget ? null : (
|
||
|
<i
|
||
|
className="fa fa-times delete"
|
||
|
onClick={(e) => {
|
||
4 years ago
|
e.stopPropagation();
|
||
4 years ago
|
this.props.showConfirmDeleteNetworkModal({
|
||
|
target: rpcUrl,
|
||
|
onConfirm: () => undefined,
|
||
4 years ago
|
});
|
||
4 years ago
|
}}
|
||
|
/>
|
||
|
)}
|
||
4 years ago
|
</DropdownMenuItem>
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
}
|
||
6 years ago
|
|
||
4 years ago
|
getNetworkName() {
|
||
4 years ago
|
const { provider } = this.props;
|
||
|
const providerName = provider.type;
|
||
7 years ago
|
|
||
4 years ago
|
let name;
|
||
7 years ago
|
|
||
5 years ago
|
if (providerName === 'mainnet') {
|
||
4 years ago
|
name = this.context.t('mainnet');
|
||
5 years ago
|
} else if (providerName === 'ropsten') {
|
||
4 years ago
|
name = this.context.t('ropsten');
|
||
5 years ago
|
} else if (providerName === 'kovan') {
|
||
4 years ago
|
name = this.context.t('kovan');
|
||
5 years ago
|
} else if (providerName === 'rinkeby') {
|
||
4 years ago
|
name = this.context.t('rinkeby');
|
||
5 years ago
|
} else if (providerName === 'goerli') {
|
||
4 years ago
|
name = this.context.t('goerli');
|
||
5 years ago
|
} else {
|
||
4 years ago
|
name = provider.nickname || this.context.t('unknownNetwork');
|
||
5 years ago
|
}
|
||
7 years ago
|
|
||
4 years ago
|
return name;
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
renderNetworkEntry(network) {
|
||
|
const {
|
||
|
provider: { type: providerType },
|
||
4 years ago
|
} = this.props;
|
||
4 years ago
|
return (
|
||
|
<DropdownMenuItem
|
||
|
key={network}
|
||
|
closeMenu={this.props.hideNetworkDropdown}
|
||
|
onClick={() => this.handleClick(network)}
|
||
|
style={DROP_DOWN_MENU_ITEM_STYLE}
|
||
|
>
|
||
|
{providerType === network ? (
|
||
|
<i className="fa fa-check" />
|
||
|
) : (
|
||
|
<div className="network-check__transparent">✓</div>
|
||
|
)}
|
||
|
<ColorIndicator
|
||
|
color={network}
|
||
4 years ago
|
size={SIZES.LG}
|
||
4 years ago
|
type={ColorIndicator.TYPES.FILLED}
|
||
|
borderColor={providerType === network ? COLORS.WHITE : network}
|
||
|
/>
|
||
|
<span
|
||
|
className="network-name-item"
|
||
|
style={{
|
||
|
color: providerType === network ? '#ffffff' : '#9b9b9b',
|
||
|
}}
|
||
|
>
|
||
|
{this.context.t(network)}
|
||
|
</span>
|
||
|
</DropdownMenuItem>
|
||
4 years ago
|
);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
render() {
|
||
4 years ago
|
const {
|
||
4 years ago
|
provider: { rpcUrl: activeNetwork },
|
||
4 years ago
|
setNetworksTabAddMode,
|
||
|
setSelectedSettingsRpcUrl,
|
||
4 years ago
|
} = this.props;
|
||
|
const rpcListDetail = this.props.frequentRpcListDetail;
|
||
|
const isOpen = this.props.networkDropdownOpen;
|
||
7 years ago
|
|
||
5 years ago
|
return (
|
||
|
<Dropdown
|
||
|
isOpen={isOpen}
|
||
|
onClickOutside={(event) => {
|
||
4 years ago
|
const { classList } = event.target;
|
||
|
const isInClassList = (className) => classList.contains(className);
|
||
4 years ago
|
const notToggleElementIndex = notToggleElementClassnames.findIndex(
|
||
|
isInClassList,
|
||
4 years ago
|
);
|
||
6 years ago
|
|
||
5 years ago
|
if (notToggleElementIndex === -1) {
|
||
4 years ago
|
event.stopPropagation();
|
||
|
this.props.hideNetworkDropdown();
|
||
5 years ago
|
}
|
||
|
}}
|
||
|
containerClassName="network-droppo"
|
||
|
zIndex={55}
|
||
|
style={{
|
||
|
position: 'absolute',
|
||
|
top: '58px',
|
||
|
width: '309px',
|
||
|
zIndex: '55px',
|
||
|
}}
|
||
|
innerStyle={{
|
||
|
padding: '18px 8px',
|
||
|
}}
|
||
|
>
|
||
|
<div className="network-dropdown-header">
|
||
|
<div className="network-dropdown-title">
|
||
|
{this.context.t('networks')}
|
||
|
</div>
|
||
|
<div className="network-dropdown-divider" />
|
||
|
<div className="network-dropdown-content">
|
||
|
{this.context.t('defaultNetwork')}
|
||
|
</div>
|
||
|
</div>
|
||
4 years ago
|
{this.renderNetworkEntry('mainnet')}
|
||
|
{this.renderNetworkEntry('ropsten')}
|
||
|
{this.renderNetworkEntry('kovan')}
|
||
|
{this.renderNetworkEntry('rinkeby')}
|
||
|
{this.renderNetworkEntry('goerli')}
|
||
|
|
||
4 years ago
|
{this.renderCustomRpcList(rpcListDetail, this.props.provider)}
|
||
5 years ago
|
<DropdownMenuItem
|
||
|
closeMenu={() => this.props.hideNetworkDropdown()}
|
||
|
onClick={() => {
|
||
4 years ago
|
this.props.history.push(
|
||
|
getEnvironmentType() === ENVIRONMENT_TYPE_FULLSCREEN
|
||
|
? NETWORKS_ROUTE
|
||
|
: NETWORKS_FORM_ROUTE,
|
||
4 years ago
|
);
|
||
|
setSelectedSettingsRpcUrl('');
|
||
|
setNetworksTabAddMode(true);
|
||
5 years ago
|
}}
|
||
4 years ago
|
style={DROP_DOWN_MENU_ITEM_STYLE}
|
||
5 years ago
|
>
|
||
4 years ago
|
{activeNetwork === 'custom' ? (
|
||
|
<i className="fa fa-check" />
|
||
|
) : (
|
||
|
<div className="network-check__transparent">✓</div>
|
||
|
)}
|
||
4 years ago
|
<ColorIndicator
|
||
|
type={ColorIndicator.TYPES.FILLED}
|
||
|
color={COLORS.TRANSPARENT}
|
||
|
borderColor={COLORS.UI2}
|
||
4 years ago
|
size={SIZES.LG}
|
||
4 years ago
|
/>
|
||
5 years ago
|
<span
|
||
|
className="network-name-item"
|
||
|
style={{
|
||
4 years ago
|
color: activeNetwork === 'custom' ? '#ffffff' : '#9b9b9b',
|
||
5 years ago
|
}}
|
||
|
>
|
||
5 years ago
|
{this.context.t('customRPC')}
|
||
5 years ago
|
</span>
|
||
|
</DropdownMenuItem>
|
||
5 years ago
|
</Dropdown>
|
||
4 years ago
|
);
|
||
7 years ago
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
export default compose(
|
||
|
withRouter,
|
||
4 years ago
|
connect(mapStateToProps, mapDispatchToProps),
|
||
4 years ago
|
)(NetworkDropdown);
|