| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {RepoPath} from 'shared/types/common'; |
| b69ab31 | | | 9 | import type {UseUncommittedSelection} from './partialSelection'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | export type PathTree<T> = Map<string, PathTree<T> | T>; |
| b69ab31 | | | 12 | /** |
| b69ab31 | | | 13 | * Path tree reconstructs the tree structure from a set of paths, |
| b69ab31 | | | 14 | * then compresses path names for single-child directories. |
| b69ab31 | | | 15 | * |
| b69ab31 | | | 16 | * ``` |
| b69ab31 | | | 17 | * buildPathTree({ |
| b69ab31 | | | 18 | * 'a/b/file1.txt': {...}, |
| b69ab31 | | | 19 | * 'a/b/file2.txt': {...}, |
| b69ab31 | | | 20 | * 'a/file3.txt': {...}, |
| b69ab31 | | | 21 | * 'a/d/e/f/file4.txt': {...}, |
| b69ab31 | | | 22 | * 'q/file5.txt': {...}, |
| b69ab31 | | | 23 | * }) |
| b69ab31 | | | 24 | * Map{ |
| b69ab31 | | | 25 | * a: Map{ |
| b69ab31 | | | 26 | * b: Map{ |
| b69ab31 | | | 27 | * 'file1.txt' : {...}, |
| b69ab31 | | | 28 | * 'file2.txt' : {...}, |
| b69ab31 | | | 29 | * }, |
| b69ab31 | | | 30 | * 'file3.txt': {...}, |
| b69ab31 | | | 31 | * 'd/e/f': Map{ |
| b69ab31 | | | 32 | * 'file4.txt': {...} |
| b69ab31 | | | 33 | * } |
| b69ab31 | | | 34 | * }, |
| b69ab31 | | | 35 | * q: Map{ |
| b69ab31 | | | 36 | * 'file5.txt': {...}, |
| b69ab31 | | | 37 | * } |
| b69ab31 | | | 38 | * } |
| b69ab31 | | | 39 | * ``` |
| b69ab31 | | | 40 | */ |
| b69ab31 | | | 41 | export function buildPathTree<T>(paths: Record<RepoPath, T>): PathTree<T> { |
| b69ab31 | | | 42 | function recurse(input: Map<string, T>) { |
| b69ab31 | | | 43 | const intermediateTree: Map<string, Map<string, T>> = new Map(); |
| b69ab31 | | | 44 | const plainFiles = new Map<string, T>(); |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | // group files by common path |
| b69ab31 | | | 47 | for (const [path, data] of input.entries()) { |
| b69ab31 | | | 48 | const [folder] = path.split('/', 1); |
| b69ab31 | | | 49 | const rest = path.slice(folder.length + 1); |
| b69ab31 | | | 50 | if (rest === '') { |
| b69ab31 | | | 51 | // no more folders in this path, use the data directly |
| b69ab31 | | | 52 | plainFiles.set(folder, data); |
| b69ab31 | | | 53 | } else if (intermediateTree.has(folder)) { |
| b69ab31 | | | 54 | const existing = intermediateTree.get(folder); |
| b69ab31 | | | 55 | existing?.set(rest, data); |
| b69ab31 | | | 56 | } else { |
| b69ab31 | | | 57 | intermediateTree.set(folder, new Map([[rest, data]])); |
| b69ab31 | | | 58 | } |
| b69ab31 | | | 59 | } |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | // recurse into each grouping |
| b69ab31 | | | 62 | const tree: PathTree<T> = new Map(); |
| b69ab31 | | | 63 | for (const [key, value] of intermediateTree.entries()) { |
| b69ab31 | | | 64 | const resultTree = recurse(value); |
| b69ab31 | | | 65 | // if a folder 'a' contains exactly one subfolder 'b', we can collapse it into just 'a/b' |
| b69ab31 | | | 66 | if (resultTree.size === 1) { |
| b69ab31 | | | 67 | const [innerkey, inner] = resultTree.entries().next().value; |
| b69ab31 | | | 68 | if (!(inner instanceof Map)) { |
| b69ab31 | | | 69 | // the single file is the bottom of the tree, don't absorb it |
| b69ab31 | | | 70 | tree.set(key, resultTree); |
| b69ab31 | | | 71 | } else { |
| b69ab31 | | | 72 | tree.set(key + '/' + innerkey, inner); |
| b69ab31 | | | 73 | } |
| b69ab31 | | | 74 | } else { |
| b69ab31 | | | 75 | tree.set(key, resultTree); |
| b69ab31 | | | 76 | } |
| b69ab31 | | | 77 | } |
| b69ab31 | | | 78 | |
| b69ab31 | | | 79 | // re-add the file entries we found |
| b69ab31 | | | 80 | for (const [key, value] of plainFiles.entries()) { |
| b69ab31 | | | 81 | tree.set(key, value); |
| b69ab31 | | | 82 | } |
| b69ab31 | | | 83 | |
| b69ab31 | | | 84 | return tree; |
| b69ab31 | | | 85 | } |
| b69ab31 | | | 86 | |
| b69ab31 | | | 87 | return recurse(new Map(Object.entries(paths))); |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | |
| b69ab31 | | | 90 | /** |
| b69ab31 | | | 91 | * Compute accumulated selected state of directory nodes in the tree, so we don't need to repeatedly |
| b69ab31 | | | 92 | * check isSelected among child nodes. |
| b69ab31 | | | 93 | * Note that this is only stored for directories (non-leaf), not files (leaves) |
| b69ab31 | | | 94 | * given tree like |
| b69ab31 | | | 95 | * Map{ |
| b69ab31 | | | 96 | * a: Map{ |
| b69ab31 | | | 97 | * b: Map{ |
| b69ab31 | | | 98 | * 'file1.txt' : checked |
| b69ab31 | | | 99 | * 'file2.txt' : checked, |
| b69ab31 | | | 100 | * }, |
| b69ab31 | | | 101 | * c: Map{ |
| b69ab31 | | | 102 | * 'file1.txt' : checked |
| b69ab31 | | | 103 | * 'file2.txt' : unchecked, |
| b69ab31 | | | 104 | * }, |
| b69ab31 | | | 105 | * d: Map{ |
| b69ab31 | | | 106 | * 'file1.txt' : checked |
| b69ab31 | | | 107 | * 'file2.txt' : partiallyChecked, |
| b69ab31 | | | 108 | * }, |
| b69ab31 | | | 109 | * }, |
| b69ab31 | | | 110 | * q: Map{ |
| b69ab31 | | | 111 | * 'file6.txt': unchecked |
| b69ab31 | | | 112 | * } |
| b69ab31 | | | 113 | * } |
| b69ab31 | | | 114 | * returns: |
| b69ab31 | | | 115 | * a -> 'indeterminate' |
| b69ab31 | | | 116 | * a/b -> true |
| b69ab31 | | | 117 | * a/c -> 'indeterminate' |
| b69ab31 | | | 118 | * a/d -> 'indeterminate' |
| b69ab31 | | | 119 | * q -> false |
| b69ab31 | | | 120 | */ |
| b69ab31 | | | 121 | export function calculateTreeSelectionStates<T extends {path: string}>( |
| b69ab31 | | | 122 | tree: PathTree<T>, |
| b69ab31 | | | 123 | selection: UseUncommittedSelection, |
| b69ab31 | | | 124 | ): Map<string, boolean | 'indeterminate'> { |
| b69ab31 | | | 125 | const checkedStates = new Map<string, boolean | 'indeterminate'>(); |
| b69ab31 | | | 126 | function inner(key: string, node: PathTree<T> | T): boolean | 'indeterminate' { |
| b69ab31 | | | 127 | if (node instanceof Map) { |
| b69ab31 | | | 128 | // every child checked -> checked |
| b69ab31 | | | 129 | // any child partially checked -> indeterminate |
| b69ab31 | | | 130 | // no child checked -> unchecked |
| b69ab31 | | | 131 | let allChecked = true; |
| b69ab31 | | | 132 | let anyChecked = false; |
| b69ab31 | | | 133 | let anyPartiallyChecked = false; |
| b69ab31 | | | 134 | |
| b69ab31 | | | 135 | for (const [childKey, child] of node.entries()) { |
| b69ab31 | | | 136 | const childChecked = inner(key + '/' + childKey, child); |
| b69ab31 | | | 137 | if (childChecked === false) { |
| b69ab31 | | | 138 | allChecked = false; |
| b69ab31 | | | 139 | } else if (childChecked === 'indeterminate') { |
| b69ab31 | | | 140 | anyPartiallyChecked = true; |
| b69ab31 | | | 141 | allChecked = false; |
| b69ab31 | | | 142 | anyChecked = true; |
| b69ab31 | | | 143 | } else { |
| b69ab31 | | | 144 | anyChecked = true; |
| b69ab31 | | | 145 | } |
| b69ab31 | | | 146 | } |
| b69ab31 | | | 147 | const checked = anyPartiallyChecked |
| b69ab31 | | | 148 | ? 'indeterminate' |
| b69ab31 | | | 149 | : allChecked |
| b69ab31 | | | 150 | ? true |
| b69ab31 | | | 151 | : anyChecked |
| b69ab31 | | | 152 | ? 'indeterminate' |
| b69ab31 | | | 153 | : false; |
| b69ab31 | | | 154 | checkedStates.set(key, checked); |
| b69ab31 | | | 155 | return checked; |
| b69ab31 | | | 156 | } else { |
| b69ab31 | | | 157 | const checked = selection.isFullySelected(node.path) |
| b69ab31 | | | 158 | ? true |
| b69ab31 | | | 159 | : selection.isFullyOrPartiallySelected(node.path) |
| b69ab31 | | | 160 | ? 'indeterminate' |
| b69ab31 | | | 161 | : false; |
| b69ab31 | | | 162 | return checked; |
| b69ab31 | | | 163 | } |
| b69ab31 | | | 164 | } |
| b69ab31 | | | 165 | inner('', tree); |
| b69ab31 | | | 166 | return checkedStates; |
| b69ab31 | | | 167 | } |