Open History to see the last commit
1import type { ProjectEntry } from "@t3tools/contracts";
2
3export interface FileTreeNode {
4 readonly path: string;
5 readonly name: string;
6 readonly kind: ProjectEntry["kind"];
7 readonly ignored?: boolean;
8 readonly children: ReadonlyArray<FileTreeNode>;
9}
10
11export interface VisibleFileTreeNode {
12 readonly node: FileTreeNode;
13 readonly depth: number;
14}
15
16interface MutableFileTreeNode {
17 path: string;
18 name: string;
19 kind: ProjectEntry["kind"];
20 ignored?: boolean;
21 children: Map<string, MutableFileTreeNode>;
22}
23
24function createMutableNode(
25 path: string,
26 name: string,
27 kind: ProjectEntry["kind"],
28): MutableFileTreeNode {
29 return {
30 path,
31 name,
32 kind,
33 children: new Map(),
34 };
35}
36
37function freezeNode(node: MutableFileTreeNode): FileTreeNode {
38 return {
39 path: node.path,
40 name: node.name,
41 kind: node.kind,
42 ...(node.ignored ? { ignored: true } : {}),
43 children: [...node.children.values()].sort(compareNodes).map(freezeNode),
44 };
45}
46
47const fileNameCollator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
48
49function compareNodes(
50 left: Pick<FileTreeNode, "kind" | "name">,
51 right: Pick<FileTreeNode, "kind" | "name">,
52): number {
53 if (left.kind !== right.kind) {
54 return left.kind === "directory" ? -1 : 1;
55 }
56 return fileNameCollator.compare(left.name, right.name);
57}
58
59export function buildFileTree(entries: ReadonlyArray<ProjectEntry>): ReadonlyArray<FileTreeNode> {
60 const root = createMutableNode("", "", "directory");
61
62 for (const entry of entries) {
63 const parts = entry.path.split("/").filter(Boolean);
64 if (parts.length === 0) {
65 continue;
66 }
67
68 let current = root;
69 for (let index = 0; index < parts.length; index += 1) {
70 const part = parts[index];
71 if (!part) {
72 continue;
73 }
74
75 const path = parts.slice(0, index + 1).join("/");
76 const isLeaf = index === parts.length - 1;
77 const kind = isLeaf ? entry.kind : "directory";
78 let child = current.children.get(part);
79 if (!child) {
80 child = createMutableNode(path, part, kind);
81 current.children.set(part, child);
82 } else if (isLeaf) {
83 child.kind = entry.kind;
84 }
85 if (isLeaf && entry.ignored) child.ignored = true;
86 current = child;
87 }
88 }
89
90 return [...root.children.values()].sort(compareNodes).map(freezeNode);
91}
92
93export function defaultExpandedTreePaths(nodes: ReadonlyArray<FileTreeNode>): ReadonlySet<string> {
94 const expanded = new Set<string>();
95 for (const node of nodes) {
96 if (node.kind === "directory") {
97 expanded.add(node.path);
98 }
99 }
100 return expanded;
101}
102
103function flattenNode(
104 output: VisibleFileTreeNode[],
105 node: FileTreeNode,
106 depth: number,
107 expanded: ReadonlySet<string>,
108 searchResultPaths: ReadonlySet<string> | null,
109): boolean {
110 const isSearching = searchResultPaths !== null;
111 const matches = searchResultPaths?.has(node.path) ?? false;
112 let descendantMatches = false;
113 const childOutput: VisibleFileTreeNode[] = [];
114
115 if (node.kind === "directory" && (expanded.has(node.path) || isSearching)) {
116 for (const child of node.children) {
117 if (flattenNode(childOutput, child, depth + 1, expanded, searchResultPaths)) {
118 descendantMatches = true;
119 }
120 }
121 }
122
123 const visible = !isSearching || matches || descendantMatches;
124 if (!visible) {
125 return false;
126 }
127
128 output.push({ node, depth });
129 output.push(...childOutput);
130 return matches || descendantMatches;
131}
132
133export function flattenFileTree(input: {
134 readonly nodes: ReadonlyArray<FileTreeNode>;
135 readonly expanded: ReadonlySet<string>;
136 // Null restores normal directory expansion; an empty set is a search with no results.
137 readonly searchResultPaths?: ReadonlySet<string> | null;
138}): ReadonlyArray<VisibleFileTreeNode> {
139 const output: VisibleFileTreeNode[] = [];
140 for (const node of input.nodes) {
141 flattenNode(output, node, 0, input.expanded, input.searchResultPaths ?? null);
142 }
143 return output;
144}