Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestFileSections.ts

37 lines1.2 KB

1import type { ReviewRenderableFile } from "../review/reviewModel";
2​
3export function fileDirectory(path: string) {
4 const separator = path.lastIndexOf("/");
5 return separator < 0 ? "" : path.slice(0, separator);
6}
7​
8type SheetItem =
9 | {
10 readonly kind: "directory";
11 readonly key: string;
12 readonly directory: string;
13 readonly collapsed: boolean;
14 }
15 | { readonly kind: "file"; readonly key: string; readonly file: ReviewRenderableFile };
16​
17export function groupByDirectory(
18 files: ReadonlyArray<ReviewRenderableFile>,
19 collapsed: ReadonlySet<string>,
20) {
21 // Keep the shared review order: a directory gets a new header each time it resumes after a subdirectory.
22 const items: SheetItem[] = [];
23 files.forEach((file, index) => {
24 const directory = fileDirectory(file.path);
25 const previous = files[index - 1];
26 if (!previous || directory !== fileDirectory(previous.path)) {
27 items.push({
28 kind: "directory",
29 key: `directory:${directory}:${index}`,
30 directory,
31 collapsed: collapsed.has(directory),
32 });
33 }
34 if (!collapsed.has(directory)) items.push({ kind: "file", key: file.id, file });
35 });
36 return items;