@1771technologies/lytenyte-pro
Version:
Blazingly fast headless React data grid with 100s of features.
40 lines (39 loc) • 1.31 kB
JavaScript
import { useEvent } from "@1771technologies/lytenyte-core/internal";
export function useRowDelete(source, onRowDelete, optimistic = false) {
const rowDelete = useEvent((rows) => {
if (!onRowDelete || !rows.length)
return null;
if (!optimistic) {
onRowDelete({ rows });
return;
}
const rollbackNodes = [];
const startDeletes = [];
const endDeletes = [];
rows.forEach((x) => {
const node = source.tree.rowIdToNode.get(x.id);
if (node && node.kind === "leaf" && node.optimistic) {
if (node.optimistic === "start")
startDeletes.push(node.row.id);
else
endDeletes.push(node.row.id);
return;
}
if (node)
node.deleted = true;
rollbackNodes.push(node);
});
if (startDeletes.length)
source.deleteBefore(startDeletes);
if (endDeletes.length)
source.deleteAfter(endDeletes);
onRowDelete({ rows }).catch(() => {
rollbackNodes.forEach((x) => {
delete x.deleted;
});
source.flatten();
});
source.flatten();
});
return rowDelete;
}