-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdiffFileReviewState.ts
More file actions
90 lines (83 loc) · 2.07 KB
/
diffFileReviewState.ts
File metadata and controls
90 lines (83 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
export interface DiffFileReviewState {
collapsed: boolean;
accepted: boolean;
contextMode: "patch" | "full";
}
export type DiffFileReviewStateByPath = Record<string, DiffFileReviewState>;
const DEFAULT_DIFF_FILE_REVIEW_STATE: DiffFileReviewState = {
collapsed: true,
accepted: false,
contextMode: "patch",
};
export function reconcileDiffFileReviewState(
paths: ReadonlyArray<string>,
current: DiffFileReviewStateByPath | undefined,
): DiffFileReviewStateByPath {
const next: DiffFileReviewStateByPath = {};
for (const path of paths) {
next[path] = current?.[path] ?? DEFAULT_DIFF_FILE_REVIEW_STATE;
}
return next;
}
export function toggleDiffFileAccepted(
current: DiffFileReviewStateByPath,
path: string,
): DiffFileReviewStateByPath {
const previous = current[path] ?? DEFAULT_DIFF_FILE_REVIEW_STATE;
const accepted = !previous.accepted;
return {
...current,
[path]: {
accepted,
collapsed: accepted,
contextMode: previous.contextMode,
},
};
}
export function toggleDiffFileCollapsed(
current: DiffFileReviewStateByPath,
path: string,
): DiffFileReviewStateByPath {
const previous = current[path] ?? DEFAULT_DIFF_FILE_REVIEW_STATE;
return {
...current,
[path]: {
...previous,
collapsed: !previous.collapsed,
},
};
}
export function setDiffFileContextMode(
current: DiffFileReviewStateByPath,
path: string,
contextMode: "patch" | "full",
): DiffFileReviewStateByPath {
const previous = current[path] ?? DEFAULT_DIFF_FILE_REVIEW_STATE;
if (previous.contextMode === contextMode) {
return current;
}
return {
...current,
[path]: {
...previous,
collapsed: contextMode === "full" ? false : previous.collapsed,
contextMode,
},
};
}
export function expandDiffFile(
current: DiffFileReviewStateByPath,
path: string,
): DiffFileReviewStateByPath {
const previous = current[path] ?? DEFAULT_DIFF_FILE_REVIEW_STATE;
if (!previous.collapsed) {
return current;
}
return {
...current,
[path]: {
...previous,
collapsed: false,
},
};
}