Open History to see the last commit
1import { type TurnId } from "@v12/contracts";
2import { memo, useCallback, useMemo, useState } from "react";
3import { type TurnDiffFileChange } from "../../types";
4import {
5 buildTurnDiffTree,
6 summarizeTurnDiffStats,
7 type TurnDiffTreeNode,
8} from "../../lib/turnDiffTree";
9import { ChevronRightIcon, FolderIcon, FolderClosedIcon } from "lucide-react";
10import { cn } from "~/lib/utils";
11import { DiffStatLabel, hasNonZeroStat } from "./DiffStatLabel";
12import { PierreEntryIcon } from "./PierreEntryIcon";
13import { Button } from "../ui/button";
14import { Collapsible, CollapsiblePanel } from "../ui/collapsible";
15
16const EMPTY_DIRECTORY_OVERRIDES: Record<string, boolean> = {};
17
18export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
19 turnId: TurnId;
20 files: ReadonlyArray<TurnDiffFileChange>;
21 allDirectoriesExpanded: boolean;
22 resolvedTheme: "light" | "dark";
23 onToggleAllDirectories: () => void;
24 onOpenTurnDiff: (turnId: TurnId, filePath?: string) => void;
25}) {
26 const {
27 turnId,
28 files,
29 allDirectoriesExpanded,
30 resolvedTheme,
31 onToggleAllDirectories,
32 onOpenTurnDiff,
33 } = props;
34 const summaryStat = useMemo(() => summarizeTurnDiffStats(files), [files]);
35
36 return (
37 <div className="relative mt-4 rounded-2xl bg-card/40 shadow-xs/5 not-dark:bg-clip-padding after:pointer-events-none after:absolute after:inset-0 after:z-20 after:rounded-2xl after:border after:border-input">
38 <div className="sticky top-0 z-10 mb-3 flex items-center justify-between gap-2 rounded-t-2xl bg-card/72 p-3 backdrop-blur-md">
39 <p className="flex items-center gap-1 font-medium text-foreground text-xs leading-4">
40 <span>{files.length} changed files</span>
41 {hasNonZeroStat(summaryStat) && (
42 <DiffStatLabel
43 additions={summaryStat.additions}
44 className="text-xs leading-4"
45 deletions={summaryStat.deletions}
46 layout="inline"
47 />
48 )}
49 </p>
50 <div className="flex items-center gap-1.5">
51 <Button
52 type="button"
53 size="xs"
54 variant="outline"
55 data-scroll-anchor-ignore
56 onClick={onToggleAllDirectories}
57 >
58 {allDirectoriesExpanded ? "Collapse all" : "Expand all"}
59 </Button>
60 <Button
61 type="button"
62 size="xs"
63 variant="outline"
64 onClick={() => onOpenTurnDiff(turnId, files[0]?.path)}
65 >
66 View diff
67 </Button>
68 </div>
69 </div>
70 <div className="px-2 pb-2">
71 <ChangedFilesTree
72 key={`changed-files-tree:${turnId}`}
73 turnId={turnId}
74 files={files}
75 allDirectoriesExpanded={allDirectoriesExpanded}
76 resolvedTheme={resolvedTheme}
77 onOpenTurnDiff={onOpenTurnDiff}
78 />
79 </div>
80 </div>
81 );
82});
83
84export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
85 turnId: TurnId;
86 files: ReadonlyArray<TurnDiffFileChange>;
87 allDirectoriesExpanded: boolean;
88 resolvedTheme: "light" | "dark";
89 onOpenTurnDiff: (turnId: TurnId, filePath?: string) => void;
90}) {
91 const { files, allDirectoriesExpanded, onOpenTurnDiff, resolvedTheme, turnId } = props;
92 const treeNodes = useMemo(() => buildTurnDiffTree(files), [files]);
93 const directoryPathsKey = useMemo(
94 () => collectDirectoryPaths(treeNodes).join("\u0000"),
95 [treeNodes],
96 );
97 const hasDirectoryNodes = directoryPathsKey.length > 0;
98 const expansionStateKey = `${allDirectoriesExpanded ? "expanded" : "collapsed"}\u0000${directoryPathsKey}`;
99 const [directoryExpansionState, setDirectoryExpansionState] = useState<{
100 key: string;
101 overrides: Record<string, boolean>;
102 }>(() => ({
103 key: expansionStateKey,
104 overrides: {},
105 }));
106 const expandedDirectories =
107 directoryExpansionState.key === expansionStateKey
108 ? directoryExpansionState.overrides
109 : EMPTY_DIRECTORY_OVERRIDES;
110
111 const toggleDirectory = useCallback(
112 (pathValue: string) => {
113 setDirectoryExpansionState((current) => {
114 const nextOverrides = current.key === expansionStateKey ? current.overrides : {};
115 return {
116 key: expansionStateKey,
117 overrides: {
118 ...nextOverrides,
119 [pathValue]: !(nextOverrides[pathValue] ?? allDirectoriesExpanded),
120 },
121 };
122 });
123 },
124 [allDirectoriesExpanded, expansionStateKey],
125 );
126
127 const renderTreeNode = (node: TurnDiffTreeNode, depth: number) => {
128 const leftPadding = 8 + depth * 14;
129 if (node.kind === "directory") {
130 const isExpanded = expandedDirectories[node.path] ?? allDirectoriesExpanded;
131 return (
132 <Collapsible key={`dir:${node.path}`} open={isExpanded}>
133 <button
134 type="button"
135 data-scroll-anchor-ignore
136 aria-expanded={isExpanded}
137 className="group flex w-full items-center gap-1.5 rounded-xl py-1 pr-3 text-left transition-colors hover:bg-accent/60 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background"
138 style={{ paddingLeft: `${leftPadding}px` }}
139 onClick={() => toggleDirectory(node.path)}
140 >
141 <ChevronRightIcon
142 aria-hidden="true"
143 className={cn(
144 "size-3.5 shrink-0 text-muted-foreground/70 transition-transform duration-200 group-hover:text-foreground/80 motion-reduce:transition-none",
145 isExpanded && "rotate-90",
146 )}
147 />
148 {isExpanded ? (
149 <FolderIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
150 ) : (
151 <FolderClosedIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
152 )}
153 <span className="truncate font-mono text-[11px] text-muted-foreground/90 group-hover:text-foreground/90">
154 {node.name}
155 </span>
156 {hasNonZeroStat(node.stat) && (
157 <span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
158 <DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
159 </span>
160 )}
161 </button>
162 <CollapsiblePanel className="space-y-0.5 ease-out motion-reduce:transition-none">
163 {node.children.map((childNode) => renderTreeNode(childNode, depth + 1))}
164 </CollapsiblePanel>
165 </Collapsible>
166 );
167 }
168
169 return (
170 <button
171 key={`file:${node.path}`}
172 type="button"
173 className="group flex w-full items-center gap-1.5 rounded-xl py-1 pr-3 text-left transition-colors hover:bg-accent/60 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background"
174 style={{ paddingLeft: `${leftPadding}px` }}
175 onClick={() => onOpenTurnDiff(turnId, node.path)}
176 >
177 {hasDirectoryNodes || depth > 0 ? (
178 <span aria-hidden="true" className="size-3.5 shrink-0" />
179 ) : null}
180 <PierreEntryIcon
181 pathValue={node.path}
182 kind="file"
183 theme={resolvedTheme}
184 className="size-3.5 text-muted-foreground/70"
185 />
186 <span className="truncate font-mono text-[11px] text-muted-foreground/80 group-hover:text-foreground/90">
187 {node.name}
188 </span>
189 {node.stat && (
190 <span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
191 <DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
192 </span>
193 )}
194 </button>
195 );
196 };
197
198 return <div className="space-y-0.5">{treeNodes.map((node) => renderTreeNode(node, 0))}</div>;
199});
200
201function collectDirectoryPaths(nodes: ReadonlyArray<TurnDiffTreeNode>): string[] {
202 const paths: string[] = [];
203 for (const node of nodes) {
204 if (node.kind !== "directory") continue;
205 paths.push(node.path);
206 paths.push(...collectDirectoryPaths(node.children));
207 }
208 return paths;
209}