VirtualizedTree.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {TreeLike} from 'sentry/utils/profiling/hooks/useVirtualizedTree/useVirtualizedTree';
  2. import {VirtualizedTreeNode} from './VirtualizedTreeNode';
  3. export class VirtualizedTree<T extends TreeLike> {
  4. roots: VirtualizedTreeNode<T>[] = [];
  5. flattened: VirtualizedTreeNode<T>[] = [];
  6. constructor(roots: VirtualizedTreeNode<T>[], flattenedList?: VirtualizedTreeNode<T>[]) {
  7. this.roots = roots;
  8. this.flattened = flattenedList || VirtualizedTree.toExpandedList(this.roots);
  9. }
  10. // Rebuilds the tree
  11. static fromRoots<T extends TreeLike>(
  12. items: T[],
  13. skipFn: (n: VirtualizedTreeNode<T>) => boolean = () => false,
  14. expandedNodes?: Set<T>
  15. ): VirtualizedTree<T> {
  16. const roots: VirtualizedTreeNode<T>[] = [];
  17. function toTreeNode(
  18. node: T,
  19. parent: VirtualizedTreeNode<T> | null,
  20. collection: VirtualizedTreeNode<T>[] | null,
  21. depth: number
  22. ) {
  23. const treeNode = new VirtualizedTreeNode<T>(
  24. node,
  25. parent,
  26. depth,
  27. expandedNodes ? expandedNodes.has(node) : false
  28. );
  29. // We cannot skip root nodes, so we check that the parent is not null.
  30. // If the node should be skipped, then we don't add it to the tree and descend
  31. // into its children without incrementing the depth.
  32. if (parent && skipFn(treeNode)) {
  33. for (let i = 0; i < node.children.length; i++) {
  34. toTreeNode(node.children[i] as T, treeNode, parent.children, depth);
  35. }
  36. return;
  37. }
  38. if (collection) {
  39. collection.push(treeNode);
  40. }
  41. for (let i = 0; i < node.children.length; i++) {
  42. toTreeNode(node.children[i] as T, treeNode, treeNode.children, depth + 1);
  43. }
  44. }
  45. for (let i = 0; i < items.length; i++) {
  46. toTreeNode(items[i], null, roots, 0);
  47. }
  48. return new VirtualizedTree<T>(roots, undefined);
  49. }
  50. // Returns a list of nodes that are visible in the tree.
  51. static toExpandedList<T extends TreeLike>(
  52. nodes: VirtualizedTreeNode<T>[]
  53. ): VirtualizedTreeNode<T>[] {
  54. const list: VirtualizedTreeNode<T>[] = [];
  55. function visit(node: VirtualizedTreeNode<T>): void {
  56. list.push(node);
  57. if (!node.expanded) {
  58. return;
  59. }
  60. for (let i = 0; i < node.children.length; i++) {
  61. visit(node.children[i]);
  62. }
  63. }
  64. for (let i = 0; i < nodes.length; i++) {
  65. visit(nodes[i]);
  66. }
  67. return list;
  68. }
  69. expandNode(
  70. node: VirtualizedTreeNode<T>,
  71. value: boolean,
  72. opts?: {expandChildren: boolean}
  73. ) {
  74. // Because node.setExpanded handles toggling the node and all it's children, we still need to update the
  75. // flattened list. To do that w/o having to rebuild the entire tree, we can just remove the node and add them
  76. const removedOrAddedNodes = node.setExpanded(value, opts);
  77. // If toggling the node resulted in no changes to the actual tree, do nothing
  78. if (!removedOrAddedNodes.length) {
  79. return removedOrAddedNodes;
  80. }
  81. // If a node was expanded, we need to add all of its children to the flattened list.
  82. if (node.expanded) {
  83. this.flattened.splice(this.flattened.indexOf(node) + 1, 0, ...removedOrAddedNodes);
  84. } else {
  85. // If a node was collapsed, we need to remove all of its children from the flattened list.
  86. this.flattened.splice(this.flattened.indexOf(node) + 1, removedOrAddedNodes.length);
  87. }
  88. return removedOrAddedNodes;
  89. }
  90. // Sorts the entire tree and rebuilds the flattened list.
  91. sort(sortFn: (a: VirtualizedTreeNode<T>, b: VirtualizedTreeNode<T>) => number) {
  92. if (!this.roots.length) {
  93. return;
  94. }
  95. function visit(node: VirtualizedTreeNode<T>) {
  96. const sortedChildren = node.children.sort(sortFn);
  97. for (let i = 0; i < sortedChildren.length; i++) {
  98. visit(sortedChildren[i]);
  99. }
  100. }
  101. const sortedRoots = this.roots.sort(sortFn);
  102. for (let i = 0; i < sortedRoots.length; i++) {
  103. visit(sortedRoots[i]);
  104. }
  105. this.flattened = VirtualizedTree.toExpandedList(this.roots);
  106. }
  107. getAllExpandedNodes(previouslyExpandedNodes: Set<T>): Set<T> {
  108. const expandedNodes = new Set<T>([...previouslyExpandedNodes]);
  109. function visit(node: VirtualizedTreeNode<T>) {
  110. if (node.expanded) {
  111. expandedNodes.add(node.node);
  112. }
  113. for (let i = 0; i < node.children.length; i++) {
  114. visit(node.children[i]);
  115. }
  116. }
  117. for (let i = 0; i < this.roots.length; i++) {
  118. visit(this.roots[i]);
  119. }
  120. return expandedNodes;
  121. }
  122. }