Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ChangedFilesTree.tsx

331 lines12.6 KB

1import { type RunId } from "@t3tools/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 {
10 ChevronsDownUpIcon,
11 ChevronsUpDownIcon,
12 ChevronRightIcon,
13 FileDiffIcon,
14 FolderIcon,
15 FolderClosedIcon,
16} from "lucide-react";
17import { cn } from "~/lib/utils";
18import { DiffStatLabel, hasNonZeroStat } from "./DiffStatLabel";
19import { PierreEntryIcon } from "./PierreEntryIcon";
20import { Button } from "../ui/button";
21import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
22import {
23 changedFileName,
24 selectChangedFilePreview,
25 summarizeChangedFileScopes,
26} from "./changedFilesPresentation";
27​
28const EMPTY_DIRECTORY_OVERRIDES: Record<string, boolean> = {};
29​
30export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
31 runId: RunId;
32 files: ReadonlyArray<TurnDiffFileChange>;
33 expanded: boolean;
34 showCompactPreview: boolean;
35 allDirectoriesExpanded: boolean;
36 resolvedTheme: "light" | "dark";
37 onExpandedChange: (expanded: boolean) => void;
38 onToggleAllDirectories: () => void;
39 onOpenTurnDiff: (runId: RunId, filePath?: string) => void;
40}) {
41 const {
42 runId,
43 files,
44 expanded,
45 showCompactPreview,
46 allDirectoriesExpanded,
47 resolvedTheme,
48 onExpandedChange,
49 onToggleAllDirectories,
50 onOpenTurnDiff,
51 } = props;
52 const summaryStat = useMemo(() => summarizeTurnDiffStats(files), [files]);
53 const scopeSummary = useMemo(() => summarizeChangedFileScopes(files), [files]);
54 const previewFiles = useMemo(() => selectChangedFilePreview(files), [files]);
55 const compactPreviewVisible = showCompactPreview && !expanded;
56​
57 return (
58 <div
59 className="mt-4 rounded-2xl border border-border/70 bg-secondary p-2 dark:border-transparent dark:bg-input/32"
60 data-changed-files-state={
61 expanded ? "expanded" : compactPreviewVisible ? "preview" : "collapsed"
62 }
63 >
64 <div
65 data-changed-files-header=""
66 className={cn(
67 "flex items-center justify-between gap-2 rounded-xl px-1",
68 expanded &&
69 "sticky top-2 z-10 mb-2 bg-secondary dark:bg-[color-mix(in_srgb,var(--foreground)_2.5%,var(--background))]",
70 )}
71 >
72 <button
73 type="button"
74 aria-expanded={expanded}
75 data-scroll-anchor-ignore
76 className="group flex min-w-0 flex-1 items-center gap-1.5 rounded-lg px-1 py-1.5 text-left transition-colors hover:bg-accent/60 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
77 onClick={() => onExpandedChange(!expanded)}
78 >
79 <ChevronRightIcon
80 aria-hidden="true"
81 className={cn(
82 "size-3.5 shrink-0 text-muted-foreground transition-transform",
83 expanded && "rotate-90",
84 )}
85 />
86 <span className="flex min-w-0 items-center gap-1 whitespace-nowrap font-medium text-foreground text-xs leading-4">
87 <span>
88 {files.length} changed file{files.length === 1 ? "" : "s"}
89 </span>
90 {hasNonZeroStat(summaryStat) && (
91 <DiffStatLabel
92 additions={summaryStat.additions}
93 className="text-xs leading-4"
94 deletions={summaryStat.deletions}
95 layout="inline"
96 />
97 )}
98 </span>
99 <span className="ml-1 hidden truncate text-[11px] text-muted-foreground group-hover:text-foreground/80 sm:inline">
100 {expanded ? "Hide files" : "Show files"}
101 </span>
102 </button>
103 <div className="flex items-center gap-1.5">
104 {expanded ? (
105 <Tooltip>
106 <TooltipTrigger
107 render={
108 <Button
109 type="button"
110 size="icon-xs"
111 variant="outline"
112 className="!size-[22px]"
113 aria-label={
114 allDirectoriesExpanded ? "Collapse all folders" : "Expand all folders"
115 }
116 data-scroll-anchor-ignore
117 onClick={onToggleAllDirectories}
118 />
119 }
120 >
121 {allDirectoriesExpanded ? (
122 <ChevronsDownUpIcon className="size-3" />
123 ) : (
124 <ChevronsUpDownIcon className="size-3" />
125 )}
126 </TooltipTrigger>
127 <TooltipPopup side="top">
128 {allDirectoriesExpanded ? "Collapse all folders" : "Expand all folders"}
129 </TooltipPopup>
130 </Tooltip>
131 ) : null}
132 <Tooltip>
133 <TooltipTrigger
134 render={
135 <Button
136 type="button"
137 size="xs"
138 variant="outline"
139 aria-label="Open diff"
140 onClick={() => onOpenTurnDiff(runId, files[0]?.path)}
141 />
142 }
143 >
144 <FileDiffIcon className="size-3" />
145 <span className="hidden sm:inline">Open diff</span>
146 </TooltipTrigger>
147 <TooltipPopup side="top">Open the full diff</TooltipPopup>
148 </Tooltip>
149 </div>
150 </div>
151 {expanded ? (
152 <ChangedFilesTree
153 key={`changed-files-tree:${runId}`}
154 runId={runId}
155 files={files}
156 allDirectoriesExpanded={allDirectoriesExpanded}
157 resolvedTheme={resolvedTheme}
158 onOpenTurnDiff={onOpenTurnDiff}
159 />
160 ) : compactPreviewVisible ? (
161 <div className="px-2 pb-1.5 pt-1">
162 <p className="flex flex-wrap items-center gap-x-1.5 gap-y-0.5 text-[11px] text-muted-foreground">
163 {scopeSummary.map((scope, index) => (
164 <span key={scope.label} className="inline-flex items-center gap-1">
165 {index > 0 ? <span aria-hidden="true">·</span> : null}
166 <span className="font-mono text-foreground/75">{scope.label}</span>
167 <span>
168 {scope.fileCount} file{scope.fileCount === 1 ? "" : "s"}
169 </span>
170 </span>
171 ))}
172 </p>
173 <div className="mt-2 flex flex-wrap items-center gap-1.5">
174 {previewFiles.map((file) => (
175 <button
176 key={file.path}
177 type="button"
178 title={file.path}
179 className="inline-flex max-w-48 items-center gap-1 rounded-md border border-border/70 bg-background/45 px-1.5 py-1 font-mono text-[10px] text-muted-foreground transition-colors hover:bg-accent/60 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
180 onClick={() => onOpenTurnDiff(runId, file.path)}
181 >
182 <PierreEntryIcon
183 pathValue={file.path}
184 kind="file"
185 theme={resolvedTheme}
186 className="size-3 shrink-0 text-muted-foreground/70"
187 />
188 <span className="truncate">{changedFileName(file.path)}</span>
189 </button>
190 ))}
191 <button
192 type="button"
193 className="rounded-md px-1.5 py-1 text-[11px] font-medium text-muted-foreground transition-colors hover:bg-accent/60 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
194 onClick={() => onExpandedChange(true)}
195 >
196 Show all {files.length} files
197 </button>
198 </div>
199 </div>
200 ) : null}
201 </div>
202 );
203});
204​
205export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
206 runId: RunId;
207 files: ReadonlyArray<TurnDiffFileChange>;
208 allDirectoriesExpanded: boolean;
209 resolvedTheme: "light" | "dark";
210 onOpenTurnDiff: (runId: RunId, filePath?: string) => void;
211}) {
212 const { files, allDirectoriesExpanded, onOpenTurnDiff, resolvedTheme, runId } = props;
213 const treeNodes = useMemo(() => buildTurnDiffTree(files), [files]);
214 const directoryPathsKey = useMemo(
215 () => collectDirectoryPaths(treeNodes).join("\u0000"),
216 [treeNodes],
217 );
218 const hasDirectoryNodes = directoryPathsKey.length > 0;
219 const expansionStateKey = `${allDirectoriesExpanded ? "expanded" : "collapsed"}\u0000${directoryPathsKey}`;
220 const [directoryExpansionState, setDirectoryExpansionState] = useState<{
221 key: string;
222 overrides: Record<string, boolean>;
223 }>(() => ({
224 key: expansionStateKey,
225 overrides: {},
226 }));
227 const expandedDirectories =
228 directoryExpansionState.key === expansionStateKey
229 ? directoryExpansionState.overrides
230 : EMPTY_DIRECTORY_OVERRIDES;
231​
232 const toggleDirectory = useCallback(
233 (pathValue: string) => {
234 setDirectoryExpansionState((current) => {
235 const nextOverrides = current.key === expansionStateKey ? current.overrides : {};
236 return {
237 key: expansionStateKey,
238 overrides: {
239 ...nextOverrides,
240 [pathValue]: !(nextOverrides[pathValue] ?? allDirectoriesExpanded),
241 },
242 };
243 });
244 },
245 [allDirectoriesExpanded, expansionStateKey],
246 );
247​
248 const renderTreeNode = (node: TurnDiffTreeNode, depth: number) => {
249 const leftPadding = 8 + depth * 14;
250 if (node.kind === "directory") {
251 const isExpanded = expandedDirectories[node.path] ?? allDirectoriesExpanded;
252 return (
253 <div key={`dir:${node.path}`}>
254 <button
255 type="button"
256 data-scroll-anchor-ignore
257 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"
258 style={{ paddingLeft: `${leftPadding}px` }}
259 onClick={() => toggleDirectory(node.path)}
260 >
261 <ChevronRightIcon
262 aria-hidden="true"
263 className={cn(
264 "size-3.5 shrink-0 text-muted-foreground/70 transition-transform group-hover:text-foreground/80",
265 isExpanded && "rotate-90",
266 )}
267 />
268 {isExpanded ? (
269 <FolderIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
270 ) : (
271 <FolderClosedIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
272 )}
273 <span className="truncate font-mono text-[11px] text-muted-foreground/90 group-hover:text-foreground/90">
274 {node.name}
275 </span>
276 {hasNonZeroStat(node.stat) && (
277 <span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
278 <DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
279 </span>
280 )}
281 </button>
282 {isExpanded && (
283 <div className="space-y-0.5">
284 {node.children.map((childNode) => renderTreeNode(childNode, depth + 1))}
285 </div>
286 )}
287 </div>
288 );
289 }
290​
291 return (
292 <button
293 key={`file:${node.path}`}
294 type="button"
295 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"
296 style={{ paddingLeft: `${leftPadding}px` }}
297 onClick={() => onOpenTurnDiff(runId, node.path)}
298 >
299 {hasDirectoryNodes || depth > 0 ? (
300 <span aria-hidden="true" className="size-3.5 shrink-0" />
301 ) : null}
302 <PierreEntryIcon
303 pathValue={node.path}
304 kind="file"
305 theme={resolvedTheme}
306 className="size-3.5 text-muted-foreground/70"
307 />
308 <span className="truncate font-mono text-[11px] text-muted-foreground/80 group-hover:text-foreground/90">
309 {node.name}
310 </span>
311 {node.stat && (
312 <span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
313 <DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
314 </span>
315 )}
316 </button>
317 );
318 };
319​
320 return <div className="space-y-0.5">{treeNodes.map((node) => renderTreeNode(node, 0))}</div>;
321});
322​
323function collectDirectoryPaths(nodes: ReadonlyArray<TurnDiffTreeNode>): string[] {
324 const paths: string[] = [];
325 for (const node of nodes) {
326 if (node.kind !== "directory") continue;
327 paths.push(node.path);
328 paths.push(...collectDirectoryPaths(node.children));
329 }
330 return paths;