|
@@ -1,19 +1,21 @@
|
|
|
-import {Dispatch, ReactNode, useReducer} from 'react';
|
|
|
+import {Dispatch, ReactNode, useCallback, useReducer} from 'react';
|
|
|
+import {browserHistory} from 'react-router';
|
|
|
+import {Location} from 'history';
|
|
|
|
|
|
import localStorage from 'sentry/utils/localStorage';
|
|
|
+import {decodeScalar} from 'sentry/utils/queryString';
|
|
|
import useOrganization from 'sentry/utils/useOrganization';
|
|
|
|
|
|
import {createDefinedContext} from './utils';
|
|
|
|
|
|
export interface MetricsEnhancedSettingContext {
|
|
|
autoSampleState: AutoSampleState;
|
|
|
- hideSinceMetricsOnly: boolean;
|
|
|
memoizationKey: string;
|
|
|
metricSettingState: MEPState | null;
|
|
|
setAutoSampleState: Dispatch<AutoSampleState>;
|
|
|
setMetricSettingState: Dispatch<MEPState>;
|
|
|
+ shouldQueryProvideMEPAutoParams: boolean;
|
|
|
shouldQueryProvideMEPMetricParams: boolean;
|
|
|
- shouldQueryProvideMEPParams: boolean;
|
|
|
shouldQueryProvideMEPTransactionParams: boolean;
|
|
|
}
|
|
|
|
|
@@ -43,6 +45,8 @@ export enum MEPState {
|
|
|
transactionsOnly = 'transactionsOnly',
|
|
|
}
|
|
|
|
|
|
+const METRIC_SETTING_PARAM = 'metricSetting';
|
|
|
+
|
|
|
const storageKey = 'performance.metrics-enhanced-setting';
|
|
|
export class MEPSetting {
|
|
|
static get(): MEPState | null {
|
|
@@ -64,43 +68,66 @@ export class MEPSetting {
|
|
|
|
|
|
export const MEPSettingProvider = ({
|
|
|
children,
|
|
|
+ location,
|
|
|
_hasMEPState,
|
|
|
- forceMetricsOnly,
|
|
|
}: {
|
|
|
children: ReactNode;
|
|
|
_hasMEPState?: MEPState;
|
|
|
- forceMetricsOnly?: boolean;
|
|
|
+ location?: Location;
|
|
|
}) => {
|
|
|
const organization = useOrganization();
|
|
|
+
|
|
|
const canUseMEP =
|
|
|
- organization.features.includes('performance-use-metrics') || !!forceMetricsOnly;
|
|
|
+ organization.features.includes('performance-use-metrics') ||
|
|
|
+ organization.features.includes('performance-transaction-name-only-search');
|
|
|
+ const shouldDefaultToMetrics = organization.features.includes(
|
|
|
+ 'performance-transaction-name-only-search'
|
|
|
+ );
|
|
|
+
|
|
|
+ const allowedStates = [MEPState.auto, MEPState.metricsOnly, MEPState.transactionsOnly];
|
|
|
+ const _metricSettingFromParam = location
|
|
|
+ ? decodeScalar(location.query[METRIC_SETTING_PARAM])
|
|
|
+ : MEPState.auto;
|
|
|
+ const defaultMetricsState = shouldDefaultToMetrics
|
|
|
+ ? MEPState.metricsOnly
|
|
|
+ : MEPState.auto;
|
|
|
|
|
|
- const isControlledMEP = typeof _hasMEPState !== 'undefined' || !!forceMetricsOnly;
|
|
|
+ const metricSettingFromParam =
|
|
|
+ allowedStates.find(s => s === _metricSettingFromParam) ?? defaultMetricsState;
|
|
|
|
|
|
- const [_metricSettingState, setMetricSettingState] = useReducer(
|
|
|
+ const isControlledMEP = typeof _hasMEPState !== 'undefined';
|
|
|
+
|
|
|
+ const [_metricSettingState, _setMetricSettingState] = useReducer(
|
|
|
(_: MEPState, next: MEPState) => next,
|
|
|
- MEPState.auto
|
|
|
+ metricSettingFromParam
|
|
|
);
|
|
|
+
|
|
|
+ const setMetricSettingState = useCallback(
|
|
|
+ (settingState: MEPState) => {
|
|
|
+ if (!location) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ browserHistory.replace({
|
|
|
+ ...location,
|
|
|
+ query: {
|
|
|
+ ...location.query,
|
|
|
+ [METRIC_SETTING_PARAM]: settingState,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ _setMetricSettingState(settingState);
|
|
|
+ },
|
|
|
+ [location, _setMetricSettingState]
|
|
|
+ );
|
|
|
+
|
|
|
const [autoSampleState, setAutoSampleState] = useReducer(
|
|
|
(_: AutoSampleState, next: AutoSampleState) => next,
|
|
|
AutoSampleState.unset
|
|
|
);
|
|
|
|
|
|
- let metricSettingState = _metricSettingState;
|
|
|
-
|
|
|
- if (isControlledMEP) {
|
|
|
- if (forceMetricsOnly) {
|
|
|
- metricSettingState = MEPState.metricsOnly;
|
|
|
- }
|
|
|
- if (_hasMEPState) {
|
|
|
- metricSettingState = _hasMEPState;
|
|
|
- }
|
|
|
- }
|
|
|
+ const metricSettingState = isControlledMEP ? _hasMEPState : _metricSettingState;
|
|
|
|
|
|
- const hideSinceMetricsOnly =
|
|
|
- canUseMEP &&
|
|
|
- (metricSettingState === MEPState.metricsOnly || metricSettingState === MEPState.auto); // TODO(k-fish): Change this so auto includes data state.
|
|
|
- const shouldQueryProvideMEPParams = canUseMEP && metricSettingState === MEPState.auto;
|
|
|
+ const shouldQueryProvideMEPAutoParams =
|
|
|
+ canUseMEP && metricSettingState === MEPState.auto;
|
|
|
const shouldQueryProvideMEPMetricParams =
|
|
|
canUseMEP && metricSettingState === MEPState.metricsOnly;
|
|
|
const shouldQueryProvideMEPTransactionParams =
|
|
@@ -113,8 +140,7 @@ export const MEPSettingProvider = ({
|
|
|
value={{
|
|
|
autoSampleState,
|
|
|
metricSettingState,
|
|
|
- hideSinceMetricsOnly,
|
|
|
- shouldQueryProvideMEPParams,
|
|
|
+ shouldQueryProvideMEPAutoParams,
|
|
|
shouldQueryProvideMEPMetricParams,
|
|
|
shouldQueryProvideMEPTransactionParams,
|
|
|
memoizationKey,
|