Skip to content

Commit

Permalink
Add legacy format for dehydrated state
Browse files Browse the repository at this point in the history
  • Loading branch information
georgecwan committed Oct 25, 2023
1 parent c8b5b1f commit 9610545
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class MockIrisGridTreeModel
throw new Error('Not defined in mock');
}

valuesTable(columns: Column[]): Promise<never> {
valuesTable(columns: Column | Column[]): Promise<never> {
throw new Error('Not defined in mock');
}

Expand Down
18 changes: 8 additions & 10 deletions packages/dashboard-core-plugins/src/panels/IrisGridPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
IrisGridState,
ChartBuilderSettings,
DehydratedIrisGridState,
DehydratedIrisGridPanelState,
ColumnHeaderGroup,
IrisGridContextMenuData,
IrisGridTableModel,
Expand Down Expand Up @@ -114,23 +115,20 @@ export interface PanelState {
movedRows: MoveOperation[];
};
irisGridState: DehydratedIrisGridState;
irisGridPanelState: {
partitionColumns: ColumnName[];
partitions: (string | null)[];
isSelectingPartition: boolean;
advancedSettings: [AdvancedSettingsType, boolean][];
};
irisGridPanelState: DehydratedIrisGridPanelState;
pluginState: unknown;
}

// Some of the properties in the loaded panel state may be omitted
// even though they can't be undefined in the dehydrated state.
// This can happen when loading the state saved before the properties were added.
type LoadedPanelState = PanelState & {
irisGridPanelState: PanelState['irisGridPanelState'] &
Partial<
Pick<PanelState['irisGridPanelState'], 'partitions' | 'partitionColumns'>
>;
irisGridPanelState: PanelState['irisGridPanelState'] & {
partitions?: (string | null)[];
partitionColumns?: ColumnName[];
partition?: string | null;
partitionColumn?: ColumnName;
};
};

export interface IrisGridPanelProps extends DashboardPanelProps {
Expand Down
5 changes: 1 addition & 4 deletions packages/iris-grid/src/IrisGridPartitionSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ class IrisGridPartitionSelector<T> extends Component<
}

const newPartitions = [...partitions];
newPartitions[index] =
TableUtils.isCharType(columns[index].type) && partition.length > 0
? partition.charCodeAt(0).toString()
: partition;
newPartitions[index] = partition;
if (partitionTables) {
this.updatePartitionFilters(newPartitions, partitionTables);
}
Expand Down
73 changes: 49 additions & 24 deletions packages/iris-grid/src/IrisGridUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,38 @@ export interface DehydratedIrisGridState {
columnHeaderGroups?: readonly ColumnGroup[];
}

export interface DehydratedIrisGridPanelStateV1 {
isSelectingPartition: boolean;
partition: string | null;
partitionColumn: ColumnName;
advancedSettings: [AdvancedSettingsType, boolean][];
}

export interface DehydratedIrisGridPanelStateV2 {
isSelectingPartition: boolean;
partitions: (string | null)[];
partitionColumns: ColumnName[];
advancedSettings: [AdvancedSettingsType, boolean][];
}

export type DehydratedIrisGridPanelState =
| DehydratedIrisGridPanelStateV1
| DehydratedIrisGridPanelStateV2;

export function isPanelStateV1(
state: DehydratedIrisGridPanelState
): state is DehydratedIrisGridPanelStateV1 {
return (state as DehydratedIrisGridPanelStateV1).partitionColumn != null;
}

export function isPanelStateV2(
state: DehydratedIrisGridPanelState
): state is DehydratedIrisGridPanelStateV2 {
return Array.isArray(
(state as DehydratedIrisGridPanelStateV2).partitionColumns
);
}

/**
* Checks if an index is valid for the given array
* @param x The index to check
Expand Down Expand Up @@ -279,12 +311,7 @@ class IrisGridUtils {
partitionColumns: Column[];
advancedSettings: Map<AdvancedSettingsType, boolean>;
}
): {
isSelectingPartition: boolean;
partitions: (string | null)[];
partitionColumns: ColumnName[];
advancedSettings: [AdvancedSettingsType, boolean][];
} {
): DehydratedIrisGridPanelState {
const {
isSelectingPartition,
partitions,
Expand All @@ -311,25 +338,21 @@ class IrisGridUtils {
*/
static hydrateIrisGridPanelState(
model: IrisGridModel,
irisGridPanelState: {
// This needs to be changed after IrisGridPanel is done
isSelectingPartition: boolean;
partitions: (string | null)[];
partitionColumns: ColumnName[];
advancedSettings: [AdvancedSettingsType, boolean][];
}
irisGridPanelState: DehydratedIrisGridPanelState
): {
isSelectingPartition: boolean;
partitions: (string | null)[];
partitionColumns: Column[];
advancedSettings: Map<AdvancedSettingsType, boolean>;
} {
const {
isSelectingPartition,
partitions,
partitionColumns,
advancedSettings,
} = irisGridPanelState;
const { isSelectingPartition, advancedSettings } = irisGridPanelState;

const { partitionColumns, partitions } = isPanelStateV2(irisGridPanelState)
? irisGridPanelState
: {
partitionColumns: [irisGridPanelState.partitionColumn],
partitions: [irisGridPanelState.partition],
};

const { columns } = model;
return {
Expand Down Expand Up @@ -391,10 +414,7 @@ class IrisGridUtils {
static extractTableSettings<AF, QF, S>(
panelState: {
irisGridState: { advancedFilters: AF; quickFilters: QF; sorts: S };
irisGridPanelState: {
partitionColumns: ColumnName[];
partitions: unknown[];
};
irisGridPanelState: DehydratedIrisGridPanelState;
},
inputFilters: InputFilter[] = []
): {
Expand All @@ -406,7 +426,12 @@ class IrisGridUtils {
sorts: S;
} {
const { irisGridPanelState, irisGridState } = panelState;
const { partitionColumns, partitions } = irisGridPanelState;
const { partitionColumns, partitions } = isPanelStateV2(irisGridPanelState)
? irisGridPanelState
: {
partitionColumns: [irisGridPanelState.partitionColumn],
partitions: [irisGridPanelState.partition],
};
const { advancedFilters, quickFilters, sorts } = irisGridState;

return {
Expand Down

0 comments on commit 9610545

Please sign in to comment.