export function toTree( nodes: T[], ) { const map = new Map(); nodes.forEach((node) => map.set(node.id, { ...node })); const roots: Array = []; map.forEach((node) => { const parent = node.pid ? map.get(node.pid) : null; if (parent) { parent.children = parent.children || []; parent.children.push(node); } else { roots.push(node); } }); return roots; }