Skip to content

Commit 3bdeb0a

Browse files
mohit-daymaclaude
andcommitted
feat(web): commit dialog can show changed files as a folder tree
The commit dialog lists changed files as a flat list of full paths, which gets long and repetitive in large changes. Add a list/tree toggle (list by default, remembered locally). Tree view groups files by folder with aggregated +/- counts and collapsible folders; in Edit mode a folder checkbox includes or excludes every file below it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cefb544 commit 3bdeb0a

5 files changed

Lines changed: 340 additions & 61 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { buildTurnDiffTree } from "~/lib/turnDiffTree";
4+
import {
5+
flattenCommitFileTree,
6+
resolveCommitFileTreeSelection,
7+
toggleCommitFileTreeNode,
8+
} from "./CommitFileTree.logic";
9+
10+
const tree = buildTurnDiffTree([
11+
{ path: "src/app/page.tsx", additions: 1, deletions: 0 },
12+
{ path: "src/app/layout.tsx", additions: 2, deletions: 1 },
13+
{ path: "src/lib/util.ts", additions: 3, deletions: 0 },
14+
{ path: "README.md", additions: 1, deletions: 1 },
15+
]);
16+
const src = tree[0]!;
17+
18+
describe("commit file tree", () => {
19+
it("hides the children of collapsed folders", () => {
20+
const rows = (collapsed: string[]) =>
21+
flattenCommitFileTree(tree, new Set(collapsed)).map(
22+
(row) => `${" ".repeat(row.depth)}${row.node.name}`,
23+
);
24+
25+
expect(rows([])).toEqual([
26+
"src",
27+
" app",
28+
" layout.tsx",
29+
" page.tsx",
30+
" lib",
31+
" util.ts",
32+
"README.md",
33+
]);
34+
expect(rows(["src/app"])).toEqual(["src", " app", " lib", " util.ts", "README.md"]);
35+
});
36+
37+
it("toggles every file below a folder and reports partial selection", () => {
38+
expect(resolveCommitFileTreeSelection(src, new Set())).toBe("all");
39+
expect(resolveCommitFileTreeSelection(src, new Set(["src/lib/util.ts"]))).toBe("some");
40+
41+
const excluded = toggleCommitFileTreeNode(src, new Set(["README.md"]));
42+
expect([...excluded].toSorted()).toEqual([
43+
"README.md",
44+
"src/app/layout.tsx",
45+
"src/app/page.tsx",
46+
"src/lib/util.ts",
47+
]);
48+
expect(resolveCommitFileTreeSelection(src, excluded)).toBe("none");
49+
50+
const partial = new Set(["src/lib/util.ts", "README.md"]);
51+
expect([...toggleCommitFileTreeNode(src, partial)]).toEqual(["README.md"]);
52+
});
53+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { type TurnDiffTreeNode } from "~/lib/turnDiffTree";
2+
3+
export interface CommitFileTreeRow {
4+
node: TurnDiffTreeNode;
5+
depth: number;
6+
}
7+
8+
export type CommitFileTreeSelection = "all" | "some" | "none";
9+
10+
export function flattenCommitFileTree(
11+
nodes: ReadonlyArray<TurnDiffTreeNode>,
12+
collapsedDirectories: ReadonlySet<string>,
13+
depth = 0,
14+
rows: CommitFileTreeRow[] = [],
15+
): CommitFileTreeRow[] {
16+
for (const node of nodes) {
17+
rows.push({ node, depth });
18+
if (node.kind === "directory" && !collapsedDirectories.has(node.path)) {
19+
flattenCommitFileTree(node.children, collapsedDirectories, depth + 1, rows);
20+
}
21+
}
22+
return rows;
23+
}
24+
25+
function collectFilePaths(node: TurnDiffTreeNode, paths: string[] = []): string[] {
26+
if (node.kind === "file") {
27+
paths.push(node.path);
28+
return paths;
29+
}
30+
for (const child of node.children) collectFilePaths(child, paths);
31+
return paths;
32+
}
33+
34+
export function resolveCommitFileTreeSelection(
35+
node: TurnDiffTreeNode,
36+
excludedFiles: ReadonlySet<string>,
37+
): CommitFileTreeSelection {
38+
const paths = collectFilePaths(node);
39+
const excludedCount = paths.filter((path) => excludedFiles.has(path)).length;
40+
if (excludedCount === 0) return "all";
41+
return excludedCount === paths.length ? "none" : "some";
42+
}
43+
44+
/** A fully included node is excluded; a partly or fully excluded node is included. */
45+
export function toggleCommitFileTreeNode(
46+
node: TurnDiffTreeNode,
47+
excludedFiles: ReadonlySet<string>,
48+
): Set<string> {
49+
const next = new Set(excludedFiles);
50+
const excludeAll = resolveCommitFileTreeSelection(node, excludedFiles) === "all";
51+
for (const path of collectFilePaths(node)) {
52+
if (excludeAll) next.add(path);
53+
else next.delete(path);
54+
}
55+
return next;
56+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { ChevronRightIcon, FolderIcon, FolderOpenIcon } from "lucide-react";
2+
import { useState } from "react";
3+
import { Checkbox } from "~/components/ui/checkbox";
4+
import { buildTurnDiffTree } from "~/lib/turnDiffTree";
5+
import { cn } from "~/lib/utils";
6+
import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
7+
import {
8+
flattenCommitFileTree,
9+
resolveCommitFileTreeSelection,
10+
toggleCommitFileTreeNode,
11+
} from "./CommitFileTree.logic";
12+
13+
interface CommitFile {
14+
path: string;
15+
insertions: number;
16+
deletions: number;
17+
}
18+
19+
export function CommitFileTree(props: {
20+
files: ReadonlyArray<CommitFile>;
21+
excludedFiles: ReadonlySet<string>;
22+
isEditing: boolean;
23+
canOpenFile: boolean;
24+
onExcludedFilesChange: (excludedFiles: Set<string>) => void;
25+
onOpenFile: (path: string) => void;
26+
}) {
27+
const { files, excludedFiles, isEditing, canOpenFile, onExcludedFilesChange, onOpenFile } = props;
28+
const [collapsedDirectories, setCollapsedDirectories] = useState<ReadonlySet<string>>(
29+
() => new Set(),
30+
);
31+
const tree = buildTurnDiffTree(
32+
files.map((file) => ({
33+
path: file.path,
34+
additions: file.insertions,
35+
deletions: file.deletions,
36+
})),
37+
);
38+
const rows = flattenCommitFileTree(tree, collapsedDirectories);
39+
40+
const toggleDirectory = (path: string) => {
41+
setCollapsedDirectories((current) => {
42+
const next = new Set(current);
43+
if (!next.delete(path)) next.add(path);
44+
return next;
45+
});
46+
};
47+
48+
return rows.map(({ node, depth }) => {
49+
const selection = resolveCommitFileTreeSelection(node, excludedFiles);
50+
const isExcluded = selection === "none";
51+
const isDirectory = node.kind === "directory";
52+
const isExpanded = isDirectory && !collapsedDirectories.has(node.path);
53+
const label = (
54+
<>
55+
<span className={cn("min-w-0 flex-1 truncate", isExcluded && "text-muted-foreground")}>
56+
{node.name}
57+
</span>
58+
<span className="shrink-0">
59+
{isExcluded ? (
60+
<span className="text-muted-foreground">Excluded</span>
61+
) : node.stat ? (
62+
<>
63+
<span className="text-diff-addition">+{formatDiffCount(node.stat.additions)}</span>
64+
<span className="text-muted-foreground"> / </span>
65+
<span className="text-diff-deletion">-{formatDiffCount(node.stat.deletions)}</span>
66+
</>
67+
) : null}
68+
</span>
69+
</>
70+
);
71+
72+
return (
73+
<div
74+
key={`${node.kind}:${node.path}`}
75+
className="flex w-full items-center gap-2 rounded-md py-1 pr-2 font-mono hover:bg-accent/50"
76+
style={{ paddingLeft: `${8 + depth * 14}px` }}
77+
>
78+
{isEditing && (
79+
<Checkbox
80+
aria-label={`Include ${node.path}`}
81+
checked={selection === "all"}
82+
indeterminate={selection === "some"}
83+
onCheckedChange={() =>
84+
onExcludedFilesChange(toggleCommitFileTreeNode(node, excludedFiles))
85+
}
86+
/>
87+
)}
88+
{isDirectory ? (
89+
<button
90+
type="button"
91+
className="flex min-w-0 flex-1 cursor-pointer items-center gap-1.5 text-left"
92+
aria-expanded={isExpanded}
93+
onClick={() => toggleDirectory(node.path)}
94+
>
95+
<ChevronRightIcon
96+
aria-hidden="true"
97+
className={cn("size-3.5 shrink-0 text-muted-foreground", isExpanded && "rotate-90")}
98+
/>
99+
{isExpanded ? (
100+
<FolderOpenIcon
101+
aria-hidden="true"
102+
className="size-3.5 shrink-0 text-muted-foreground"
103+
/>
104+
) : (
105+
<FolderIcon aria-hidden="true" className="size-3.5 shrink-0 text-muted-foreground" />
106+
)}
107+
{label}
108+
</button>
109+
) : (
110+
<button
111+
type="button"
112+
className="flex min-w-0 flex-1 items-center gap-1.5 text-left enabled:cursor-pointer"
113+
disabled={!canOpenFile}
114+
aria-label={`Open ${node.path} in editor`}
115+
onClick={() => onOpenFile(node.path)}
116+
>
117+
<span aria-hidden="true" className="size-3.5 shrink-0" />
118+
{label}
119+
</button>
120+
)}
121+
</div>
122+
);
123+
});
124+
}

0 commit comments

Comments
 (0)