2021-02-04 19:15:23 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
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';
|
2023-05-05 16:02:28 +02:00
|
|
|
import Popover from '../../ui/popover/popover.component';
|
|
|
|
import {
|
|
|
|
ButtonPrimary,
|
|
|
|
ButtonSecondary,
|
|
|
|
Icon,
|
|
|
|
IconName,
|
|
|
|
IconSize,
|
2023-07-20 00:32:35 +02:00
|
|
|
Text,
|
2023-05-05 16:02:28 +02:00
|
|
|
} from '../../component-library';
|
|
|
|
import {
|
|
|
|
DISPLAY,
|
|
|
|
IconColor,
|
|
|
|
TextAlign,
|
|
|
|
TextVariant,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
|
|
|
import Box from '../../ui/box/box';
|
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,
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: PropTypes.object,
|
2018-12-10 18:30:50 +01:00
|
|
|
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
|
|
|
}
|
2023-05-02 17:53:20 +02:00
|
|
|
const { providerConfig, providerId } = this.props;
|
|
|
|
const providerName = providerConfig.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');
|
2023-06-15 13:38:07 +02:00
|
|
|
case NETWORK_TYPES.LINEA_GOERLI:
|
|
|
|
return t('connectingToLineaGoerli');
|
2023-06-16 18:35:33 +02:00
|
|
|
case NETWORK_TYPES.LINEA_MAINNET:
|
|
|
|
return t('connectingToLineaMainnet');
|
2022-09-21 22:10:34 +02:00
|
|
|
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
|
|
|
|
2023-05-05 16:02:28 +02:00
|
|
|
renderConnectionFailureNotification = (message, showTryAgain = false) => {
|
|
|
|
const { showNetworkDropdown, setProviderArgs, setProviderType } =
|
|
|
|
this.props;
|
2022-09-29 05:26:01 +02:00
|
|
|
|
|
|
|
return (
|
2023-05-05 16:02:28 +02:00
|
|
|
<Popover
|
|
|
|
onClose={() => {
|
|
|
|
window.clearTimeout(this.cancelCallTimeout);
|
2023-05-24 14:10:12 +02:00
|
|
|
this.setState({ showErrorScreen: false });
|
2023-05-05 16:02:28 +02:00
|
|
|
}}
|
|
|
|
centerTitle
|
|
|
|
title={
|
|
|
|
<Icon
|
|
|
|
name={IconName.Danger}
|
|
|
|
size={IconSize.Xl}
|
|
|
|
color={IconColor.warningDefault}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Text
|
|
|
|
variant={TextVariant.bodyLgMedium}
|
|
|
|
textAlign={TextAlign.Center}
|
|
|
|
margin={[0, 4, 4, 4]}
|
|
|
|
>
|
|
|
|
{message}
|
|
|
|
</Text>
|
|
|
|
<Box display={DISPLAY.FLEX} padding={4} gap={2}>
|
|
|
|
<ButtonSecondary
|
2022-09-29 05:26:01 +02:00
|
|
|
onClick={() => {
|
|
|
|
window.clearTimeout(this.cancelCallTimeout);
|
2023-05-24 14:10:12 +02:00
|
|
|
this.setState({ showErrorScreen: false });
|
2022-09-29 05:26:01 +02:00
|
|
|
showNetworkDropdown();
|
|
|
|
}}
|
2023-05-05 16:02:28 +02:00
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
block
|
2022-09-29 05:26:01 +02:00
|
|
|
>
|
|
|
|
{this.context.t('switchNetworks')}
|
2023-05-05 16:02:28 +02:00
|
|
|
</ButtonSecondary>
|
|
|
|
{showTryAgain ? (
|
|
|
|
<ButtonPrimary
|
|
|
|
onClick={() => {
|
|
|
|
this.setState({ showErrorScreen: false });
|
|
|
|
setProviderType(...setProviderArgs);
|
|
|
|
window.clearTimeout(this.cancelCallTimeout);
|
|
|
|
this.cancelCallTimeout = setTimeout(
|
|
|
|
this.cancelCall,
|
|
|
|
this.props.cancelTime || SECOND * 15,
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
block
|
|
|
|
>
|
|
|
|
{this.context.t('tryAgain')}
|
|
|
|
</ButtonPrimary>
|
|
|
|
) : null}
|
|
|
|
</Box>
|
|
|
|
</Popover>
|
2022-09-29 05:26:01 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-05-05 16:02:28 +02:00
|
|
|
renderDeprecatedRpcUrlWarning = () => {
|
|
|
|
return this.renderConnectionFailureNotification(
|
|
|
|
this.context.t('currentRpcUrlDeprecated'),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
};
|
2018-12-06 20:39:47 +01:00
|
|
|
|
2023-05-05 16:02:28 +02:00
|
|
|
renderErrorScreenContent = () => {
|
|
|
|
const { providerConfig } = this.props;
|
|
|
|
return this.renderConnectionFailureNotification(
|
|
|
|
this.context.t('networkSwitchConnectionError', [providerConfig.nickname]),
|
|
|
|
true,
|
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) => {
|
2023-05-02 17:53:20 +02:00
|
|
|
const { providerConfig } = this.props;
|
|
|
|
const { providerConfig: prevProvider } = prevProps;
|
|
|
|
if (providerConfig.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
|
|
|
}
|
|
|
|
}
|