|
| 1 | +import { basename, dirname } from "node:path/posix"; |
1 | 2 | import type { DiffFile } from "../../core/types"; |
2 | 3 |
|
3 | 4 | export interface FileListEntry { |
| 5 | + kind: "file"; |
| 6 | + id: string; |
| 7 | + name: string; |
| 8 | + additionsText: string; |
| 9 | + deletionsText: string; |
| 10 | +} |
| 11 | + |
| 12 | +export interface FileGroupEntry { |
| 13 | + kind: "group"; |
4 | 14 | id: string; |
5 | 15 | label: string; |
6 | | - description: string; |
7 | 16 | } |
8 | 17 |
|
9 | | -/** Build the sidebar label and summary text for one diff file. */ |
10 | | -export function buildFileListEntry(file: DiffFile): FileListEntry { |
11 | | - const prefix = |
12 | | - file.metadata.type === "new" |
13 | | - ? "A" |
14 | | - : file.metadata.type === "deleted" |
15 | | - ? "D" |
16 | | - : file.metadata.type.startsWith("rename") |
17 | | - ? "R" |
18 | | - : "M"; |
19 | | - |
20 | | - const pathLabel = |
21 | | - file.previousPath && file.previousPath !== file.path |
22 | | - ? `${file.previousPath} -> ${file.path}` |
23 | | - : file.path; |
24 | | - |
25 | | - return { |
26 | | - id: file.id, |
27 | | - label: `${prefix} ${pathLabel}`, |
28 | | - description: `+${file.stats.additions} -${file.stats.deletions}${file.agent ? " agent" : ""}`, |
29 | | - }; |
| 18 | +export type SidebarEntry = FileListEntry | FileGroupEntry; |
| 19 | + |
| 20 | +/** Build the filename-first label shown inside one sidebar row. */ |
| 21 | +function sidebarFileName(file: DiffFile) { |
| 22 | + if (!file.previousPath || file.previousPath === file.path) { |
| 23 | + return basename(file.path); |
| 24 | + } |
| 25 | + |
| 26 | + const previousName = basename(file.previousPath); |
| 27 | + const nextName = basename(file.path); |
| 28 | + return previousName === nextName ? nextName : `${previousName} -> ${nextName}`; |
| 29 | +} |
| 30 | + |
| 31 | +/** Group sidebar rows by their current parent folder while preserving file order. */ |
| 32 | +export function buildSidebarEntries(files: DiffFile[]): SidebarEntry[] { |
| 33 | + const entries: SidebarEntry[] = []; |
| 34 | + let activeGroup: string | null = null; |
| 35 | + |
| 36 | + files.forEach((file, index) => { |
| 37 | + const group = dirname(file.path); |
| 38 | + const nextGroup = group === "." ? null : group; |
| 39 | + |
| 40 | + if (nextGroup !== activeGroup) { |
| 41 | + activeGroup = nextGroup; |
| 42 | + if (activeGroup) { |
| 43 | + entries.push({ |
| 44 | + kind: "group", |
| 45 | + id: `group:${activeGroup}:${index}`, |
| 46 | + label: `${activeGroup}/`, |
| 47 | + }); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + entries.push({ |
| 52 | + kind: "file", |
| 53 | + id: file.id, |
| 54 | + name: sidebarFileName(file), |
| 55 | + additionsText: `+${file.stats.additions}`, |
| 56 | + deletionsText: `-${file.stats.deletions}`, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + return entries; |
30 | 61 | } |
31 | 62 |
|
32 | 63 | /** Build the canonical file label used across headers and note cards. */ |
|
0 commit comments