2021-02-04 19:15:23 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Button from '../../ui/button';
|
|
|
|
import LoadingScreen from '../../ui/loading-screen';
|
2021-06-10 21:27:03 +02:00
|
|
|
import { SECOND } from '../../../../shared/constants/time';
|
2022-09-21 22:10:34 +02:00
|
|
|
import { NETWORK_TYPES } from '../../../../shared/constants/network';
|
2018-12-06 20:39:47 +01:00
|
|
|
|
2018-12-10 18:30:50 +01:00
|
|
|
export default class LoadingNetworkScreen extends PureComponent {
|
|
|
|
state = {
|
|
|
|
showErrorScreen: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-12-06 20:39:47 +01:00
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-12-06 20:39:47 +01:00
|
|
|
|
2018-12-10 18:30:50 +01:00
|
|
|
static propTypes = {
|
|
|
|
loadingMessage: PropTypes.string,
|
|
|
|
cancelTime: PropTypes.number,
|
|
|
|
provider: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
|
|
|
providerId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
showNetworkDropdown: PropTypes.func,
|
|
|
|
setProviderArgs: PropTypes.array,
|
|
|
|
setProviderType: PropTypes.func,
|
2021-01-07 00:31:11 +01:00
|
|
|
rollbackToPreviousProvider: PropTypes.func,
|
2021-03-12 23:23:26 +01:00
|
|
|
isNetworkLoading: PropTypes.bool,
|
2022-09-29 05:26:01 +02:00
|
|
|
showDeprecatedRpcUrlWarning: PropTypes.bool,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-12-10 18:30:50 +01:00
|
|
|
|
|
|
|
componentDidMount = () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
this.cancelCallTimeout = setTimeout(
|
|
|
|
this.cancelCall,
|
2021-06-10 21:27:03 +02:00
|
|
|
this.props.cancelTime || SECOND * 15,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2018-12-06 20:39:47 +01:00
|
|
|
|
|
|
|
getConnectingLabel = function (loadingMessage) {
|
|
|
|
if (loadingMessage) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return loadingMessage;
|
2018-12-06 20:39:47 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const { provider, providerId } = this.props;
|
|
|
|
const providerName = provider.type;
|
2022-09-21 22:10:34 +02:00
|
|
|
const { t } = this.context;
|
2018-12-06 20:39:47 +01:00
|
|
|
|
2022-09-21 22:10:34 +02:00
|
|
|
switch (providerName) {
|
|
|
|
case NETWORK_TYPES.MAINNET:
|
|
|
|
return t('connectingToMainnet');
|
|
|
|
case NETWORK_TYPES.GOERLI:
|
|
|
|
return t('connectingToGoerli');
|
|
|
|
case NETWORK_TYPES.SEPOLIA:
|
|
|
|
return t('connectingToSepolia');
|
|
|
|
default:
|
|
|
|
return t('connectingTo', [providerId]);
|
2018-12-06 20:39:47 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-12-06 20:39:47 +01:00
|
|
|
|
2022-09-29 05:26:01 +02:00
|
|
|
renderDeprecatedRpcUrlWarning = () => {
|
|
|
|
const { showNetworkDropdown } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="loading-overlay__error-screen">
|
|
|
|
<span className="loading-overlay__emoji">😞</span>
|
|
|
|
<span>{this.context.t('currentRpcUrlDeprecated')}</span>
|
|
|
|
<div className="loading-overlay__error-buttons">
|
|
|
|
<Button
|
|
|
|
type="secondary"
|
|
|
|
onClick={() => {
|
|
|
|
window.clearTimeout(this.cancelCallTimeout);
|
|
|
|
showNetworkDropdown();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{this.context.t('switchNetworks')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-12-06 20:39:47 +01:00
|
|
|
renderErrorScreenContent = () => {
|
2022-07-31 20:26:40 +02:00
|
|
|
const { showNetworkDropdown, setProviderArgs, setProviderType } =
|
|
|
|
this.props;
|
2018-12-06 20:39:47 +01:00
|
|
|
|
2019-12-03 17:35:44 +01:00
|
|
|
return (
|
|
|
|
<div className="loading-overlay__error-screen">
|
|
|
|
<span className="loading-overlay__emoji">😞</span>
|
2020-11-03 00:41:28 +01:00
|
|
|
<span>{this.context.t('somethingWentWrong')}</span>
|
2019-12-03 17:35:44 +01:00
|
|
|
<div className="loading-overlay__error-buttons">
|
|
|
|
<Button
|
2021-10-05 21:20:42 +02:00
|
|
|
type="secondary"
|
2019-12-03 17:35:44 +01:00
|
|
|
onClick={() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
window.clearTimeout(this.cancelCallTimeout);
|
|
|
|
showNetworkDropdown();
|
2019-12-03 17:35:44 +01:00
|
|
|
}}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('switchNetworks')}
|
2019-12-03 17:35:44 +01:00
|
|
|
</Button>
|
2018-12-06 20:39:47 +01:00
|
|
|
|
2019-12-03 17:35:44 +01:00
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
onClick={() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ showErrorScreen: false });
|
|
|
|
setProviderType(...setProviderArgs);
|
|
|
|
window.clearTimeout(this.cancelCallTimeout);
|
2020-11-03 00:41:28 +01:00
|
|
|
this.cancelCallTimeout = setTimeout(
|
|
|
|
this.cancelCall,
|
2021-06-10 21:27:03 +02:00
|
|
|
this.props.cancelTime || SECOND * 15,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-12-03 17:35:44 +01:00
|
|
|
}}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('tryAgain')}
|
2019-12-03 17:35:44 +01:00
|
|
|
</Button>
|
|
|
|
</div>
|
2018-12-06 20:39:47 +01:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2018-12-06 20:39:47 +01:00
|
|
|
|
|
|
|
cancelCall = () => {
|
2021-03-12 23:23:26 +01:00
|
|
|
const { isNetworkLoading } = this.props;
|
2018-12-06 20:39:47 +01:00
|
|
|
|
2021-03-12 23:23:26 +01:00
|
|
|
if (isNetworkLoading) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ showErrorScreen: true });
|
2018-12-06 20:39:47 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-12-06 20:39:47 +01:00
|
|
|
|
|
|
|
componentDidUpdate = (prevProps) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { provider } = this.props;
|
|
|
|
const { provider: prevProvider } = prevProps;
|
2018-12-06 20:39:47 +01:00
|
|
|
if (provider.type !== prevProvider.type) {
|
2021-02-04 19:15:23 +01:00
|
|
|
window.clearTimeout(this.cancelCallTimeout);
|
|
|
|
this.setState({ showErrorScreen: false });
|
2020-11-03 00:41:28 +01:00
|
|
|
this.cancelCallTimeout = setTimeout(
|
|
|
|
this.cancelCall,
|
2021-06-10 21:27:03 +02:00
|
|
|
this.props.cancelTime || SECOND * 15,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-12-06 20:39:47 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-12-06 20:39:47 +01:00
|
|
|
|
|
|
|
componentWillUnmount = () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
window.clearTimeout(this.cancelCallTimeout);
|
|
|
|
};
|
2018-12-06 20:39:47 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2022-09-29 05:26:01 +02:00
|
|
|
const { rollbackToPreviousProvider, showDeprecatedRpcUrlWarning } =
|
|
|
|
this.props;
|
|
|
|
|
|
|
|
let loadingMessageToRender;
|
|
|
|
if (this.state.showErrorScreen) {
|
|
|
|
loadingMessageToRender = this.renderErrorScreenContent();
|
|
|
|
} else if (showDeprecatedRpcUrlWarning) {
|
|
|
|
loadingMessageToRender = this.renderDeprecatedRpcUrlWarning();
|
|
|
|
} else {
|
|
|
|
loadingMessageToRender = this.getConnectingLabel(
|
|
|
|
this.props.loadingMessage,
|
|
|
|
);
|
|
|
|
}
|
2018-12-06 20:39:47 +01:00
|
|
|
|
|
|
|
return (
|
2020-08-19 23:02:26 +02:00
|
|
|
<LoadingScreen
|
2020-11-03 00:41:28 +01:00
|
|
|
header={
|
2020-08-19 23:02:26 +02:00
|
|
|
<div
|
|
|
|
className="page-container__header-close"
|
2021-01-07 00:31:11 +01:00
|
|
|
onClick={rollbackToPreviousProvider}
|
2020-08-19 23:02:26 +02:00
|
|
|
/>
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2020-08-19 23:02:26 +02:00
|
|
|
showLoadingSpinner={!this.state.showErrorScreen}
|
2022-09-29 05:26:01 +02:00
|
|
|
loadingMessage={loadingMessageToRender}
|
2020-08-19 23:02:26 +02:00
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-12-06 20:39:47 +01:00
|
|
|
}
|
|
|
|
}
|