diff --git a/src/actions/DiscoveryActions.js b/src/actions/DiscoveryActions.js index 06ee9e3e..1c749ab0 100644 --- a/src/actions/DiscoveryActions.js +++ b/src/actions/DiscoveryActions.js @@ -2,7 +2,6 @@ import React from 'react'; import { FormattedMessage } from 'react-intl'; import TrezorConnect, { UI } from 'trezor-connect'; -import * as BLOCKCHAIN_ACTION from 'actions/constants/blockchain'; import * as DISCOVERY from 'actions/constants/discovery'; import * as ACCOUNT from 'actions/constants/account'; import * as NOTIFICATION from 'actions/constants/notification'; @@ -10,7 +9,6 @@ import * as NOTIFICATION from 'actions/constants/notification'; import type { ThunkAction, AsyncAction, - PromiseAction, PayloadAction, GetState, Dispatch, @@ -331,30 +329,7 @@ const finish = (device: TrezorDevice, discoveryProcess: Discovery): AsyncAction }); }; -export const reconnect = (network: string, timeout: number = 30): PromiseAction => async ( - dispatch: Dispatch -): Promise => { - // Runs two promises. - // First promise is a subscribe action which will never resolve in case of completely lost connection to the backend - // That's why there is a second promise that rejects after the specified timeout. - return Promise.race([ - dispatch(BlockchainActions.subscribe(network)), - new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout)), - ]) - .catch(() => { - // catch error from first promises that rejects (most likely timeout) - dispatch({ - type: BLOCKCHAIN_ACTION.FAIL_SUBSCRIBE, - shortcut: network, - }); - }) - .then(() => { - // dispatch restore when subscribe promise resolves - dispatch(restore()); - }); -}; - -// Called after DEVICE.CONNECT ('trezor-connect') or CONNECT.AUTH_DEVICE actions in WalletService +// Called after DEVICE.CONNECT ('trezor-connect') or CONNECT.AUTH_DEVICE or BLOCKCHAIN.CONNECT actions in WalletService // OR after BlockchainSubscribe in this.reconnect export const restore = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => { // check if current url has "network" parameter diff --git a/src/components/notifications/Context/components/Account/index.js b/src/components/notifications/Context/components/Account/index.js index 48ce722a..331e2b82 100644 --- a/src/components/notifications/Context/components/Account/index.js +++ b/src/components/notifications/Context/components/Account/index.js @@ -14,17 +14,21 @@ export default (props: Props) => { if (notification.type === 'backend') { // special case: backend is down // TODO: this is a different component with "auto resolve" button + const inProgress = blockchain && blockchain.connecting; + const status = inProgress + ? l10nMessages.TR_RECONNECTING + : l10nMessages.TR_CONNECT_TO_BACKEND; return ( { - await props.blockchainReconnect(network.shortcut); + if (!inProgress) props.blockchainReconnect(network.shortcut); }, }, ]} diff --git a/src/components/notifications/Context/components/Account/index.messages.js b/src/components/notifications/Context/components/Account/index.messages.js index 436faa00..e39b19f9 100644 --- a/src/components/notifications/Context/components/Account/index.messages.js +++ b/src/components/notifications/Context/components/Account/index.messages.js @@ -7,6 +7,10 @@ const definedMessages: Messages = defineMessages({ id: 'TR_CONNECT_TO_BACKEND', defaultMessage: 'Connect', }, + TR_RECONNECTING: { + id: 'TR_RECONNECTING', + defaultMessage: 'Reconnecting...', + }, }); export default definedMessages; diff --git a/src/components/notifications/Context/index.js b/src/components/notifications/Context/index.js index ff2de3a3..1480ac77 100644 --- a/src/components/notifications/Context/index.js +++ b/src/components/notifications/Context/index.js @@ -7,7 +7,7 @@ import type { IntlShape } from 'react-intl'; import type { State, Dispatch } from 'flowtype'; -import { reconnect } from 'actions/DiscoveryActions'; +import { subscribe } from 'actions/BlockchainActions'; import * as NotificationActions from 'actions/NotificationActions'; import StaticNotifications from './components/Static'; @@ -29,7 +29,7 @@ export type StateProps = {| export type DispatchProps = {| close: typeof NotificationActions.close, - blockchainReconnect: typeof reconnect, + blockchainReconnect: typeof subscribe, |}; export type Props = {| ...OwnProps, ...StateProps, ...DispatchProps |}; @@ -52,7 +52,7 @@ const mapStateToProps = (state: State): StateProps => ({ const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({ close: bindActionCreators(NotificationActions.close, dispatch), - blockchainReconnect: bindActionCreators(reconnect, dispatch), + blockchainReconnect: bindActionCreators(subscribe, dispatch), }); export default injectIntl(