@1771technologies/lytenyte-pro
Version:
Blazingly fast headless React data grid with 100s of features.
50 lines (49 loc) • 1.81 kB
JavaScript
import { useEvent } from "@1771technologies/lytenyte-core/internal";
export function useOnRowsUpdated(tree, p) {
const onRowsUpdate = useEvent((updates) => {
if (!p.onRowDataChange)
return;
const changes = [];
const top = new Map();
const bottom = new Map();
for (const [row, u] of updates) {
// Updating data
if (row.kind === "branch") {
const node = tree.rowIdToNode.get(row.id);
if (!node) {
console.error(`Attempting to update a node that does not exist: ${row.id}`);
return;
}
let current = node.parent;
const path = [];
while (current.kind !== "root") {
path.push(current.key);
current = current.parent;
}
path.reverse();
changes.push({
key: node.key,
next: u,
parent: node?.parent.data,
prev: node.data,
path,
});
continue;
}
const topIndex = p.topData?.findIndex((r) => r.id === row.id);
if (topIndex != null && topIndex !== -1) {
top.set(topIndex, u);
continue;
}
const botIndex = p.botData?.findIndex((r) => r.id === row.id);
if (botIndex != null && botIndex !== -1) {
bottom.set(botIndex, u);
continue;
}
console.error(`Attempting to update a node that does not exist: ${row.id}`);
return;
}
p.onRowDataChange({ changes, top, bottom });
});
return onRowsUpdate;
}