SBIN2010 commented on code in PR #43938:
URL: https://github.com/apache/superset/pull/43938#discussion_r3982999166


##########
superset-frontend/packages/superset-ui-chart-controls/src/utils/headerGroups.ts:
##########
@@ -0,0 +1,719 @@
+/**
+ * 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 { t } from '@apache-superset/core/translation';
+import {
+  ComparisonType,
+  ensureIsArray,
+  getColumnLabel,
+  getMetricLabel,
+  QueryFormColumn,
+  QueryFormMetric,
+  QueryMode,
+  SqlaFormData,
+} from '@superset-ui/core';
+import { isEmpty, last } from 'lodash-es';
+import {
+  isPercentMetric,
+  isRegularMetric,
+  shouldSkipMetricColumn,
+} from './metricColumnFilter';
+
+export type HeaderGroupLabelAlign = 'left' | 'center' | 'right';
+
+export type HeaderGroupPlacement = 'left' | 'right';
+
+export type HeaderGroupConfig = {
+  id: string;
+  label: string;
+  columns: string[];
+  labelAlign?: HeaderGroupLabelAlign;
+  placement?: HeaderGroupPlacement;
+  source?: 'time_compare';
+  children?: HeaderGroupConfig[];
+};
+
+export type HeaderGroupCell = {
+  key: string;
+  label: string;
+  colSpan: number;
+  rowSpan: number;
+  columnIndex: number;
+  labelAlign?: HeaderGroupLabelAlign;
+  isLastColumn: boolean;
+};
+
+export function getTimeComparisonColumnKeys(colname: string): string[] {
+  return [
+    `${t('Main')} ${colname}`,
+    `# ${colname}`,
+    `△ ${colname}`,
+    `% ${colname}`,
+  ];
+}
+
+export function expandGroupColumnKey(
+  identifier: string,
+  visibleKeys: string[],
+): string[] {
+  const visible = new Set(visibleKeys);
+  const candidates = visible.has(identifier)
+    ? [identifier]
+    : [
+        `%${identifier}`,
+        ...getTimeComparisonColumnKeys(identifier),
+        `Main ${identifier}`,
+      ];
+  const matchSet = new Set(candidates.filter(key => visible.has(key)));
+  return visibleKeys.filter(key => matchSet.has(key));
+}
+
+export function buildTimeComparisonHeaderGroups(
+  metricKeys: string[],
+  labelFor: (key: string) => string = key => key,
+): HeaderGroupConfig[] {
+  return metricKeys.map(key => ({
+    id: `time-compare-${key}`,
+    label: labelFor(key),
+    columns: getTimeComparisonColumnKeys(key),
+    labelAlign: 'left',
+    placement: 'right',
+    source: 'time_compare',
+  }));
+}
+
+function isTimeComparisonSlotKey(column: string, prefix: string): boolean {
+  return column.startsWith(`${prefix} `);
+}
+
+function remapTimeComparisonColumns(
+  columns: string[],
+  currentKeys: string[],
+): string[] {
+  const currentSet = new Set(currentKeys);
+  if (columns.every(column => currentSet.has(column))) {
+    return columns;
+  }
+  const mainKey = currentKeys.find(
+    key =>
+      !isTimeComparisonSlotKey(key, '#') &&
+      !isTimeComparisonSlotKey(key, '△') &&
+      !isTimeComparisonSlotKey(key, '%'),
+  );
+  const hashKey = currentKeys.find(key => isTimeComparisonSlotKey(key, '#'));
+  const deltaKey = currentKeys.find(key => isTimeComparisonSlotKey(key, '△'));
+  const percentKey = currentKeys.find(key => isTimeComparisonSlotKey(key, 
'%'));
+  const remapped = columns
+    .map(column => {
+      if (currentSet.has(column)) {
+        return column;
+      }
+      if (isTimeComparisonSlotKey(column, '#')) {
+        return hashKey;
+      }
+      if (isTimeComparisonSlotKey(column, '△')) {
+        return deltaKey;
+      }
+      if (isTimeComparisonSlotKey(column, '%')) {
+        return percentKey;
+      }
+      return mainKey;
+    })
+    .filter((column): column is string => Boolean(column));
+  return remapped.length > 0 ? [...new Set(remapped)] : currentKeys;
+}
+
+function refreshTimeComparisonGroup(
+  group: HeaderGroupConfig,
+  currentKeys: string[],
+  replaceColumns: boolean,
+): HeaderGroupConfig {
+  return {
+    ...group,
+    columns: replaceColumns
+      ? currentKeys
+      : remapTimeComparisonColumns(group.columns ?? [], currentKeys),
+    children: group.children?.map(child =>
+      refreshTimeComparisonGroup(child, currentKeys, false),
+    ),
+  };
+}
+
+export function syncTimeComparisonGroups(
+  groups: HeaderGroupConfig[],
+  timeComparisonGroups: HeaderGroupConfig[] = [],
+): HeaderGroupConfig[] {
+  const autoById = new Map(
+    timeComparisonGroups.map(group => [group.id, group]),
+  );
+  const existingAutoIds = new Set(
+    groups
+      .filter(group => group.source === 'time_compare')
+      .map(group => group.id),
+  );
+  const kept = groups
+    .filter(group => group.source !== 'time_compare' || autoById.has(group.id))
+    .map(group => {
+      if (group.source !== 'time_compare') {

Review Comment:
   Groups are no longer skipped when the locale changes: 
`syncTimeComparisonGroups` and `pruneStaleHeaderGroupColumns` overwrite the old 
translation with the stable ID instead of discarding the column.
   fix in 828e0cf



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to