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