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 className="group flex w-full items-center gap-1.5 rounded-md py-1 pr-2 text-left hover:bg-background/80"
54 style={{ paddingLeft: `${leftPadding}px` }}
55 onClick={() => toggleDirectory(node.path, depth === 0)}
56 >
57 <ChevronRightIcon
58 aria-hidden="true"
59 className={cn(
60 "size-3.5 shrink-0 text-muted-foreground/70 transition-transform group-hover:text-foreground/80",
61 isExpanded && "rotate-90",
62 )}
63 />
64 {isExpanded ? (
65 <FolderIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
66 ) : (
67 <FolderClosedIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
68 )}
69 <span className="truncate font-mono text-[11px] text-muted-foreground/90 group-hover:text-foreground/90">
70 {node.name}
71 </span>
72 {hasNonZeroStat(node.stat) && (
73 <span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
74 <DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
75 </span>
76 )}
77 </button>
78 {isExpanded && (
79 <div className="space-y-0.5">
80 {node.children.map((childNode) => renderTreeNode(childNode, depth + 1))}
81 </div>
82 )}
83 </div>
84 );
85 }
86
87 return (
88 <button
89 key={`file:${node.path}`}
90 type="button"
91 className="group flex w-full items-center gap-1.5 rounded-md py-1 pr-2 text-left hover:bg-background/80"
92 style={{ paddingLeft: `${leftPadding}px` }}
93 onClick={() => onOpenTurnDiff(turnId, node.path)}
94 >
95 <span aria-hidden="true" className="size-3.5 shrink-0" />
96 <VscodeEntryIcon
97 pathValue={node.path}
98 kind="file"
99 theme={resolvedTheme}
100 className="size-3.5 text-muted-foreground/70"
101 />
102 <span className="truncate font-mono text-[11px] text-muted-foreground/80 group-hover:text-foreground/90">
103 {node.name}
104 </span>
105 {node.stat && (
106 <span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
107 <DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
108 </span>
109 )}
110 </button>
111 );
112 };
113
114 return <div className="space-y-0.5">{treeNodes.map((node) => renderTreeNode(node, 0))}</div>;
115});
116
117function collectDirectoryPaths(nodes: ReadonlyArray<TurnDiffTreeNode>): string[] {
118 const paths: string[] = [];
119 for (const node of nodes) {
120 if (node.kind !== "directory") continue;
121 paths.push(node.path);
122 paths.push(...collectDirectoryPaths(node.children));
123 }
124 return paths;
125}
126
127function buildDirectoryExpansionState(
128 directoryPaths: ReadonlyArray<string>,
129 expanded: boolean,
130): Record<string, boolean> {
131 const expandedState: Record<string, boolean> = {};
132 for (const directoryPath of directoryPaths) {
133 expandedState[directoryPath] = expanded;
134 }
135 return expandedState;
136}