mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-23 10:30:04 +01:00
df85ab6e10
A new page has been created for viewing assets. This replaces the old `selectedToken` state, which previously would augment the home page to show token-specific information. The new asset page shows the standard token overview as seen previously on the home page, plus a history filtered to show just transactions relevant to that token. The actions that were available in the old token list menu have been moved to a "Token Options" menu that mirrors the "Account Options" menu. The `selectedTokenAddress` state has been removed, as it is no longer being used for anything. `getMetaMetricState` has been renamed to `getBackgroundMetaMetricState` because its sole purpose is extracting data from the background state to send metrics from the background. It's not really a selector, but it was convenient for it to use the same selectors the UI uses to extract background data, so I left it there for now. A new Redux store has been added to track state related to browser history. The most recent "overview" page (i.e. the home page or the asset page) is currently being tracked, so that actions taken from the asset page can return the user back to the asset page when the action has finished.
132 lines
3.7 KiB
JavaScript
132 lines
3.7 KiB
JavaScript
import React, { Component, createContext, useEffect, useCallback, useState } from 'react'
|
|
import { useSelector } from 'react-redux'
|
|
import PropTypes from 'prop-types'
|
|
import { useHistory } from 'react-router-dom'
|
|
import { captureException } from '@sentry/browser'
|
|
|
|
import {
|
|
getCurrentNetworkId,
|
|
getAccountType,
|
|
getNumberOfAccounts,
|
|
getNumberOfTokens,
|
|
} from '../selectors/selectors'
|
|
import { getSendToken } from '../selectors/send'
|
|
import {
|
|
txDataSelector,
|
|
} from '../selectors/confirm-transaction'
|
|
import { getEnvironmentType } from '../../../app/scripts/lib/util'
|
|
import {
|
|
sendMetaMetricsEvent,
|
|
sendCountIsTrackable,
|
|
} from '../helpers/utils/metametrics.util'
|
|
|
|
|
|
export const MetaMetricsContext = createContext(() => {
|
|
captureException(
|
|
Error(`MetaMetrics context function was called from a react node that is not a descendant of a MetaMetrics context provider`)
|
|
)
|
|
})
|
|
|
|
export function MetaMetricsProvider ({ children }) {
|
|
const txData = useSelector(txDataSelector) || {}
|
|
const network = useSelector(getCurrentNetworkId)
|
|
const environmentType = getEnvironmentType()
|
|
const activeCurrency = useSelector(getSendToken)?.symbol
|
|
const accountType = useSelector(getAccountType)
|
|
const confirmTransactionOrigin = txData.origin
|
|
const metaMetricsId = useSelector((state) => state.metamask.metaMetricsId)
|
|
const participateInMetaMetrics = useSelector((state) => state.metamask.participateInMetaMetrics)
|
|
const metaMetricsSendCount = useSelector((state) => state.metamask.metaMetricsSendCount)
|
|
const numberOfTokens = useSelector(getNumberOfTokens)
|
|
const numberOfAccounts = useSelector(getNumberOfAccounts)
|
|
const history = useHistory()
|
|
const [state, setState] = useState(() => ({
|
|
currentPath: window.location.href,
|
|
previousPath: '',
|
|
}))
|
|
|
|
const { previousPath, currentPath } = state
|
|
|
|
useEffect(() => {
|
|
const unlisten = history.listen(() => setState((prevState) => ({
|
|
currentPath: window.location.href,
|
|
previousPath: prevState.currentPath,
|
|
})))
|
|
// remove this listener if the component is no longer mounted
|
|
return unlisten
|
|
}, [history])
|
|
|
|
const metricsEvent = useCallback((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,
|
|
version: global.platform.getVersion(),
|
|
...config,
|
|
previousPath,
|
|
currentPath,
|
|
excludeMetaMetricsId: isSendFlow && !sendCountIsTrackable(metaMetricsSendCount + 1),
|
|
...overrides,
|
|
})
|
|
}
|
|
}, [
|
|
network,
|
|
environmentType,
|
|
activeCurrency,
|
|
accountType,
|
|
confirmTransactionOrigin,
|
|
participateInMetaMetrics,
|
|
previousPath,
|
|
metaMetricsId,
|
|
numberOfTokens,
|
|
numberOfAccounts,
|
|
currentPath,
|
|
metaMetricsSendCount,
|
|
])
|
|
|
|
return (
|
|
<MetaMetricsContext.Provider value={metricsEvent}>
|
|
{children}
|
|
</MetaMetricsContext.Provider>
|
|
)
|
|
}
|
|
|
|
MetaMetricsProvider.propTypes = { children: PropTypes.node }
|
|
|
|
export class LegacyMetaMetricsProvider extends Component {
|
|
static propTypes = {
|
|
children: PropTypes.node,
|
|
}
|
|
|
|
static defaultProps = {
|
|
children: undefined,
|
|
}
|
|
|
|
static contextType = MetaMetricsContext
|
|
|
|
static childContextTypes = {
|
|
metricsEvent: PropTypes.func,
|
|
}
|
|
|
|
getChildContext () {
|
|
return {
|
|
metricsEvent: this.context,
|
|
}
|
|
}
|
|
|
|
render () {
|
|
return this.props.children
|
|
}
|
|
}
|