@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 135 lines 4.88 KB
Raw
1 import type { FileDiffMetadata } from "@pierre/diffs";
2 import type {
3 FileTreeBatchOperation,
4 FileTreeIconConfig,
5 FileTreeSortComparator,
6 GitStatus,
7 } from "@pierre/trees";
8
9 import { resolveFileDiffPath } from "~/lib/diffRendering";
10 import { basenameOfPath, resolvePierreIconForEntry, T3_PIERRE_ICONS } from "~/pierre-icons";
11
12 export interface DiffFileTreeEntry {
13 readonly path: string;
14 readonly status: GitStatus;
15 }
16
17 function toGitStatus(file: FileDiffMetadata): GitStatus {
18 switch (file.type) {
19 case "new":
20 return "added";
21 case "deleted":
22 return "deleted";
23 case "rename-pure":
24 case "rename-changed":
25 return "renamed";
26 case "change":
27 return "modified";
28 }
29 }
30
31 export function diffFileTreeEntries(
32 files: ReadonlyArray<FileDiffMetadata>,
33 ): ReadonlyArray<DiffFileTreeEntry> {
34 const statusByPath = new Map<string, GitStatus>();
35 for (const file of files) {
36 const path = resolveFileDiffPath(file);
37 const status = toGitStatus(file);
38 const previous = statusByPath.get(path);
39 statusByPath.set(path, previous === undefined || previous === status ? status : "modified");
40 }
41 return [...statusByPath].map(([path, status]) => ({ path, status }));
42 }
43
44 // Pierre cannot store a file and a directory at the same path. Give only those
45 // files an invisible suffix, keeping real paths at the diff/viewer boundary.
46 export function diffFileTreeModel(paths: ReadonlyArray<string>) {
47 const directories = new Set(collectDirectoryPaths(paths));
48 // Icon overrides match basenames case-insensitively, so aliases must also avoid
49 // real names in other directories, including names that already contain the suffix.
50 const occupiedNames = new Set(paths.flatMap((path) => path.toLowerCase().split("/")));
51 const selectionByModelPath = new Map<string, string>();
52 const modelBySelectionPath = new Map<string, string>();
53 const byFileName: FileTreeIconConfig["byFileName"] = { ...T3_PIERRE_ICONS.byFileName };
54 const modelPaths = paths.map((path) => {
55 if (!directories.has(`${path}/`)) return path;
56 let modelPath = path;
57 do {
58 modelPath += "\u200b";
59 } while (occupiedNames.has(basenameOfPath(modelPath).toLowerCase()));
60 occupiedNames.add(basenameOfPath(modelPath).toLowerCase());
61 selectionByModelPath.set(modelPath, path);
62 modelBySelectionPath.set(path, modelPath);
63 const icon = resolvePierreIconForEntry(path, "file");
64 if (icon !== null) byFileName[basenameOfPath(modelPath)] = icon;
65 return modelPath;
66 });
67 return {
68 paths: modelBySelectionPath.size === 0 ? paths : modelPaths,
69 selectionPath: (modelPath: string) => selectionByModelPath.get(modelPath) ?? modelPath,
70 modelPath: (path: string) => modelBySelectionPath.get(path) ?? path,
71 icons: modelBySelectionPath.size === 0 ? T3_PIERRE_ICONS : { ...T3_PIERRE_ICONS, byFileName },
72 };
73 }
74
75 export function collectDirectoryPaths(paths: ReadonlyArray<string>): ReadonlyArray<string> {
76 const directories = new Set<string>();
77 for (const path of paths) {
78 const segments = path.split("/");
79 let directory = "";
80 for (const segment of segments.slice(0, -1)) {
81 directory += `${segment}/`;
82 directories.add(directory);
83 }
84 }
85 return [...directories];
86 }
87
88 export function compareDiffFileTreeEntries(
89 getPositions: () => ReadonlyMap<string, number>,
90 ): FileTreeSortComparator {
91 return (left, right) => {
92 const positions = getPositions();
93 return (
94 (positions.get(left.path) ?? Number.MAX_SAFE_INTEGER) -
95 (positions.get(right.path) ?? Number.MAX_SAFE_INTEGER) ||
96 left.depth - right.depth ||
97 left.path.localeCompare(right.path)
98 );
99 };
100 }
101
102 function pathDepth(path: string): number {
103 return path.split("/").filter(Boolean).length;
104 }
105
106 export function buildDiffFileTreeUpdates(
107 previousPaths: ReadonlyArray<string>,
108 nextPaths: ReadonlyArray<string>,
109 ): FileTreeBatchOperation[] {
110 const previousDirectories = new Set(collectDirectoryPaths(previousPaths));
111 const nextDirectories = new Set(collectDirectoryPaths(nextPaths));
112 const previous = new Set(previousPaths);
113 const next = new Set(nextPaths);
114 const updates: FileTreeBatchOperation[] = [];
115
116 for (const path of previousPaths) {
117 if (!next.has(path)) updates.push({ type: "remove", path });
118 }
119 const removedDirectories = [...previousDirectories]
120 .filter((directory) => !nextDirectories.has(directory))
121 .toSorted((left, right) => pathDepth(right) - pathDepth(left));
122 for (const directory of removedDirectories) {
123 updates.push({ type: "remove", path: directory, recursive: true });
124 }
125
126 const addedDirectories = [...nextDirectories]
127 .filter((directory) => !previousDirectories.has(directory))
128 .toSorted((left, right) => pathDepth(left) - pathDepth(right));
129 for (const directory of addedDirectories) updates.push({ type: "add", path: directory });
130 for (const path of nextPaths) {
131 if (!previous.has(path)) updates.push({ type: "add", path });
132 }
133
134 return updates;
135 }