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

main
ts 77 lines 2.44 KB
Raw
1 export type DiffFoldOverride = "expanded" | "folded" | null;
2
3 /** The reader's own fold choice per file. It is absolute, so a Viewed mark loading later keeps it. */
4 export type FileDiffFoldChoices = ReadonlyMap<string, boolean>;
5
6 export function isFileDiffCollapsed(
7 fileKey: string,
8 foldOverride: DiffFoldOverride,
9 choices: FileDiffFoldChoices,
10 viewed = false,
11 ): boolean {
12 return choices.get(fileKey) ?? (foldOverride === null ? viewed : foldOverride === "folded");
13 }
14
15 export function withFileDiffFold(
16 choices: FileDiffFoldChoices,
17 fileKey: string,
18 collapsed: boolean | undefined,
19 ): FileDiffFoldChoices {
20 if (choices.get(fileKey) === collapsed) return choices;
21 const next = new Map(choices);
22 if (collapsed === undefined) next.delete(fileKey);
23 else next.set(fileKey, collapsed);
24 return next;
25 }
26
27 export interface DiffFoldGroup {
28 readonly override: DiffFoldOverride;
29 readonly choices: FileDiffFoldChoices;
30 }
31
32 /** Test files keep their own group, so a bulk fold while they are hidden leaves them as they were. */
33 export interface PullRequestDiffFolds {
34 readonly source: DiffFoldGroup;
35 readonly test: DiffFoldGroup;
36 }
37
38 const EMPTY_GROUP: DiffFoldGroup = { override: null, choices: new Map() };
39
40 export const EMPTY_PULL_REQUEST_DIFF_FOLDS: PullRequestDiffFolds = {
41 source: EMPTY_GROUP,
42 test: EMPTY_GROUP,
43 };
44
45 export function withPullRequestDiffFold(
46 folds: PullRequestDiffFolds,
47 test: boolean,
48 update: (choices: FileDiffFoldChoices) => FileDiffFoldChoices,
49 ): PullRequestDiffFolds {
50 const group = test ? folds.test : folds.source;
51 const choices = update(group.choices);
52 if (choices === group.choices) return folds;
53 return { ...folds, [test ? "test" : "source"]: { ...group, choices } };
54 }
55
56 /**
57 * Expand or fold every file, on later pages too. While test files are hidden, only the tests
58 * still shown (a revealed one) follow; the rest keep their choices and fallback.
59 */
60 export function foldAllPullRequestDiffs(
61 folds: PullRequestDiffFolds,
62 override: NonNullable<DiffFoldOverride>,
63 hiddenTests: { readonly shownKeys: ReadonlyArray<string> } | null,
64 ): PullRequestDiffFolds {
65 const all: DiffFoldGroup = { override, choices: new Map() };
66 if (hiddenTests === null) return { source: all, test: all };
67 return {
68 source: all,
69 test: {
70 ...folds.test,
71 choices: hiddenTests.shownKeys.reduce(
72 (choices, key) => withFileDiffFold(choices, key, override === "folded"),
73 folds.test.choices,
74 ),
75 },
76 };
77 }