mirror of
https://github.com/apache/superset.git
synced 2026-05-08 09:25:56 +00:00
fix(dashboard): preserve dynamic group by column order (#39333)
(cherry picked from commit 0f417f0040)
This commit is contained in:
committed by
Michael S. Molina
parent
2c163576a7
commit
2b4e9909db
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
import {
|
||||
type ColDef,
|
||||
type ColumnState,
|
||||
} from '@superset-ui/core/components/ThemedAgGridReact';
|
||||
|
||||
type ColumnGroupDef = ColDef & {
|
||||
children?: ColumnDefLike[];
|
||||
};
|
||||
|
||||
type ColumnDefLike = ColDef | ColumnGroupDef;
|
||||
|
||||
function hasChildren(colDef: ColumnDefLike): colDef is ColumnGroupDef {
|
||||
return 'children' in colDef;
|
||||
}
|
||||
|
||||
export interface ReconciledColumnState {
|
||||
applyOrder: boolean;
|
||||
columnState: ColumnState[];
|
||||
}
|
||||
|
||||
export function getLeafColumnIds(colDefs: ColumnDefLike[]): string[] {
|
||||
return colDefs.flatMap(colDef => {
|
||||
if (
|
||||
hasChildren(colDef) &&
|
||||
Array.isArray(colDef.children) &&
|
||||
colDef.children.length > 0
|
||||
) {
|
||||
return getLeafColumnIds(colDef.children);
|
||||
}
|
||||
|
||||
return typeof colDef.field === 'string' ? [colDef.field] : [];
|
||||
});
|
||||
}
|
||||
|
||||
export default function reconcileColumnState(
|
||||
savedColumnState: ColumnState[] | undefined,
|
||||
colDefs: ColumnDefLike[],
|
||||
): ReconciledColumnState | null {
|
||||
if (!Array.isArray(savedColumnState) || savedColumnState.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const currentColumnIds = getLeafColumnIds(colDefs);
|
||||
const currentColumnIdSet = new Set(currentColumnIds);
|
||||
const filteredColumnState = savedColumnState.filter(
|
||||
column =>
|
||||
typeof column.colId === 'string' && currentColumnIdSet.has(column.colId),
|
||||
);
|
||||
|
||||
if (filteredColumnState.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const savedColumnIdSet = new Set(
|
||||
filteredColumnState.map(column => column.colId),
|
||||
);
|
||||
const hasSameColumnSet =
|
||||
currentColumnIds.length === savedColumnIdSet.size &&
|
||||
currentColumnIds.every(columnId => savedColumnIdSet.has(columnId));
|
||||
|
||||
return {
|
||||
columnState: filteredColumnState,
|
||||
applyOrder: hasSameColumnSet,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user