Skip to content

Commit 6a22b21

Browse files
feat(dashboards): enable sorting by column in table view (#82239)
Enable sorting by column in dashboards table view. - This PR adds sorting by `name`, `date created`, and `owner` - Added sort by `Name (Z-A)` to dropdown to maintain consistency b/w grid and table view - Sorting by `owner` column makes your dashboards show up on top (i.e. `myDashboards` in the sort dropdown) - The plan is to remove the sort dropdown for the table view in the future <img width="800" alt="Screenshot 2024-12-17 at 1 03 30 PM" src="https://github.com/user-attachments/assets/6393a5ec-2b3c-43e8-8dca-2731060c6add" />
1 parent 0203fa4 commit 6a22b21

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Diff for: static/app/views/dashboards/manage/dashboardTable.tsx

+44
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import GridEditable, {
2121
COL_WIDTH_UNDEFINED,
2222
type GridColumnOrder,
2323
} from 'sentry/components/gridEditable';
24+
import SortLink from 'sentry/components/gridEditable/sortLink';
2425
import Link from 'sentry/components/links/link';
2526
import TimeSince from 'sentry/components/timeSince';
2627
import {IconCopy, IconDelete, IconStar} from 'sentry/icons';
2728
import {t} from 'sentry/locale';
2829
import {space} from 'sentry/styles/space';
2930
import type {Organization} from 'sentry/types/organization';
3031
import {trackAnalytics} from 'sentry/utils/analytics';
32+
import {decodeScalar} from 'sentry/utils/queryString';
3133
import withApi from 'sentry/utils/withApi';
3234
import EditAccessSelector from 'sentry/views/dashboards/editAccessSelector';
3335
import type {
@@ -56,6 +58,12 @@ enum ResponseKeys {
5658
FAVORITE = 'isFavorited',
5759
}
5860

61+
const SortKeys = {
62+
title: {asc: 'title', desc: '-title'},
63+
dateCreated: {asc: 'dateCreated', desc: '-dateCreated'},
64+
createdBy: {asc: 'mydashboards', desc: 'mydashboards'},
65+
};
66+
5967
type FavoriteButtonProps = {
6068
api: Client;
6169
dashboardId: string;
@@ -159,6 +167,41 @@ function DashboardTable({
159167
: {}),
160168
};
161169

170+
function renderHeadCell(column: GridColumnOrder<string>) {
171+
if (column.key in SortKeys) {
172+
const urlSort = decodeScalar(location.query.sort, 'mydashboards');
173+
const isCurrentSort =
174+
urlSort === SortKeys[column.key].asc || urlSort === SortKeys[column.key].desc;
175+
const sortDirection =
176+
!isCurrentSort || column.key === 'createdBy'
177+
? undefined
178+
: urlSort.startsWith('-')
179+
? 'desc'
180+
: 'asc';
181+
182+
return (
183+
<SortLink
184+
align={'left'}
185+
title={column.name}
186+
direction={sortDirection}
187+
canSort
188+
generateSortLink={() => {
189+
const newSort = isCurrentSort
190+
? sortDirection === 'asc'
191+
? SortKeys[column.key].desc
192+
: SortKeys[column.key].asc
193+
: SortKeys[column.key].asc;
194+
return {
195+
...location,
196+
query: {...location.query, sort: newSort},
197+
};
198+
}}
199+
/>
200+
);
201+
}
202+
return column.name;
203+
}
204+
162205
const renderBodyCell = (
163206
column: GridColumnOrder<string>,
164207
dataRow: DashboardListItem
@@ -289,6 +332,7 @@ function DashboardTable({
289332
columnSortBy={[]}
290333
grid={{
291334
renderBodyCell,
335+
renderHeadCell: column => renderHeadCell(column),
292336
// favorite column
293337
renderPrependColumns: (isHeader: boolean, dataRow?: any) => {
294338
if (!organization.features.includes('dashboards-favourite')) {

Diff for: static/app/views/dashboards/manage/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import TemplateCard from './templateCard';
6060
const SORT_OPTIONS: SelectValue<string>[] = [
6161
{label: t('My Dashboards'), value: 'mydashboards'},
6262
{label: t('Dashboard Name (A-Z)'), value: 'title'},
63+
{label: t('Dashboard Name (Z-A)'), value: '-title'},
6364
{label: t('Date Created (Newest)'), value: '-dateCreated'},
6465
{label: t('Date Created (Oldest)'), value: 'dateCreated'},
6566
{label: t('Most Popular'), value: 'mostPopular'},

0 commit comments

Comments
 (0)