@1771technologies/lytenyte-pro
Version:
Blazingly fast headless React data grid with 100s of features.
37 lines (36 loc) • 1.55 kB
JavaScript
import { computePathTree, } from "@1771technologies/lytenyte-shared";
import { useMemo } from "react";
export function useFlattenedTree(paths, expansions, expansionDefault, nonAdjacentPathTrees) {
const { flat, nodeToIndex, indexToId, allIds, idToNode, root } = useMemo(() => {
const root = computePathTree(paths, undefined, nonAdjacentPathTrees);
const flat = [];
const nodeToIndex = new Map();
const stack = [...root.children.values()];
const allIds = new Set();
const indexToId = new Map();
const idToNode = new Map();
let index = 0;
while (stack.length) {
const item = stack.shift();
if (item.kind === "leaf") {
flat.push(item);
}
else {
flat.push(item);
const isExpanded = expansions[item.id] ?? expansionDefault;
if (isExpanded) {
const children = [...item.children.values()];
stack.unshift(...children);
}
}
const id = item.kind === "branch" ? item.data.idOccurrence : item.data.id;
allIds.add(id);
indexToId.set(index, id);
nodeToIndex.set(item, index);
idToNode.set(id, item);
index++;
}
return { flat, nodeToIndex, indexToId, allIds, idToNode, root };
}, [expansionDefault, expansions, nonAdjacentPathTrees, paths]);
return { flat, nodeToIndex, indexToId, allIds, idToNode, root };
}