Skip to content

Commit 2452a84

Browse files
authored
refactor: Decouple IrisGrid from Dh.Sort, use SortDescriptor instead (#2583)
- Decouple IrisGrid from Dh.Sort, delegate sort hydration to model to allow plugins use own sort API - Allow negative column indexes in IrisGrid, so plugins could sort and filter on non-columns, such as ColumnBy sources
1 parent 7b1c878 commit 2452a84

File tree

13 files changed

+295
-111
lines changed

13 files changed

+295
-111
lines changed

packages/dashboard-core-plugins/src/panels/IrisGridPanel.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
type RowDataMap,
5454
type AdvancedFilterOptions,
5555
type FormattingRule,
56+
type SortDescriptor,
5657
} from '@deephaven/jsapi-utils';
5758
import Log from '@deephaven/log';
5859
import {
@@ -199,7 +200,7 @@ interface IrisGridPanelState {
199200
columnAlignmentMap: Map<string, CanvasTextAlign>;
200201
isFilterBarShown: boolean;
201202
quickFilters: ReadonlyQuickFilterMap;
202-
sorts: readonly dh.Sort[];
203+
sorts: readonly SortDescriptor[];
203204
userColumnWidths: ModelSizeMap;
204205
userRowHeights: ModelSizeMap;
205206
reverse: boolean;

packages/iris-grid/src/CommonTypes.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { type AdvancedFilterOptions } from '@deephaven/jsapi-utils';
1+
import {
2+
type AdvancedFilterOptions,
3+
type SortDescriptor,
4+
} from '@deephaven/jsapi-utils';
25
import { type GridRangeIndex, type ModelIndex } from '@deephaven/grid';
36
import type { dh } from '@deephaven/jsapi-types';
47
import { type Shortcut } from '@deephaven/components';
@@ -97,7 +100,7 @@ export interface IrisGridStateOverride extends Record<string, unknown> {
97100
loadingScrimProgress: number | null;
98101
advancedFilters: ReadonlyAdvancedFilterMap;
99102
quickFilters: ReadonlyQuickFilterMap;
100-
sorts: readonly dh.Sort[];
103+
sorts: readonly SortDescriptor[];
101104
reverse: boolean;
102105
rollupConfig: UIRollupConfig | undefined;
103106
}

packages/iris-grid/src/IrisGrid.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import {
7777
type TableColumnFormat,
7878
type Settings,
7979
isSortDirection,
80+
type SortDescriptor,
8081
} from '@deephaven/jsapi-utils';
8182
import {
8283
assertNotNull,
@@ -237,7 +238,7 @@ function isEmptyConfig({
237238
rollupConfig?: UIRollupConfig;
238239
searchFilter?: DhType.FilterCondition;
239240
selectDistinctColumns: readonly ColumnName[];
240-
sorts: readonly DhType.Sort[];
241+
sorts: readonly SortDescriptor[];
241242
}): boolean {
242243
return (
243244
advancedFilters.size === 0 &&
@@ -312,7 +313,7 @@ export interface IrisGridProps {
312313
/** @deprecated use `partitionConfig` instead */
313314
partitions?: (string | null)[];
314315
partitionConfig?: PartitionConfig;
315-
sorts: readonly DhType.Sort[];
316+
sorts: readonly SortDescriptor[];
316317

317318
/** @deprecated use `reverse` instead */
318319
reverseType?: ReverseType;
@@ -396,7 +397,7 @@ export interface IrisGridState {
396397
shownAdvancedFilter: number | null;
397398
hoverAdvancedFilter: number | null;
398399

399-
sorts: readonly DhType.Sort[];
400+
sorts: readonly SortDescriptor[];
400401
reverse: boolean;
401402
customColumns: readonly ColumnName[];
402403
selectDistinctColumns: readonly ColumnName[];
@@ -1421,7 +1422,7 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
14211422
loadingScrimProgress: number | null,
14221423
quickFilters: ReadonlyQuickFilterMap,
14231424
advancedFilters: ReadonlyAdvancedFilterMap,
1424-
sorts: readonly DhType.Sort[],
1425+
sorts: readonly SortDescriptor[],
14251426
reverse: boolean,
14261427
rollupConfig: UIRollupConfig | undefined,
14271428
isMenuShown: boolean
@@ -1602,6 +1603,11 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
16021603
return '';
16031604
}
16041605

1606+
/**
1607+
* Get the model column index for the provided visible index
1608+
* @param columnIndex Visible column index
1609+
* @returns Model column index, or null if not found
1610+
*/
16051611
getModelColumn(columnIndex: GridRangeIndex): ModelIndex | null | undefined {
16061612
const { metrics } = this.state;
16071613
assertNotNull(metrics);
@@ -1610,6 +1616,11 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
16101616
return null;
16111617
}
16121618

1619+
if (columnIndex != null && columnIndex < 0) {
1620+
// ColumnBy sources aren't movable, so just return the index directly
1621+
return columnIndex;
1622+
}
1623+
16131624
return columnIndex != null ? modelColumns.get(columnIndex) : null;
16141625
}
16151626

@@ -2842,7 +2853,7 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
28422853
}
28432854
}
28442855

2845-
updateSorts(sorts: readonly DhType.Sort[]): void {
2856+
updateSorts(sorts: readonly SortDescriptor[]): void {
28462857
this.startLoading('Sorting...');
28472858
this.setState({ sorts });
28482859
this.grid?.forceUpdate();

packages/iris-grid/src/IrisGridModel.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
type VisibleIndex,
1414
} from '@deephaven/grid';
1515
import type { dh as DhType } from '@deephaven/jsapi-types';
16-
import { type Formatter } from '@deephaven/jsapi-utils';
16+
import { type Formatter, type SortDescriptor } from '@deephaven/jsapi-utils';
1717
import {
1818
type ColumnName,
1919
type UITotalsTableConfig,
@@ -274,12 +274,12 @@ abstract class IrisGridModel<
274274
/**
275275
* @returns The sorts used on this model
276276
*/
277-
abstract get sort(): readonly DhType.Sort[];
277+
abstract get sort(): readonly SortDescriptor[];
278278

279279
/**
280280
* @param sort The sorts to use on this model
281281
*/
282-
abstract set sort(sort: readonly DhType.Sort[]);
282+
abstract set sort(sort: readonly SortDescriptor[]);
283283

284284
/**
285285
/**

packages/iris-grid/src/IrisGridModelUpdater.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useEffect, useMemo } from 'react';
44
import type { dh } from '@deephaven/jsapi-types';
55
import { type ModelIndex, type MoveOperation } from '@deephaven/grid';
6-
import { type Formatter } from '@deephaven/jsapi-utils';
6+
import { type SortDescriptor, type Formatter } from '@deephaven/jsapi-utils';
77
import { EMPTY_ARRAY, EMPTY_MAP } from '@deephaven/utils';
88
import { useOnChange } from '@deephaven/react-hooks';
99
import IrisGridUtils from './IrisGridUtils';
@@ -29,7 +29,7 @@ interface IrisGridModelUpdaterProps {
2929
left: number | null;
3030
right: number | null;
3131
filter: readonly dh.FilterCondition[];
32-
sorts: readonly dh.Sort[];
32+
sorts: readonly SortDescriptor[];
3333
reverse?: boolean;
3434
customColumns: readonly ColumnName[];
3535
movedColumns: readonly MoveOperation[];

packages/iris-grid/src/IrisGridRenderer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import {
99
GridUtils,
1010
type VisibleIndex,
1111
} from '@deephaven/grid';
12-
import type { dh } from '@deephaven/jsapi-types';
13-
import { TableUtils } from '@deephaven/jsapi-utils';
12+
import { type SortDescriptor, TableUtils } from '@deephaven/jsapi-utils';
1413
import { assertNotNull, getOrThrow } from '@deephaven/utils';
1514
import {
1615
type AdvancedFilter,
@@ -52,7 +51,7 @@ export class IrisGridRenderer extends GridRenderer {
5251

5352
protected dataBarCellRenderer = new IrisGridDataBarCellRenderer();
5453

55-
getSortIcon(sort: dh.Sort | null, size: number): Path2D | null {
54+
getSortIcon(sort: SortDescriptor | null, size: number): Path2D | null {
5655
if (!sort) {
5756
return null;
5857
}

packages/iris-grid/src/IrisGridTableModelTemplate.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ import {
2626
Formatter,
2727
FormatterUtils,
2828
DateUtils,
29+
type SortDescriptor,
2930
} from '@deephaven/jsapi-utils';
3031
import IrisGridModel, { type DisplayColumn } from './IrisGridModel';
32+
3133
import AggregationOperation from './sidebar/aggregations/AggregationOperation';
3234
import IrisGridUtils from './IrisGridUtils';
3335
import MissingKeyError from './MissingKeyError';
@@ -1251,13 +1253,13 @@ class IrisGridTableModelTemplate<
12511253
);
12521254
}
12531255

1254-
get sort(): DhType.Sort[] {
1256+
get sort(): readonly SortDescriptor[] {
12551257
return this.table.sort;
12561258
}
12571259

1258-
set sort(sort: DhType.Sort[]) {
1260+
set sort(sort: readonly SortDescriptor[]) {
12591261
this.closeSubscription();
1260-
this.table.applySort(sort);
1262+
this.table.applySort(this.irisGridUtils.hydrateDhSort(this.columns, sort));
12611263
this.applyViewport();
12621264
}
12631265

packages/iris-grid/src/IrisGridTestUtils.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { type GridRangeIndex, type ModelSizeMap } from '@deephaven/grid';
2-
import { type dh as DhType } from '@deephaven/jsapi-types';
3-
import { Formatter } from '@deephaven/jsapi-utils';
2+
import type { dh as DhType } from '@deephaven/jsapi-types';
3+
import { Formatter, type SortDescriptor } from '@deephaven/jsapi-utils';
44
import IrisGridProxyModel from './IrisGridProxyModel';
5+
import IrisGridUtils from './IrisGridUtils';
56

67
class IrisGridTestUtils {
78
static DEFAULT_TYPE = 'java.lang.String';
@@ -20,8 +21,11 @@ class IrisGridTestUtils {
2021

2122
private dh: typeof DhType;
2223

24+
private irisGridUtils: IrisGridUtils;
25+
2326
constructor(dh: typeof DhType) {
2427
this.dh = dh;
28+
this.irisGridUtils = new IrisGridUtils(dh);
2529
}
2630

2731
makeColumn(
@@ -72,9 +76,16 @@ class IrisGridTestUtils {
7276
return new (this.dh as any).FilterCondition();
7377
}
7478

75-
makeSort(): DhType.Sort {
79+
makeSort(column: DhType.Column = this.makeColumn()): DhType.Sort {
7680
// eslint-disable-next-line @typescript-eslint/no-explicit-any
77-
return new (this.dh as any).Sort();
81+
return new (this.dh as any).Sort({ column });
82+
}
83+
84+
hydrateSort(
85+
sortDescriptor: readonly SortDescriptor[],
86+
columns: DhType.Column[]
87+
): DhType.Sort[] {
88+
return this.irisGridUtils.hydrateDhSort(columns, sortDescriptor);
7889
}
7990

8091
makeTable({
@@ -85,7 +96,7 @@ class IrisGridTestUtils {
8596
}: {
8697
columns?: DhType.Column[];
8798
size?: number;
88-
sort?: readonly DhType.Sort[];
99+
sort?: readonly SortDescriptor[];
89100
layoutHints?: Partial<DhType.LayoutHints>;
90101
} = {}): DhType.Table {
91102
// eslint-disable-next-line @typescript-eslint/no-explicit-any

packages/iris-grid/src/IrisGridUtils.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,16 @@ describe('sort exporting/importing', () => {
185185

186186
expect(importedSort).toEqual([
187187
expect.objectContaining({
188-
column: columns[3],
188+
column: expect.objectContaining({
189+
name: columns[3].name,
190+
}),
189191
isAbs: false,
190192
direction: 'ASC',
191193
}),
192194
expect.objectContaining({
193-
column: columns[7],
195+
column: expect.objectContaining({
196+
name: columns[7].name,
197+
}),
194198
isAbs: true,
195199
direction: 'DESC',
196200
}),
@@ -375,6 +379,15 @@ describe('remove columns in moved columns', () => {
375379
});
376380
});
377381

382+
describe('removeSortsInColumns', () => {
383+
it('removes sort for the given column names', () => {
384+
const table = makeTable();
385+
const sort = [table.columns[2].sort(), table.columns[5].sort().desc()];
386+
const newSort = IrisGridUtils.removeSortsInColumns(sort, ['name_2']);
387+
expect(newSort).toEqual([table.columns[5].sort().desc()]);
388+
});
389+
});
390+
378391
describe('getPrevVisibleColumns', () => {
379392
const columns = irisGridTestUtils.makeColumns(5);
380393
it('returns [] for startIndex < 0', () => {

0 commit comments

Comments
 (0)