Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle one specific permissions request per tab #7620

Merged
merged 1 commit into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/scripts/controllers/permissions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class PermissionsController {
requestUserApproval: async (req) => {
const { metadata: { id } } = req

this._platform.openExtensionInBrowser('connect')
this._platform.openExtensionInBrowser(`connect/${id}`)

return new Promise((resolve, reject) => {
this.pendingApprovals[id] = { resolve, reject }
Expand Down
8 changes: 4 additions & 4 deletions ui/app/pages/home/home.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ export default class Home extends PureComponent {
setShowRestorePromptToFalse: PropTypes.func,
threeBoxLastUpdated: PropTypes.number,
hasDaiV1Token: PropTypes.bool,
hasPermissionRequests: PropTypes.bool,
firstPermissionsRequestId: PropTypes.string,
}

componentWillMount () {
const {
history,
unconfirmedTransactionsCount = 0,
hasPermissionRequests,
firstPermissionsRequestId,
} = this.props

if (hasPermissionRequests) {
history.push(CONNECT_ROUTE)
if (firstPermissionsRequestId) {
history.push(`${CONNECT_ROUTE}/${firstPermissionsRequestId}`)
}

if (unconfirmedTransactionsCount > 0) {
Expand Down
8 changes: 6 additions & 2 deletions ui/app/pages/home/home.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { compose } from 'recompose'
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
import { unconfirmedTransactionsCountSelector } from '../../selectors/confirm-transaction'
import { getCurrentEthBalance, getDaiV1Token, hasPermissionRequests } from '../../selectors/selectors'
import { getCurrentEthBalance, getDaiV1Token, getFirstPermissionRequest } from '../../selectors/selectors'
import {
restoreFromThreeBox,
turnThreeBoxSyncingOn,
Expand All @@ -28,6 +28,10 @@ const mapStateToProps = state => {
const { forgottenPassword, threeBoxLastUpdated } = appState

const isPopup = getEnvironmentType(window.location.href) === ENVIRONMENT_TYPE_POPUP
const firstPermissionsRequest = getFirstPermissionRequest(state)
const firstPermissionsRequestId = (firstPermissionsRequest && firstPermissionsRequest.metadata)
? firstPermissionsRequest.metadata.id
: null

return {
forgottenPassword,
Expand All @@ -41,7 +45,7 @@ const mapStateToProps = state => {
selectedAddress,
threeBoxLastUpdated,
hasDaiV1Token: Boolean(getDaiV1Token(state)),
hasPermissionRequests: hasPermissionRequests(state),
firstPermissionsRequestId,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ export default class PermissionConnect extends Component {
}

componentDidUpdate (prevProps) {
const { domains, permissionsRequestId } = this.props
const { domains, permissionsRequest } = this.props
const { originName, page } = this.state

if (!permissionsRequestId && prevProps.permissionsRequestId && page !== null) {
if (!permissionsRequest && prevProps.permissionsRequest && page !== null) {
const permissionDataForDomain = domains && domains[originName] || {}
const permissionsForDomain = permissionDataForDomain.permissions || []
const prevPermissionDataForDomain = prevProps.domains && prevProps.domains[originName] || {}
Expand All @@ -82,12 +82,6 @@ export default class PermissionConnect extends Component {
} else {
this.redirectFlow(false)
}
} else if (permissionsRequestId && prevProps.permissionsRequestId &&
permissionsRequestId !== prevProps.permissionsRequestId && page !== null) {
this.setState({
originName: this.props.originName,
page: 1,
})
}
}

Expand Down Expand Up @@ -130,10 +124,16 @@ export default class PermissionConnect extends Component {
const {
getCurrentWindowTab,
getRequestAccountTabIds,
permissionsRequest,
history,
} = this.props
getCurrentWindowTab()
getRequestAccountTabIds()

if (!permissionsRequest) {
return history.push(DEFAULT_ROUTE)
}

if (getEnvironmentType(window.location.href) === ENVIRONMENT_TYPE_FULLSCREEN) {
window.addEventListener('beforeunload', this.beforeUnload)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { connect } from 'react-redux'
import { compose } from 'recompose'
import { withRouter } from 'react-router-dom'
import PropTypes from 'prop-types'
import PermissionApproval from './permissions-connect.component'
import {
getFirstPermissionRequest,
getPermissionsRequests,
getNativeCurrency,
getAccountsWithLabels,
getLastConnectedInfo,
Expand All @@ -12,8 +12,13 @@ import {
import { formatDate } from '../../helpers/utils/util'
import { approvePermissionsRequest, rejectPermissionsRequest, showModal, getCurrentWindowTab, getRequestAccountTabIds } from '../../store/actions'

const mapStateToProps = state => {
const permissionsRequest = getFirstPermissionRequest(state)
const mapStateToProps = (state, ownProps) => {
const { match: { params: { id: permissionsRequestId } } } = ownProps
const permissionsRequests = getPermissionsRequests(state)

const permissionsRequest = permissionsRequests
.find(permissionsRequest => permissionsRequest.metadata.id === permissionsRequestId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we can assume that permissionsRequest.metadata will always be an object

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can. This is the only place requests are added: https://github.com/MetaMask/rpc-cap/blob/master/index.ts#L817

Copy link
Member

@rekmarks rekmarks Dec 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that rpc-cap metadata has not been a problem historically. Site/domain metadata in the permissions middleware has caused trouble by not existing (ごめんなさい 🙇).


const { metadata = {} } = permissionsRequest || {}
const { origin } = metadata
const nativeCurrency = getNativeCurrency(state)
Expand All @@ -29,8 +34,6 @@ const mapStateToProps = state => {
addressLastConnectedMap[key] = formatDate(addressLastConnectedMap[key], 'yyyy-M-d')
})

const permissionsRequestId = (permissionsRequest && permissionsRequest.metadata) ? permissionsRequest.metadata.id : null

return {
permissionsRequest,
permissionsRequestId,
Expand Down Expand Up @@ -60,7 +63,17 @@ const mapDispatchToProps = dispatch => {
}
}

export default compose(
withRouter,
const PermissionApprovalContainer = compose(
connect(mapStateToProps, mapDispatchToProps)
)(PermissionApproval)

PermissionApprovalContainer.propTypes = {
history: PropTypes.object.isRequired,
match: PropTypes.shape({
params: PropTypes.shape({
id: PropTypes.string,
}).isRequired,
}).isRequired,
}

export default PermissionApprovalContainer
2 changes: 1 addition & 1 deletion ui/app/pages/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Routes extends Component {
<Authenticated path={CONFIRM_ADD_TOKEN_ROUTE} component={ConfirmAddTokenPage} exact />
<Authenticated path={CONFIRM_ADD_SUGGESTED_TOKEN_ROUTE} component={ConfirmAddSuggestedTokenPage} exact />
<Authenticated path={NEW_ACCOUNT_ROUTE} component={CreateAccountPage} />
<Authenticated path={CONNECT_ROUTE} component={PermissionsConnect} exact />
<Authenticated path={`${CONNECT_ROUTE}/:id`} component={PermissionsConnect} exact />
<Authenticated path={CONNECTED_ROUTE} component={ConnectedSites} exact />
<Authenticated path={DEFAULT_ROUTE} component={Home} exact />
</Switch>
Expand Down