From 62777a81b4e51693c4ac8f492417c23d7eef4248 Mon Sep 17 00:00:00 2001 From: Erik Marks <25517051+rekmarks@users.noreply.github.com> Date: Mon, 20 Apr 2020 10:21:57 -0700 Subject: [PATCH] Close notification UI if no unapproved confirmations (#8358) * close notification UI if no pending confirmations * change benchmark page to 'home' --- test/e2e/benchmark.js | 2 +- ui/app/pages/home/home.component.js | 17 ++++++++++++- ui/app/pages/home/home.container.js | 24 +++++++++++++++---- ui/app/selectors/selectors.js | 37 ++++++++++++++++++++--------- 4 files changed, 63 insertions(+), 17 deletions(-) diff --git a/test/e2e/benchmark.js b/test/e2e/benchmark.js index 6917005c9..86f8c9f9d 100644 --- a/test/e2e/benchmark.js +++ b/test/e2e/benchmark.js @@ -110,7 +110,7 @@ async function getFirstParentDirectoryThatExists (directory) { async function main () { const args = process.argv.slice(2) - let pages = ['notification'] + let pages = ['home'] let numSamples = DEFAULT_NUM_SAMPLES let outputPath let outputDirectory diff --git a/ui/app/pages/home/home.component.js b/ui/app/pages/home/home.component.js index 475c15adc..d986a4bf4 100644 --- a/ui/app/pages/home/home.component.js +++ b/ui/app/pages/home/home.component.js @@ -39,6 +39,7 @@ export default class Home extends PureComponent { unconfirmedTransactionsCount: PropTypes.number, shouldShowSeedPhraseReminder: PropTypes.bool, isPopup: PropTypes.bool, + isNotification: PropTypes.bool.isRequired, threeBoxSynced: PropTypes.bool, setupThreeBox: PropTypes.func, turnThreeBoxSyncingOn: PropTypes.func, @@ -49,6 +50,7 @@ export default class Home extends PureComponent { threeBoxLastUpdated: PropTypes.number, hasDaiV1Token: PropTypes.bool, firstPermissionsRequestId: PropTypes.string, + totalUnapprovedCount: PropTypes.number.isRequired, } UNSAFE_componentWillMount () { @@ -70,9 +72,15 @@ export default class Home extends PureComponent { componentDidMount () { const { history, + isNotification, suggestedTokens = {}, + totalUnapprovedCount, } = this.props + if (isNotification && totalUnapprovedCount === 0) { + global.platform.closeCurrentWindow() + } + // suggested new tokens if (Object.keys(suggestedTokens).length > 0) { history.push(CONFIRM_ADD_SUGGESTED_TOKEN_ROUTE) @@ -81,11 +89,18 @@ export default class Home extends PureComponent { componentDidUpdate () { const { - threeBoxSynced, + isNotification, setupThreeBox, showRestorePrompt, threeBoxLastUpdated, + threeBoxSynced, + totalUnapprovedCount, } = this.props + + if (isNotification && totalUnapprovedCount === 0) { + global.platform.closeCurrentWindow() + } + if (threeBoxSynced && showRestorePrompt && threeBoxLastUpdated === null) { setupThreeBox() } diff --git a/ui/app/pages/home/home.container.js b/ui/app/pages/home/home.container.js index 77fe4b612..a1de53118 100644 --- a/ui/app/pages/home/home.container.js +++ b/ui/app/pages/home/home.container.js @@ -2,8 +2,15 @@ import Home from './home.component' import { compose } from 'redux' import { connect } from 'react-redux' import { withRouter } from 'react-router-dom' -import { unconfirmedTransactionsCountSelector } from '../../selectors/confirm-transaction' -import { getCurrentEthBalance, getDaiV1Token, getFirstPermissionRequest } from '../../selectors/selectors' +import { + unconfirmedTransactionsCountSelector, +} from '../../selectors/confirm-transaction' +import { + getCurrentEthBalance, + getDaiV1Token, + getFirstPermissionRequest, + getTotalUnapprovedCount, +} from '../../selectors/selectors' import { restoreFromThreeBox, turnThreeBoxSyncingOn, @@ -12,7 +19,10 @@ import { } from '../../store/actions' import { setThreeBoxLastUpdated } from '../../ducks/app/app' import { getEnvironmentType } from '../../../../app/scripts/lib/util' -import { ENVIRONMENT_TYPE_POPUP } from '../../../../app/scripts/lib/enums' +import { + ENVIRONMENT_TYPE_NOTIFICATION, + ENVIRONMENT_TYPE_POPUP, +} from '../../../../app/scripts/lib/enums' const mapStateToProps = (state) => { const { metamask, appState } = state @@ -26,8 +36,12 @@ const mapStateToProps = (state) => { } = metamask const accountBalance = getCurrentEthBalance(state) const { forgottenPassword, threeBoxLastUpdated } = appState + const totalUnapprovedCount = getTotalUnapprovedCount(state) + + const envType = getEnvironmentType() + const isPopup = envType === ENVIRONMENT_TYPE_POPUP + const isNotification = envType === ENVIRONMENT_TYPE_NOTIFICATION - const isPopup = getEnvironmentType() === ENVIRONMENT_TYPE_POPUP const firstPermissionsRequest = getFirstPermissionRequest(state) const firstPermissionsRequestId = (firstPermissionsRequest && firstPermissionsRequest.metadata) ? firstPermissionsRequest.metadata.id @@ -39,12 +53,14 @@ const mapStateToProps = (state) => { unconfirmedTransactionsCount: unconfirmedTransactionsCountSelector(state), shouldShowSeedPhraseReminder: !seedPhraseBackedUp && (parseInt(accountBalance, 16) > 0 || tokens.length > 0), isPopup, + isNotification, threeBoxSynced, showRestorePrompt, selectedAddress, threeBoxLastUpdated, hasDaiV1Token: Boolean(getDaiV1Token(state)), firstPermissionsRequestId, + totalUnapprovedCount, } } diff --git a/ui/app/selectors/selectors.js b/ui/app/selectors/selectors.js index d6f251b0c..50a8dbff8 100644 --- a/ui/app/selectors/selectors.js +++ b/ui/app/selectors/selectors.js @@ -306,18 +306,28 @@ export function getSelectedTokenContract (state) { : null } -export function getTotalUnapprovedCount ({ metamask }) { +export function getTotalUnapprovedCount (state) { const { - unapprovedTxs = {}, - unapprovedMsgCount, - unapprovedPersonalMsgCount, - unapprovedDecryptMsgCount, - unapprovedEncryptionPublicKeyMsgCount, - unapprovedTypedMessagesCount, - } = metamask + unapprovedMsgCount = 0, + unapprovedPersonalMsgCount = 0, + unapprovedDecryptMsgCount = 0, + unapprovedEncryptionPublicKeyMsgCount = 0, + unapprovedTypedMessagesCount = 0, + } = state.metamask + + return unapprovedMsgCount + unapprovedPersonalMsgCount + unapprovedDecryptMsgCount + + unapprovedEncryptionPublicKeyMsgCount + unapprovedTypedMessagesCount + + getUnapprovedTxCount(state) + getPermissionsRequestCount(state) + getSuggestedTokenCount(state) +} - return Object.keys(unapprovedTxs).length + unapprovedMsgCount + unapprovedPersonalMsgCount + - unapprovedTypedMessagesCount + unapprovedDecryptMsgCount + unapprovedEncryptionPublicKeyMsgCount +function getUnapprovedTxCount (state) { + const { unapprovedTxs = {} } = state.metamask + return Object.keys(unapprovedTxs).length +} + +function getSuggestedTokenCount (state) { + const { suggestedTokens = {} } = state.metamask + return Object.keys(suggestedTokens).length } export function getIsMainnet (state) { @@ -363,7 +373,12 @@ export function getPermissionsDescriptions (state) { } export function getPermissionsRequests (state) { - return state.metamask.permissionsRequests + return state.metamask.permissionsRequests || [] +} + +export function getPermissionsRequestCount (state) { + const permissionsRequests = getPermissionsRequests(state) + return permissionsRequests.length } export function getDomainMetadata (state) {