MCPcopy
hub / github.com/coder/mux / flattenWorkspaceTree

Function flattenWorkspaceTree

src/browser/utils/ui/workspaceFiltering.ts:10–78  ·  view source on GitHub ↗
(
  workspaces: FrontendWorkspaceMetadata[]
)

Source from the content-addressed store, hash-verified

8}
9
10function flattenWorkspaceTree(
11 workspaces: FrontendWorkspaceMetadata[]
12): FrontendWorkspaceMetadata[] {
13 if (workspaces.length === 0) return [];
14
15 const byId = new Map<string, FrontendWorkspaceMetadata>();
16 for (const workspace of workspaces) {
17 byId.set(workspace.id, workspace);
18 }
19
20 const childrenByParent = new Map<string, FrontendWorkspaceMetadata[]>();
21 const roots: FrontendWorkspaceMetadata[] = [];
22
23 // Preserve input order for both roots and siblings by iterating in-order.
24 // Active sub-workspaces only render when their full parent chain is active.
25 for (const workspace of workspaces) {
26 const parentId = workspace.parentWorkspaceId;
27 if (parentId == null) {
28 roots.push(workspace);
29 continue;
30 }
31
32 if (!byId.has(parentId)) {
33 continue;
34 }
35
36 const children = childrenByParent.get(parentId) ?? [];
37 children.push(workspace);
38 childrenByParent.set(parentId, children);
39 }
40
41 const result: FrontendWorkspaceMetadata[] = [];
42 const visited = new Set<string>();
43 const stack = roots.slice().reverse();
44
45 while (stack.length > 0) {
46 const workspace = stack.pop();
47 assert(workspace != null, "flattenWorkspaceTree: stack entries must exist while traversing");
48
49 if (visited.has(workspace.id)) {
50 continue;
51 }
52 visited.add(workspace.id);
53 result.push(workspace);
54
55 const children = childrenByParent.get(workspace.id);
56 if (!children) {
57 continue;
58 }
59
60 for (let index = children.length - 1; index >= 0; index -= 1) {
61 stack.push(children[index]);
62 }
63 }
64
65 for (const workspace of workspaces) {
66 if (visited.has(workspace.id)) {
67 continue;

Callers 1

Calls 6

assertFunction · 0.90
setMethod · 0.80
addMethod · 0.80
pushMethod · 0.65
getMethod · 0.65
hasMethod · 0.45

Tested by

no test coverage detected