Repository navigation
Expand file tree
/
Copy pathpullRequestStackSnapshot.ts
More file actions
72 lines (70 loc) · 2.19 KB
/
Copy pathpullRequestStackSnapshot.ts
File metadata and controls
72 lines (70 loc) · 2.19 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
import type { PullRequestRef, PullRequestStack, ThreadPullRequestLink } from "@t3tools/contracts";
/** Saved membership supports navigation, never action head SHAs. */
export function savedPullRequestStack(
links: ReadonlyArray<ThreadPullRequestLink>,
reference: PullRequestRef,
): PullRequestStack | null {
const host = reference.host?.toLowerCase();
if (!host) return null;
const matching = links.filter(
(link) =>
link.host.toLowerCase() === host &&
link.repository.toLowerCase() === reference.repository.toLowerCase(),
);
const exact = matching.filter((link) => link.number === reference.number);
const candidates =
exact.length > 0
? exact
: matching.filter((link) =>
link.stack?.layers.some((layer) => layer.number === reference.number),
);
const newest = candidates.toSorted(
(a, b) =>
Date.parse(b.snapshot?.syncedAt ?? b.linkedAt) -
Date.parse(a.snapshot?.syncedAt ?? a.linkedAt),
)[0];
const stack = newest?.stack;
if (!stack || !stack.layers.some((layer) => layer.number === reference.number)) return null;
return {
id: stack.id,
number: stack.number,
url: stack.url,
base: stack.base,
layers: stack.layers.map((layer) => {
const snapshot = matching
.filter((link) => link.number === layer.number)
.toSorted(
(a, b) =>
Date.parse(b.snapshot?.syncedAt ?? b.linkedAt) -
Date.parse(a.snapshot?.syncedAt ?? a.linkedAt),
)[0]?.snapshot;
return {
...layer,
...(snapshot ? { title: snapshot.title, isDraft: snapshot.isDraft } : {}),
};
}),
};
}
export function pullRequestStackView(
query: {
data: PullRequestStack | null;
isSuccess: boolean;
isPending: boolean;
error: string | null;
},
saved: PullRequestStack | null,
) {
const data = query.isSuccess ? query.data : (query.data ?? saved);
return {
data,
isFresh: query.isSuccess,
notice:
data === null
? null
: query.error
? "Stack data may be stale. We couldn’t refresh it."
: !query.isSuccess || query.isPending
? "Refreshing stack… Showing saved data."
: null,
};
}