sadpandajoe commented on code in PR #36050:
URL: https://github.com/apache/superset/pull/36050#discussion_r2529215743


##########
superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/TableRenderers.jsx:
##########
@@ -71,15 +74,97 @@ function displayHeaderCell(
   );
 }
 
+function sortHierarchicalObject(obj, objSort, rowPartialOnTop) {
+  // Performs a recursive sort of nested object structures. Sorts objects 
based on
+  // their currentVal property. The function preserves the hierarchical 
structure
+  // while sorting each level according to the specified criteria.
+  const sortedKeys = Object.keys(obj).sort((a, b) => {
+    const valA = obj[a].currentVal || 0;
+    const valB = obj[b].currentVal || 0;
+    if (rowPartialOnTop) {
+      if (obj[a].currentVal !== undefined && obj[b].currentVal === undefined) {
+        return -1;
+      }
+      if (obj[b].currentVal !== undefined && obj[a].currentVal === undefined) {
+        return 1;
+      }
+    }
+    return objSort === 'asc' ? valA - valB : valB - valA;
+  });
+
+  const result = new Map();
+  sortedKeys.forEach(key => {
+    const value = obj[key];
+    if (typeof value === 'object' && !Array.isArray(value)) {
+      result.set(key, sortHierarchicalObject(value, objSort, rowPartialOnTop));
+    } else {
+      result.set(key, value);
+    }
+  });
+  return result;
+}
+
+function convertToArray(
+  obj,
+  rowEnabled,
+  rowPartialOnTop,
+  maxRowIndex,
+  parentKeys = [],
+  result = [],
+  flag = false,
+) {
+  // Recursively flattens a hierarchical Map structure into an array of key 
paths.
+  // Handles different rendering scenarios based on row grouping 
configurations and
+  // depth limitations. The function supports complex hierarchy flattening with
+  let updatedFlag = flag;
+
+  const keys = Array.from(obj.keys());
+  const getValue = key => obj.get(key);
+
+  keys.forEach(key => {
+    if (key === 'currentVal') {
+      return;
+    }
+    const value = getValue(key);
+    if (rowEnabled && rowPartialOnTop && parentKeys.length < maxRowIndex - 1) {
+      result.push(parentKeys.length > 0 ? [...parentKeys, key] : [key]);
+      updatedFlag = true;
+    }
+    if (typeof value === 'object' && !Array.isArray(value)) {
+      convertToArray(
+        value,
+        rowEnabled,
+        rowPartialOnTop,
+        maxRowIndex,
+        [...parentKeys, key],
+        result,
+      );
+    }
+    if (
+      parentKeys.length >= maxRowIndex - 1 ||
+      (rowEnabled && !rowPartialOnTop)
+    ) {
+      if (!updatedFlag) {
+        result.push(parentKeys.length > 0 ? [...parentKeys, key] : [key]);
+        return;
+      }
+    }
+    if (parentKeys.length === 0 && maxRowIndex === 1) {
+      result.push([key]);
+    }
+  });
+  return result;
+}
+
 export class TableRenderer extends Component {
   constructor(props) {
     super(props);
 
     // We need state to record which entries are collapsed and which aren't.
     // This is an object with flat-keys indicating if the corresponding rows
     // should be collapsed.
-    this.state = { collapsedRows: {}, collapsedCols: {} };
-
+    this.state = { collapsedRows: {}, collapsedCols: {}, sortingOrder: [] };
+    this.sortCache = new Map();

Review Comment:
   I think copilot might be right here that we need to clear the cache. 
   
   The sortCache needs to be cleared not just on unmount, but also when props 
change (when this.cachedProps !== this.props in the render method). Otherwise, 
when the underlying pivot data changes, the old cached sort results will be 
returned for the new data since the cache key doesn't include any data 
identifier.



-- 
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