Skip to content

feat(dashboards): add sorting to non-issues table widgets #94884

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
merged 2 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions static/app/views/dashboards/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {PageFilters} from 'sentry/types/core';
import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
import type {Organization} from 'sentry/types/organization';
import {trackAnalytics} from 'sentry/utils/analytics';
import type {Sort} from 'sentry/utils/discover/fields';
import {DatasetSource} from 'sentry/utils/discover/types';
import withApi from 'sentry/utils/withApi';
import withPageFilters from 'sentry/utils/withPageFilters';
Expand Down Expand Up @@ -351,6 +352,23 @@ class Dashboard extends Component<Props, State> {
];
}

handleWidgetTableSort(index: number) {
const {dashboard, onUpdate} = this.props;
return function (sort: Sort) {
const widget = dashboard.widgets[index]!;
const widgetCopy = cloneDeep(widget);
if (widgetCopy.queries[0]) {
const direction = sort.kind === 'desc' ? '-' : '';
widgetCopy.queries[0].orderby = `${direction}${sort.field}`;
}

const nextList = [...dashboard.widgets];
nextList[index] = widgetCopy;

onUpdate(nextList);
};
}

renderWidget(widget: Widget, index: number) {
const {isMobile, windowWidth} = this.state;
const {
Expand Down Expand Up @@ -391,6 +409,7 @@ class Dashboard extends Component<Props, State> {
index={String(index)}
newlyAddedWidget={newlyAddedWidget}
onNewWidgetScrollComplete={onNewWidgetScrollComplete}
onWidgetTableSort={this.handleWidgetTableSort(index)}
/>
</div>
);
Expand Down
4 changes: 4 additions & 0 deletions static/app/views/dashboards/sortableWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styled from '@emotion/styled';
import {LazyRender} from 'sentry/components/lazyRender';
import PanelAlert from 'sentry/components/panels/panelAlert';
import type {User} from 'sentry/types/user';
import type {Sort} from 'sentry/utils/discover/fields';
import useOrganization from 'sentry/utils/useOrganization';
import {useUser} from 'sentry/utils/useUser';
import {useUserTeams} from 'sentry/utils/useUserTeams';
Expand Down Expand Up @@ -34,6 +35,7 @@ type Props = {
isPreview?: boolean;
newlyAddedWidget?: Widget;
onNewWidgetScrollComplete?: () => void;
onWidgetTableSort?: (sort: Sort) => void;
windowWidth?: number;
};

Expand All @@ -57,6 +59,7 @@ function SortableWidget(props: Props) {
dashboardCreator,
newlyAddedWidget,
onNewWidgetScrollComplete,
onWidgetTableSort,
} = props;

const organization = useOrganization();
Expand Down Expand Up @@ -104,6 +107,7 @@ function SortableWidget(props: Props) {
isMobile,
windowWidth,
tableItemLimit: TABLE_ITEM_LIMIT,
onWidgetTableSort,
};

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PanelAlert from 'sentry/components/panels/panelAlert';
import {dedupeArray} from 'sentry/utils/dedupeArray';
import type {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
import type {Sort} from 'sentry/utils/discover/fields';
import {useLocation} from 'sentry/utils/useLocation';
import {useNavigate} from 'sentry/utils/useNavigate';
import useOrganization from 'sentry/utils/useOrganization';
Expand All @@ -12,6 +13,7 @@ import {
WidgetType,
} from 'sentry/views/dashboards/types';
import {useWidgetBuilderContext} from 'sentry/views/dashboards/widgetBuilder/contexts/widgetBuilderContext';
import {BuilderStateAction} from 'sentry/views/dashboards/widgetBuilder/hooks/useWidgetBuilderState';
import {convertBuilderStateToWidget} from 'sentry/views/dashboards/widgetBuilder/utils/convertBuilderStateToWidget';
import WidgetCard from 'sentry/views/dashboards/widgetCard';
import WidgetLegendNameEncoderDecoder from 'sentry/views/dashboards/widgetLegendNameEncoderDecoder';
Expand Down Expand Up @@ -39,7 +41,7 @@ function WidgetPreview({
const navigate = useNavigate();
const pageFilters = usePageFilters();

const {state} = useWidgetBuilderContext();
const {state, dispatch} = useWidgetBuilderContext();

const widget = convertBuilderStateToWidget(state);

Expand Down Expand Up @@ -75,6 +77,13 @@ function WidgetPreview({
}),
};

function handleWidgetTableSort(sort: Sort) {
dispatch({
payload: [sort],
type: BuilderStateAction.SET_SORT,
});
}

return (
<WidgetCard
disableFullscreen
Expand Down Expand Up @@ -117,6 +126,7 @@ function WidgetPreview({
minTableColumnWidth={MIN_TABLE_COLUMN_WIDTH_PX}
disableZoom
showLoadingText
onWidgetTableSort={handleWidgetTableSort}
/>
);
}
Expand Down
21 changes: 18 additions & 3 deletions static/app/views/dashboards/widgetCard/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ import {
} from 'sentry/utils/discover/charts';
import type {EventsMetaType, MetaType} from 'sentry/utils/discover/eventView';
import type {RenderFunctionBaggage} from 'sentry/utils/discover/fieldRenderers';
import type {AggregationOutputType, DataUnit} from 'sentry/utils/discover/fields';
import type {AggregationOutputType, DataUnit, Sort} from 'sentry/utils/discover/fields';
import {
aggregateOutputType,
getAggregateArg,
getEquation,
getMeasurementSlug,
isAggregateField,
isEquation,
maybeEquationAlias,
stripDerivedMetricsPrefix,
stripEquationPrefix,
} from 'sentry/utils/discover/fields';
import getDynamicText from 'sentry/utils/getDynamicText';
import {decodeSorts} from 'sentry/utils/queryString';
import {getDatasetConfig} from 'sentry/views/dashboards/datasetConfig/base';
import type {Widget} from 'sentry/views/dashboards/types';
import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
Expand Down Expand Up @@ -103,6 +105,7 @@ type WidgetCardChartProps = Pick<
selected: Record<string, boolean>;
type: 'legendselectchanged';
}>;
onWidgetTableSort?: (sort: Sort) => void;
onZoom?: EChartDataZoomHandler;
sampleCount?: number;
shouldResize?: boolean;
Expand Down Expand Up @@ -144,8 +147,15 @@ class WidgetCardChart extends Component<WidgetCardChartProps> {
}

tableResultComponent({loading, tableResults}: TableResultProps): React.ReactNode {
const {widget, selection, minTableColumnWidth, location, organization, theme} =
this.props;
const {
widget,
selection,
minTableColumnWidth,
location,
organization,
theme,
onWidgetTableSort,
} = this.props;
if (loading || !tableResults?.[0]) {
// Align height to other charts.
return <LoadingPlaceholder />;
Expand Down Expand Up @@ -176,9 +186,12 @@ class WidgetCardChart extends Component<WidgetCardChartProps> {
name: column.name,
width: minTableColumnWidth ?? column.width,
type: column.type === 'never' ? null : column.type,
sortable:
widget.widgetType === WidgetType.RELEASE ? isAggregateField(column.key) : true,
}));
const aliases = decodeColumnAliases(columns, fieldAliases, fieldHeaderMap);
const tableData = convertTableDataToTabularData(tableResults?.[i]);
const sort = decodeSorts(widget.queries[0]?.orderby)?.[0];

return (
<TableWrapper key={`table:${result.title}`}>
Expand All @@ -190,6 +203,8 @@ class WidgetCardChart extends Component<WidgetCardChartProps> {
scrollable
fit="max-content"
aliases={aliases}
onChangeSort={onWidgetTableSort}
sort={sort}
getRenderer={(field, _dataRow, meta) => {
const customRenderer = datasetConfig.getCustomFieldRenderer?.(
field,
Expand Down
5 changes: 4 additions & 1 deletion static/app/views/dashboards/widgetCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {Series} from 'sentry/types/echarts';
import type {WithRouterProps} from 'sentry/types/legacyReactRouter';
import type {Confidence, Organization} from 'sentry/types/organization';
import type {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
import type {AggregationOutputType} from 'sentry/utils/discover/fields';
import type {AggregationOutputType, Sort} from 'sentry/utils/discover/fields';
import {statsPeriodToDays} from 'sentry/utils/duration/statsPeriodToDays';
import {hasOnDemandMetricWidgetFeature} from 'sentry/utils/onDemandMetrics/features';
import {useExtractionStatus} from 'sentry/utils/performance/contexts/metricsEnhancedPerformanceDataContext';
Expand Down Expand Up @@ -89,6 +89,7 @@ type Props = WithRouterProps & {
onSetTransactionsDataset?: () => void;
onUpdate?: (widget: Widget | null) => void;
onWidgetSplitDecision?: (splitDecision: WidgetType) => void;
onWidgetTableSort?: (sort: Sort) => void;
renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
shouldResize?: boolean;
showConfidenceWarning?: boolean;
Expand Down Expand Up @@ -156,6 +157,7 @@ function WidgetCard(props: Props) {
disableZoom,
showLoadingText,
router,
onWidgetTableSort,
} = props;

if (widget.displayType === DisplayType.TOP_N) {
Expand Down Expand Up @@ -322,6 +324,7 @@ function WidgetCard(props: Props) {
disableZoom={disableZoom}
onDataFetchStart={onDataFetchStart}
showLoadingText={showLoadingText && isLoadingTextVisible}
onWidgetTableSort={onWidgetTableSort}
/>
</WidgetFrame>
</VisuallyCompleteWithData>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
} from 'sentry/types/echarts';
import type {Organization} from 'sentry/types/organization';
import type {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
import type {AggregationOutputType} from 'sentry/utils/discover/fields';
import type {AggregationOutputType, Sort} from 'sentry/utils/discover/fields';
import {useLocation} from 'sentry/utils/useLocation';
import type {DashboardFilters, Widget} from 'sentry/views/dashboards/types';
import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
Expand Down Expand Up @@ -57,6 +57,7 @@ type Props = {
type: 'legendselectchanged';
}>;
onWidgetSplitDecision?: (splitDecision: WidgetType) => void;
onWidgetTableSort?: (sort: Sort) => void;
onZoom?: EChartDataZoomHandler;
renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
shouldResize?: boolean;
Expand Down Expand Up @@ -90,6 +91,7 @@ export function WidgetCardChartContainer({
onDataFetchStart,
disableZoom,
showLoadingText,
onWidgetTableSort,
}: Props) {
const location = useLocation();
const theme = useTheme();
Expand Down Expand Up @@ -215,6 +217,7 @@ export function WidgetCardChartContainer({
isSampled={isSampled}
showLoadingText={showLoadingText}
theme={theme}
onWidgetTableSort={onWidgetTableSort}
/>
</Fragment>
);
Expand Down
Loading