Open History to see the last commit
1import { type TurnId } from "@t3tools/contracts";
2import { memo, useCallback, useMemo, useState } from "react";
3import { type TurnDiffFileChange } from "../../types";
4import { buildTurnDiffTree, type TurnDiffTreeNode } from "../../lib/turnDiffTree";
5import { ChevronRightIcon, FolderIcon, FolderClosedIcon } from "lucide-react";
6import { cn } from "~/lib/utils";
7import { DiffStatLabel, hasNonZeroStat } from "./DiffStatLabel";
8import { VscodeEntryIcon } from "./VscodeEntryIcon";
9
10const EMPTY_DIRECTORY_OVERRIDES: Record<string, boolean> = {};
11
12export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
13 turnId: TurnId;
14 files: ReadonlyArray<TurnDiffFileChange>;
15 allDirectoriesExpanded: boolean;
16 resolvedTheme: "light" | "dark";
17 onOpenTurnDiff: (turnId: TurnId, filePath?: string) => void;
18}) {
19 const { files, allDirectoriesExpanded, onOpenTurnDiff, resolvedTheme, turnId } = props;
20 const treeNodes = useMemo(() => buildTurnDiffTree(files), [files]);
21 const directoryPathsKey = useMemo(
22 () => collectDirectoryPaths(treeNodes).join("\u0000"),
23 [treeNodes],
24 );
25 const expansionStateKey = `${allDirectoriesExpanded ? "expanded" : "collapsed"}\u0000${directoryPathsKey}`;
26 const [directoryExpansionState, setDirectoryExpansionState] = useState<{
27 key: string;
28 overrides: Record<string, boolean>;
29 }>(() => ({
30 key: expansionStateKey,
31 overrides: {},
32 }));
33 const expandedDirectories =
34 directoryExpansionState.key === expansionStateKey
35 ? directoryExpansionState.overrides
36 : EMPTY_DIRECTORY_OVERRIDES;
37
38 const toggleDirectory = useCallback(
39 (pathValue: string) => {
40 setDirectoryExpansionState((current) => {
41 const nextOverrides = current.key === expansionStateKey ? current.overrides : {};
42 return {
43 key: expansionStateKey,
44 overrides: {
45 ...nextOverrides,
46 [pathValue]: !(nextOverrides[pathValue] ?? allDirectoriesExpanded),
47 },
48 };
49 });
50 },
51 [allDirectoriesExpanded, expansionStateKey],
52 );
53
54 const renderTreeNode = (node: TurnDiffTreeNode, depth: number) => {
55 const leftPadding = 8 + depth * 14;
56 if (node.kind === "directory") {
57 const isExpanded = expandedDirectories[node.path] ?? allDirectoriesExpanded;
58 return (
59 <div key={`dir:${node.path}`}>
60 <button
61 type="button"
62 data-scroll-anchor-ignore
63 className="group flex w-full items-center gap-1.5 rounded-md py-1 pr-2 text-left hover:bg-background/80"
64 style={{ paddingLeft: `${leftPadding}px` }}
65 onClick={() => toggleDirectory(node.path)}
66 >
67 <ChevronRightIcon
68 aria-hidden="true"
69 className={cn(
70 "size-3.5 shrink-0 text-muted-foreground/70 transition-transform group-hover:text-foreground/80",
71 isExpanded && "rotate-90",
72 )}
73 />
74 {isExpanded ? (
75 <FolderIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
76 ) : (
77 <FolderClosedIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
78 )}
79 <span className="truncate font-mono text-[11px] text-muted-foreground/90 group-hover:text-foreground/90">
80 {node.name}
81 </span>
82 {hasNonZeroStat(node.stat) && (
83 <span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
84 <DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
85 </span>
86 )}
87 </button>
88 {isExpanded && (
89 <div className="space-y-0.5">
90 {node.children.map((childNode) => renderTreeNode(childNode, depth + 1))}
91 </div>
92 )}
93 </div>
94 );
95 }
96
97 return (
98 <button
99 key={`file:${node.path}`}
100 type="button"
101 className="group flex w-full items-center gap-1.5 rounded-md py-1 pr-2 text-left hover:bg-background/80"
102 style={{ paddingLeft: `${leftPadding}px` }}
103 onClick={() => onOpenTurnDiff(turnId, node.path)}
104 >
105 <span aria-hidden="true" className="size-3.5 shrink-0" />
106 <VscodeEntryIcon
107 pathValue={node.path}
108 kind="file"
109 theme={resolvedTheme}
110 className="size-3.5 text-muted-foreground/70"
111 />
112 <span className="truncate font-mono text-[11px] text-muted-foreground/80 group-hover:text-foreground/90">
113 {node.name}
114 </span>
115 {node.stat && (
116 <span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
117 <DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
118 </span>
119 )}
120 </button>
121 );
122 };
123
124 return <div className="space-y-0.5">{treeNodes.map((node) => renderTreeNode(node, 0))}</div>;
125});
126
127function collectDirectoryPaths(nodes: ReadonlyArray<TurnDiffTreeNode>): string[] {
128 const paths: string[] = [];
129 for (const node of nodes) {
130 if (node.kind !== "directory") continue;
131 paths.push(node.path);
132 paths.push(...collectDirectoryPaths(node.children));
133 }
134 return paths;
135}