Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const useGridAggregation = (
);

const abortControllerRef = React.useRef<AbortController | null>(null);
const hasAppliedAggregationRef = React.useRef(false);
const applyAggregation = React.useCallback(
(reason?: 'filter' | 'sort') => {
// Abort previous if any
Expand All @@ -107,7 +108,11 @@ export const useGridAggregation = (
const aggregatedFields = Object.keys(aggregationRules);
const currentAggregationLookup = gridAggregationLookupSelector(apiRef);
const needsSorting = shouldApplySorting(aggregationRules, aggregatedFields);
if (reason === 'sort' && !needsSorting) {
// Only skip re-applying aggregation if:
// 1. This is triggered by a sort event
// 2. None of the aggregated columns need sorting
// 3. Aggregation has been applied at least once before
if (reason === 'sort' && !needsSorting && hasAppliedAggregationRef.current) {
// no need to re-apply aggregation on `sortedRowsSet` if sorting is not needed
return;
}
Expand Down Expand Up @@ -149,6 +154,7 @@ export const useGridAggregation = (
apiRef.current.applySorting();
}
abortControllerRef.current = null;
hasAppliedAggregationRef.current = true;
return;
}

Expand Down Expand Up @@ -199,6 +205,7 @@ export const useGridAggregation = (
...state,
aggregation: { ...state.aggregation, lookup: {} },
}));
hasAppliedAggregationRef.current = true;
}
},
[
Expand Down Expand Up @@ -286,6 +293,12 @@ export const useGridAggregation = (
useGridEvent(apiRef, 'columnsChange', checkAggregationRulesDiff);
useGridEvent(apiRef, 'filteredRowsSet', deferredApplyAggregation);
useGridEvent(apiRef, 'sortedRowsSet', () => deferredApplyAggregation('sort'));
useGridEvent(apiRef, 'rowsSet', () => {
// Reset the flag so that subsequent aggregation (even from sort) will run
// This ensures aggregation recalculates when rows load asynchronously
hasAppliedAggregationRef.current = false;
deferredApplyAggregation();
});

/**
* EFFECTS
Expand Down
Loading