-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(metrics): Add drag-and-drop reordering to metric panels #112671
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
Merged
nsdeschenes
merged 28 commits into
master
from
nd/EXP-827/feat-tracemetrics-add-dnd-to-metrics-page
Apr 14, 2026
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
9390b81
feat(metrics): Add useReorderMetricQueries hook
nsdeschenes 5ea1219
feat(metrics): Accept drag-and-drop props in MetricPanel and MetricTo…
nsdeschenes db019e3
feat(metrics): Wire up dnd-kit for metric panel reordering
nsdeschenes 215e505
feat(metrics): Disable drag handle when only one metric panel
nsdeschenes de37489
fix(metrics): Fix dnd accessibility, stable keys, and layout thrash
nsdeschenes f7d561f
fix(metrics): Fix perf, duplicate keys, and memoization in dnd
nsdeschenes 61e22a1
fix(metrics): Show placeholder message only on the dragged panel
nsdeschenes a7d33ee
ref(metrics): Simplify dnd placeholder with ref callback and extract …
nsdeschenes b33f0a6
fix(metrics): Revise dnd placeholder copy to follow brand voice guide…
nsdeschenes 942c94e
fix(metrics): Use occurrence-count keys for stable dnd panel identity
nsdeschenes 5ec4de6
ref(metrics): Extract useSortableMetricQueries hook into its own file
nsdeschenes d8ec7f6
Just inline props
nsdeschenes 6e06de4
Tidy up sortable hook
nsdeschenes 9783f38
Match padding sizes
nsdeschenes 542cacf
Render placeholder chart and table during dnd reorder
nsdeschenes b95fdaa
Fix dnd placeholder height to match measured content height
nsdeschenes aaadf74
Use height prop
nsdeschenes e204909
ref: Update DnD placeholder text to explain why charts are hidden
nsdeschenes d57d46b
feat(metrics): Render tab list and placeholders in DnD reorder state
nsdeschenes 0f102ec
perf(metrics): Use React Activity to preserve table state during DnD
nsdeschenes d5eb0ae
fix(metrics): Fix iterator compatibility and simplify DnD placeholder
nsdeschenes 60a1e6c
ref(metrics): Remove unused export from MetricInfoTabList
nsdeschenes c1ecb53
fix(metrics): Restore tab list and actions render order
nsdeschenes dd30c4f
Merge remote-tracking branch 'origin/master' into nd/EXP-827/feat-tra…
nsdeschenes 44810e4
fix(metrics): Keep labels stable during panel reorder
nsdeschenes 8f6defb
ref(metrics): Use labels for sortable query IDs
nsdeschenes 67443d8
fix(metrics): Scope sortable metric reordering
nsdeschenes 4cd7899
feat(metrics): Separate aggregate and equation panels
nsdeschenes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
214 changes: 214 additions & 0 deletions
214
static/app/views/explore/metrics/hooks/useSortableMetricQueries.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| import type {DragEndEvent} from '@dnd-kit/core'; | ||
|
|
||
| import {act, renderHookWithProviders} from 'sentry-test/reactTestingLibrary'; | ||
|
|
||
| import {EQUATION_PREFIX} from 'sentry/utils/discover/fields'; | ||
| import {useSortableMetricQueries} from 'sentry/views/explore/metrics/hooks/useSortableMetricQueries'; | ||
| import {MultiMetricsQueryParamsProvider} from 'sentry/views/explore/metrics/multiMetricsQueryParams'; | ||
| import {useMultiMetricsQueryParams} from 'sentry/views/explore/metrics/multiMetricsQueryParams'; | ||
| import { | ||
| isVisualizeEquation, | ||
| VisualizeEquation, | ||
| VisualizeFunction, | ||
| } from 'sentry/views/explore/queryParams/visualize'; | ||
|
|
||
| function Wrapper({children}: {children: React.ReactNode}) { | ||
| return <MultiMetricsQueryParamsProvider>{children}</MultiMetricsQueryParamsProvider>; | ||
| } | ||
|
|
||
| describe('useSortableMetricQueries', () => { | ||
| it('uses stable labels as sortable ids', () => { | ||
| const {result} = renderHookWithProviders(useSortableMetricQueries, { | ||
| additionalWrapper: Wrapper, | ||
| initialRouterConfig: { | ||
| location: { | ||
| pathname: '/organizations/org-slug/explore/metrics/', | ||
| query: { | ||
| metric: [ | ||
| JSON.stringify({ | ||
| metric: {name: 'foo', type: 'counter'}, | ||
| query: '', | ||
| aggregateFields: [ | ||
| new VisualizeFunction('sum(value,foo,counter,-)').serialize(), | ||
| ], | ||
| aggregateSortBys: [], | ||
| mode: 'samples', | ||
| }), | ||
| JSON.stringify({ | ||
| metric: {name: 'bar', type: 'counter'}, | ||
| query: '', | ||
| aggregateFields: [ | ||
| new VisualizeFunction('sum(value,bar,counter,-)').serialize(), | ||
| ], | ||
| aggregateSortBys: [], | ||
| mode: 'samples', | ||
| }), | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| expect(result.current.sortableItems.map(({id}) => id)).toEqual(['A', 'B']); | ||
| }); | ||
|
|
||
| it('filters sortable items by query type and reorders within that section', () => { | ||
| const {result} = renderHookWithProviders( | ||
| () => { | ||
| const sortable = useSortableMetricQueries({ | ||
| predicate: metricQuery => | ||
| !isVisualizeEquation(metricQuery.queryParams.visualizes[0]!), | ||
| }); | ||
| const metricQueries = useMultiMetricsQueryParams(); | ||
| return {sortable, metricQueries}; | ||
| }, | ||
| { | ||
| additionalWrapper: Wrapper, | ||
| initialRouterConfig: { | ||
| location: { | ||
| pathname: '/organizations/org-slug/explore/metrics/', | ||
| query: { | ||
| metric: [ | ||
| JSON.stringify({ | ||
| metric: {name: 'foo', type: 'counter'}, | ||
| query: '', | ||
| aggregateFields: [ | ||
| new VisualizeFunction('sum(value,foo,counter,-)').serialize(), | ||
| ], | ||
| aggregateSortBys: [], | ||
| mode: 'samples', | ||
| }), | ||
| JSON.stringify({ | ||
| metric: {name: 'bar', type: 'counter'}, | ||
| query: '', | ||
| aggregateFields: [ | ||
| new VisualizeFunction('sum(value,bar,counter,-)').serialize(), | ||
| ], | ||
| aggregateSortBys: [], | ||
| mode: 'samples', | ||
| }), | ||
| JSON.stringify({ | ||
| metric: {name: '', type: ''}, | ||
| query: '', | ||
| aggregateFields: [new VisualizeEquation(EQUATION_PREFIX).serialize()], | ||
| aggregateSortBys: [], | ||
| mode: 'samples', | ||
| }), | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| expect(result.current.sortable.sortableItems.map(({id}) => id)).toEqual(['A', 'B']); | ||
| expect(result.current.metricQueries.map(metricQuery => metricQuery.label)).toEqual([ | ||
| 'A', | ||
| 'B', | ||
| 'ƒ1', | ||
| ]); | ||
|
|
||
| act(() => { | ||
| result.current.sortable.onDragEnd({ | ||
| active: {id: 'B'}, | ||
| over: {id: 'A'}, | ||
| } as DragEndEvent); | ||
| }); | ||
|
|
||
| expect(result.current.metricQueries.map(metricQuery => metricQuery.label)).toEqual([ | ||
| 'B', | ||
| 'A', | ||
| 'ƒ1', | ||
| ]); | ||
| }); | ||
|
|
||
| it('ignores drops outside the current section', () => { | ||
| const {result} = renderHookWithProviders( | ||
| () => { | ||
| const sortable = useSortableMetricQueries({ | ||
| predicate: metricQuery => | ||
| !isVisualizeEquation(metricQuery.queryParams.visualizes[0]!), | ||
| }); | ||
| const metricQueries = useMultiMetricsQueryParams(); | ||
| return {sortable, metricQueries}; | ||
| }, | ||
| { | ||
| additionalWrapper: Wrapper, | ||
| initialRouterConfig: { | ||
| location: { | ||
| pathname: '/organizations/org-slug/explore/metrics/', | ||
| query: { | ||
| metric: [ | ||
| JSON.stringify({ | ||
| metric: {name: 'foo', type: 'counter'}, | ||
| query: '', | ||
| aggregateFields: [ | ||
| new VisualizeFunction('sum(value,foo,counter,-)').serialize(), | ||
| ], | ||
| aggregateSortBys: [], | ||
| mode: 'samples', | ||
| }), | ||
| JSON.stringify({ | ||
| metric: {name: '', type: ''}, | ||
| query: '', | ||
| aggregateFields: [new VisualizeEquation(EQUATION_PREFIX).serialize()], | ||
| aggregateSortBys: [], | ||
| mode: 'samples', | ||
| }), | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| act(() => { | ||
| result.current.sortable.onDragEnd({ | ||
| active: {id: 'A'}, | ||
| over: {id: 'ƒ1'}, | ||
| } as DragEndEvent); | ||
| }); | ||
|
|
||
| expect(result.current.metricQueries.map(metricQuery => metricQuery.label)).toEqual([ | ||
| 'A', | ||
| 'ƒ1', | ||
| ]); | ||
| }); | ||
|
|
||
| it('reorders duplicate queries by label ids', () => { | ||
| const duplicateQuery = JSON.stringify({ | ||
| metric: {name: 'foo', type: 'counter'}, | ||
| query: '', | ||
| aggregateFields: [new VisualizeFunction('sum(value,foo,counter,-)').serialize()], | ||
| aggregateSortBys: [], | ||
| mode: 'samples', | ||
| }); | ||
|
|
||
| const {result} = renderHookWithProviders(useSortableMetricQueries, { | ||
| additionalWrapper: Wrapper, | ||
| initialRouterConfig: { | ||
| location: { | ||
| pathname: '/organizations/org-slug/explore/metrics/', | ||
| query: { | ||
| metric: [duplicateQuery, duplicateQuery], | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| expect( | ||
| result.current.sortableItems.map(({metricQuery}) => metricQuery.label) | ||
| ).toEqual(['A', 'B']); | ||
|
|
||
| act(() => { | ||
| result.current.onDragEnd({ | ||
| active: {id: 'A'}, | ||
| over: {id: 'B'}, | ||
| } as DragEndEvent); | ||
| }); | ||
|
|
||
| expect( | ||
| result.current.sortableItems.map(({metricQuery}) => metricQuery.label) | ||
| ).toEqual(['B', 'A']); | ||
| }); | ||
| }); |
73 changes: 73 additions & 0 deletions
73
static/app/views/explore/metrics/hooks/useSortableMetricQueries.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import {useCallback, useMemo, useState} from 'react'; | ||
| import type {DragEndEvent} from '@dnd-kit/core'; | ||
| import {KeyboardSensor, PointerSensor, useSensor, useSensors} from '@dnd-kit/core'; | ||
| import {arrayMove, sortableKeyboardCoordinates} from '@dnd-kit/sortable'; | ||
|
|
||
| import type {MetricQuery} from 'sentry/views/explore/metrics/metricQuery'; | ||
| import { | ||
| useMultiMetricsQueryParams, | ||
| useReorderMetricQueries, | ||
| } from 'sentry/views/explore/metrics/multiMetricsQueryParams'; | ||
|
|
||
| interface UseSortableMetricQueriesOptions { | ||
| predicate?: (metricQuery: MetricQuery) => boolean; | ||
| } | ||
|
|
||
| export function useSortableMetricQueries({ | ||
| predicate, | ||
| }: UseSortableMetricQueriesOptions = {}) { | ||
| const metricQueries = useMultiMetricsQueryParams(); | ||
| const reorderMetricQueries = useReorderMetricQueries(); | ||
| const [isDragging, setIsDragging] = useState(false); | ||
|
|
||
| const sortableItems = useMemo(() => { | ||
| return metricQueries.flatMap((metricQuery, index) => | ||
| predicate?.(metricQuery) === false | ||
| ? [] | ||
| : [ | ||
| { | ||
| id: metricQuery.label ?? String(index), | ||
| metricQuery, | ||
| index, | ||
| }, | ||
| ] | ||
| ); | ||
| }, [metricQueries, predicate]); | ||
|
|
||
| const sensors = useSensors( | ||
| useSensor(PointerSensor), | ||
| useSensor(KeyboardSensor, { | ||
| coordinateGetter: sortableKeyboardCoordinates, | ||
| }) | ||
| ); | ||
|
|
||
| const onDragStart = useCallback(() => { | ||
| setIsDragging(true); | ||
| }, []); | ||
|
|
||
| const onDragEnd = useCallback( | ||
| (event: DragEndEvent) => { | ||
| setIsDragging(false); | ||
| const {active, over} = event; | ||
| if (active.id !== over?.id) { | ||
| const oldIndex = sortableItems.find(({id}) => id === active.id)?.index; | ||
| const newIndex = sortableItems.find(({id}) => id === over?.id)?.index; | ||
|
|
||
| if (oldIndex === undefined || newIndex === undefined) return; | ||
|
|
||
| reorderMetricQueries( | ||
| arrayMove([...metricQueries], oldIndex, newIndex), | ||
| oldIndex, | ||
| newIndex | ||
| ); | ||
| } | ||
| }, | ||
| [sortableItems, metricQueries, reorderMetricQueries] | ||
| ); | ||
|
|
||
| const onDragCancel = useCallback(() => { | ||
| setIsDragging(false); | ||
| }, []); | ||
|
|
||
| return {sortableItems, sensors, onDragStart, onDragEnd, onDragCancel, isDragging}; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoa, keyboard navigation for DnD is so cool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah!! I was impressed trying it out, they really nailed it. What a relief for accessibility too.