Skip to content

Commit

Permalink
fix(metrics): Handle project selection change (#75900)
Browse files Browse the repository at this point in the history
Reset MRI and span metric condition if the currently selected value
cannot be found anymore.
  • Loading branch information
ArthurKnaus authored Aug 12, 2024
1 parent e036c11 commit ac61e5f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion static/app/components/metrics/metricQuerySelect.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useCallback, useMemo} from 'react';
import {useCallback, useEffect, useMemo} from 'react';
import styled from '@emotion/styled';

import {Button} from 'sentry/components/button';
Expand Down Expand Up @@ -62,6 +62,13 @@ export function MetricQuerySelect({onChange, conditionId, mri}: Props) {
pageFilters.selection.projects[0] === -1 ||
pageFilters.selection.projects.length === 0;

// If the selected condition cannot be found, select the first condition
useEffect(() => {
if (!selectedCondition && spanConditions.length > 0) {
onChange(spanConditions[0].id);
}
}, [onChange, selectedCondition, spanConditions]);

const options: SelectOptionOrSection<number>[] = useMemo(() => {
let builtInOption: SelectOption<number> | null = null;
const sectionMap = new Map<number, SelectOption<number>[]>();
Expand Down
9 changes: 9 additions & 0 deletions static/app/components/metrics/mriSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
isTransactionDuration,
isTransactionMeasurement,
} from 'sentry/utils/metrics';
import {emptyMetricsQueryWidget} from 'sentry/utils/metrics/constants';
import {
hasCustomMetricsExtractionRules,
hasMetricsNewInputs,
Expand Down Expand Up @@ -170,6 +171,14 @@ export const MRISelect = memo(function MRISelect({
const metricsWithDuplicateNames = useMetricsWithDuplicateNames(metricsMeta);
const filteredMRIs = useFilteredMRIs(metricsMeta, inputValue, mriMode);

// If the mri is not in the list of metrics, set it to the default metric
const selectedMeta = metricsMeta.find(metric => metric.mri === value);
useEffect(() => {
if (!selectedMeta) {
onChange(emptyMetricsQueryWidget.mri);
}
}, [onChange, selectedMeta]);

const handleFilterOption = useCallback(
(option: ComboBoxOption<MRI>) => {
return filteredMRIs.has(option.value);
Expand Down

0 comments on commit ac61e5f

Please sign in to comment.