1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 22:24:27 +01:00
metamask-extension/ui/app/helpers/higher-order-components/metametrics/metametrics.provider.js
Mark Stacey 49a525b9f8
Add react/no-unused-prop-types ESLint rule (#7655)
* Add `react/no-unused-prop-types` rule

All detected unused prop types have been removed. I have attempted to
ensure these props are no longer passed in either.

* Update handling of props to avoid false positive lint errors

These cases were detected by `react/no-unused-prop-types` as being
unused props, even though they were used. These minor adjustments
prevent them from being flagged as errors.

* Update unit tests

Many of these tests were just checking that specific props were passed
from containers or to a child component. These were deleted, as I can't
imagine how they'd be useful.

* Disable `react/no-unused-prop-types` in `componentWillReceiveProps

The rule `react/no-unused-prop-types` doesn't seem to be detecting
props used within `UNSAFE_componentWillReceiveProps`. The two cases
have been disabled temporarily until we can replace these unsafe
lifecycle functions.
2019-12-07 23:10:47 -04:00

128 lines
3.4 KiB
JavaScript

import { Component } from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { withRouter } from 'react-router-dom'
import { compose } from 'recompose'
import {
getCurrentNetworkId,
getSelectedAsset,
getAccountType,
getNumberOfAccounts,
getNumberOfTokens,
} from '../../../selectors/selectors'
import {
txDataSelector,
} from '../../../selectors/confirm-transaction'
import { getEnvironmentType } from '../../../../../app/scripts/lib/util'
import {
sendMetaMetricsEvent,
sendCountIsTrackable,
} from '../../utils/metametrics.util'
class MetaMetricsProvider extends Component {
static propTypes = {
accountType: PropTypes.string.isRequired,
activeCurrency: PropTypes.string.isRequired,
children: PropTypes.object.isRequired,
confirmTransactionOrigin: PropTypes.string,
environmentType: PropTypes.string.isRequired,
history: PropTypes.object.isRequired,
metaMetricsId: PropTypes.string,
metaMetricsSendCount: PropTypes.number.isRequired,
network: PropTypes.string.isRequired,
numberOfTokens: PropTypes.number,
numberOfAccounts: PropTypes.number,
participateInMetaMetrics: PropTypes.bool,
}
static childContextTypes = {
metricsEvent: PropTypes.func,
}
constructor (props) {
super(props)
this.state = {
previousPath: '',
currentPath: window.location.href,
}
props.history.listen(() => {
this.setState({
previousPath: this.state.currentPath,
currentPath: window.location.href,
})
})
}
getChildContext () {
const {
network,
environmentType,
activeCurrency,
accountType,
confirmTransactionOrigin,
metaMetricsId,
participateInMetaMetrics,
metaMetricsSendCount,
numberOfTokens,
numberOfAccounts,
} = this.props
const { previousPath, currentPath } = this.state
return {
metricsEvent: (config = {}, overrides = {}) => {
const { eventOpts = {} } = config
const { name = '' } = eventOpts
const { pathname: overRidePathName = '' } = overrides
const isSendFlow = Boolean(name.match(/^send|^confirm/) || overRidePathName.match(/send|confirm/))
if (participateInMetaMetrics || config.isOptIn) {
return sendMetaMetricsEvent({
network,
environmentType,
activeCurrency,
accountType,
confirmTransactionOrigin,
metaMetricsId,
numberOfTokens,
numberOfAccounts,
...config,
previousPath,
currentPath,
excludeMetaMetricsId: isSendFlow && !sendCountIsTrackable(metaMetricsSendCount + 1),
...overrides,
})
}
},
}
}
render () {
return this.props.children
}
}
const mapStateToProps = state => {
const txData = txDataSelector(state) || {}
return {
network: getCurrentNetworkId(state),
environmentType: getEnvironmentType(),
activeCurrency: getSelectedAsset(state),
accountType: getAccountType(state),
confirmTransactionOrigin: txData.origin,
metaMetricsId: state.metamask.metaMetricsId,
participateInMetaMetrics: state.metamask.participateInMetaMetrics,
metaMetricsSendCount: state.metamask.metaMetricsSendCount,
numberOfTokens: getNumberOfTokens(state),
numberOfAccounts: getNumberOfAccounts(state),
}
}
module.exports = compose(
withRouter,
connect(mapStateToProps)
)(MetaMetricsProvider)