Revert useApplyGridTransaction changes
This commit is contained in:
parent
1db7919875
commit
afd82a8e45
@ -1,48 +1,10 @@
|
||||
import { GridApi } from 'ag-grid-community';
|
||||
import { useEffect } from 'react';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import { produce } from 'immer';
|
||||
|
||||
export const updateCallback =
|
||||
<T>(
|
||||
gridApiRef: { current: GridApi | null },
|
||||
getRowNodeId: (row: T) => string
|
||||
) =>
|
||||
(data: T[]) => {
|
||||
if (!gridApiRef.current) return;
|
||||
|
||||
const update: T[] = [];
|
||||
const add: T[] = [];
|
||||
|
||||
// split into updates and adds
|
||||
data.forEach((d) => {
|
||||
if (!gridApiRef.current) return;
|
||||
|
||||
const rowNode = gridApiRef.current.getRowNode(getRowNodeId(d));
|
||||
|
||||
if (rowNode) {
|
||||
if (
|
||||
produce(rowNode.data, (draft: T) => Object.assign(draft, d)) !==
|
||||
rowNode.data
|
||||
) {
|
||||
update.push(d);
|
||||
}
|
||||
} else {
|
||||
add.push(d);
|
||||
}
|
||||
});
|
||||
// async transaction for optimal handling of high grequency updates
|
||||
gridApiRef.current.applyTransactionAsync({
|
||||
update,
|
||||
add,
|
||||
addIndex: 0,
|
||||
});
|
||||
};
|
||||
|
||||
export const useApplyGridTransaction = <T>(
|
||||
export const useApplyGridTransaction = <T extends { id: string }>(
|
||||
data: T[],
|
||||
gridApi: GridApi | null,
|
||||
getRowNodeId: (row: T) => string
|
||||
gridApi: GridApi | null
|
||||
) => {
|
||||
useEffect(() => {
|
||||
if (!gridApi) return;
|
||||
@ -54,7 +16,7 @@ export const useApplyGridTransaction = <T>(
|
||||
data.forEach((d) => {
|
||||
if (!gridApi) return;
|
||||
|
||||
const rowNode = gridApi.getRowNode(getRowNodeId(d));
|
||||
const rowNode = gridApi.getRowNode(d.id);
|
||||
|
||||
if (rowNode) {
|
||||
if (!isEqual(rowNode.data, d)) {
|
||||
@ -64,11 +26,11 @@ export const useApplyGridTransaction = <T>(
|
||||
add.push(d);
|
||||
}
|
||||
});
|
||||
// async transaction for optimal handling of high grequency updates
|
||||
|
||||
gridApi.applyTransaction({
|
||||
update,
|
||||
add,
|
||||
addIndex: 0,
|
||||
});
|
||||
}, [data, gridApi, getRowNodeId]);
|
||||
}, [data, gridApi]);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user