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 {
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";
22
23const EMPTY_DIRECTORY_OVERRIDES: Record<string, boolean> = {};
24
25export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
26 turnId: TurnId;
27 files: ReadonlyArray<TurnDiffFileChange>;
28 allDirectoriesExpanded: boolean;
29 resolvedTheme: "light" | "dark";
30 onToggleAllDirectories: () => void;
31 onOpenTurnDiff: (turnId: TurnId, filePath?: string) => void;
32}) {
33 const {
34 turnId,
35 files,
36 allDirectoriesExpanded,
37 resolvedTheme,
38 onToggleAllDirectories,
39 onOpenTurnDiff,
40 } = props;
41 const summaryStat = useMemo(() => summarizeTurnDiffStats(files), [files]);
42
43 return (
44 <div className="mt-4 rounded-2xl border border-input bg-background p-2 pt-4 shadow-xs/5 not-dark:bg-clip-padding dark:bg-input/32">
45 <div className="sticky top-0 z-10 mb-3 flex items-center justify-between gap-2 bg-background px-2 before:absolute before:inset-x-0 before:-top-4 before:h-4 before:bg-background before:content-[''] dark:bg-[color-mix(in_srgb,var(--foreground)_2.5%,var(--background))] dark:before:bg-[color-mix(in_srgb,var(--foreground)_2.5%,var(--background))]">
46 <p className="flex items-center gap-1 whitespace-nowrap font-medium text-foreground text-xs leading-4">
47 <span>
48 {files.length} changed file{files.length === 1 ? "" : "s"}
49 </span>
50 {hasNonZeroStat(summaryStat) && (
51 <DiffStatLabel
52 additions={summaryStat.additions}
53 className="text-xs leading-4"
54 deletions={summaryStat.deletions}
55 layout="inline"
56 />
57 )}
58 </p>
59 <div className="flex items-center gap-1.5">
60 <Tooltip>
61 <TooltipTrigger
62 render={
63 <Button
64 type="button"
65 size="icon-xs"
66 variant="outline"
67 className="!size-[22px]"
68 aria-label={allDirectoriesExpanded ? "Collapse all" : "Expand all"}
69 data-scroll-anchor-ignore
70 onClick={onToggleAllDirectories}
71 />
72 }
73 >
74 {allDirectoriesExpanded ? (
75 <ChevronsDownUpIcon className="size-3" />
76 ) : (
77 <ChevronsUpDownIcon className="size-3" />
78 )}
79 </TooltipTrigger>
80 <TooltipPopup side="top">
81 {allDirectoriesExpanded ? "Collapse all" : "Expand all"}
82 </TooltipPopup>
83 </Tooltip>
84 <Tooltip>
85 <TooltipTrigger
86 render={
87 <Button
88 type="button"
89 size="icon-xs"
90 variant="outline"
91 className="!size-[22px]"
92 aria-label="View diff"
93 onClick={() => onOpenTurnDiff(turnId, files[0]?.path)}
94 />
95 }
96 >
97 <FileDiffIcon className="size-3" />
98 </TooltipTrigger>
99 <TooltipPopup side="top">View diff</TooltipPopup>
100 </Tooltip>
101 </div>
102 </div>
103 <ChangedFilesTree
104 key={`changed-files-tree:${turnId}`}
105 turnId={turnId}
106 files={files}
107 allDirectoriesExpanded={allDirectoriesExpanded}
108 resolvedTheme={resolvedTheme}
109 onOpenTurnDiff={onOpenTurnDiff}
110 />
111 </div>
112 );
113});
114
115export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
116 turnId: TurnId;
117 files: ReadonlyArray<TurnDiffFileChange>;
118 allDirectoriesExpanded: boolean;
119 resolvedTheme: "light" | "dark";
120 onOpenTurnDiff: (turnId: TurnId, filePath?: string) => void;
121}) {
122 const { files, allDirectoriesExpanded, onOpenTurnDiff, resolvedTheme, turnId } = props;
123 const treeNodes = useMemo(() => buildTurnDiffTree(files), [files]);
124 const directoryPathsKey = useMemo(
125 () => collectDirectoryPaths(treeNodes).join("\u0000"),
126 [treeNodes],
127 );
128 const hasDirectoryNodes = directoryPathsKey.length > 0;
129 const expansionStateKey = `${allDirectoriesExpanded ? "expanded" : "collapsed"}\u0000${directoryPathsKey}`;
130 const [directoryExpansionState, setDirectoryExpansionState] = useState<{
131 key: string;
132 overrides: Record<string, boolean>;
133 }>(() => ({
134 key: expansionStateKey,
135 overrides: {},
136 }));
137 const expandedDirectories =
138 directoryExpansionState.key === expansionStateKey
139 ? directoryExpansionState.overrides
140 : EMPTY_DIRECTORY_OVERRIDES;
141
142 const toggleDirectory = useCallback(
143 (pathValue: string) => {
144 setDirectoryExpansionState((current) => {
145 const nextOverrides = current.key === expansionStateKey ? current.overrides : {};
146 return {
147 key: expansionStateKey,
148 overrides: {
149 ...nextOverrides,
150 [pathValue]: !(nextOverrides[pathValue] ?? allDirectoriesExpanded),
151 },
152 };
153 });
154 },
155 [allDirectoriesExpanded, expansionStateKey],
156 );
157
158 const renderTreeNode = (node: TurnDiffTreeNode, depth: number) => {
159 const leftPadding = 8 + depth * 14;
160 if (node.kind === "directory") {
161 const isExpanded = expandedDirectories[node.path] ?? allDirectoriesExpanded;
162 return (
163 <div key={`dir:${node.path}`}>
164 <button
165 type="button"
166 data-scroll-anchor-ignore
167 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"
168 style={{ paddingLeft: `${leftPadding}px` }}
169 onClick={() => toggleDirectory(node.path)}
170 >
171 <ChevronRightIcon
172 aria-hidden="true"
173 className={cn(
174 "size-3.5 shrink-0 text-muted-foreground/70 transition-transform group-hover:text-foreground/80",
175 isExpanded && "rotate-90",
176 )}
177 />
178 {isExpanded ? (
179 <FolderIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
180 ) : (
181 <FolderClosedIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
182 )}
183 <span className="truncate font-mono text-[11px] text-muted-foreground/90 group-hover:text-foreground/90">
184 {node.name}
185 </span>
186 {hasNonZeroStat(node.stat) && (
187 <span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
188 <DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
189 </span>
190 )}
191 </button>
192 {isExpanded && (
193 <div className="space-y-0.5">
194 {node.children.map((childNode) => renderTreeNode(childNode, depth + 1))}
195 </div>
196 )}
197 </div>
198 );
199 }
200
201 return (
202 <button
203 key={`file:${node.path}`}
204 type="button"
205 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"
206 style={{ paddingLeft: `${leftPadding}px` }}
207 onClick={() => onOpenTurnDiff(turnId, node.path)}
208 >
209 {hasDirectoryNodes || depth > 0 ? (
210 <span aria-hidden="true" className="size-3.5 shrink-0" />
211 ) : null}
212 <PierreEntryIcon
213 pathValue={node.path}
214 kind="file"
215 theme={resolvedTheme}
216 className="size-3.5 text-muted-foreground/70"
217 />
218 <span className="truncate font-mono text-[11px] text-muted-foreground/80 group-hover:text-foreground/90">
219 {node.name}
220 </span>
221 {node.stat && (
222 <span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
223 <DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
224 </span>
225 )}
226 </button>
227 );
228 };
229
230 return <div className="space-y-0.5">{treeNodes.map((node) => renderTreeNode(node, 0))}</div>;
231});
232
233function collectDirectoryPaths(nodes: ReadonlyArray<TurnDiffTreeNode>): string[] {
234 const paths: string[] = [];
235 for (const node of nodes) {
236 if (node.kind !== "directory") continue;
237 paths.push(node.path);
238 paths.push(...collectDirectoryPaths(node.children));
239 }
240 return paths;
241}