Open History to see the last commit
1import type { PullRequestRef, PullRequestStack, ThreadPullRequestLink } from "@t3tools/contracts";
2
3/** Saved membership supports navigation, never action head SHAs. */
4export function savedPullRequestStack(
5 links: ReadonlyArray<ThreadPullRequestLink>,
6 reference: PullRequestRef,
7): PullRequestStack | null {
8 const host = reference.host?.toLowerCase();
9 if (!host) return null;
10 const matching = links.filter(
11 (link) =>
12 link.host.toLowerCase() === host &&
13 link.repository.toLowerCase() === reference.repository.toLowerCase(),
14 );
15 const exact = matching.filter((link) => link.number === reference.number);
16 const candidates =
17 exact.length > 0
18 ? exact
19 : matching.filter((link) =>
20 link.stack?.layers.some((layer) => layer.number === reference.number),
21 );
22 const newest = candidates.toSorted(
23 (a, b) =>
24 Date.parse(b.snapshot?.syncedAt ?? b.linkedAt) -
25 Date.parse(a.snapshot?.syncedAt ?? a.linkedAt),
26 )[0];
27 const stack = newest?.stack;
28 if (!stack || !stack.layers.some((layer) => layer.number === reference.number)) return null;
29 return {
30 id: stack.id,
31 number: stack.number,
32 url: stack.url,
33 base: stack.base,
34 layers: stack.layers.map((layer) => {
35 const snapshot = matching
36 .filter((link) => link.number === layer.number)
37 .toSorted(
38 (a, b) =>
39 Date.parse(b.snapshot?.syncedAt ?? b.linkedAt) -
40 Date.parse(a.snapshot?.syncedAt ?? a.linkedAt),
41 )[0]?.snapshot;
42 return {
43 ...layer,
44 ...(snapshot ? { title: snapshot.title, isDraft: snapshot.isDraft } : {}),
45 };
46 }),
47 };
48}
49
50export function pullRequestStackView(
51 query: {
52 data: PullRequestStack | null;
53 isSuccess: boolean;
54 isPending: boolean;
55 error: string | null;
56 },
57 saved: PullRequestStack | null,
58) {
59 const data = query.isSuccess ? query.data : (query.data ?? saved);
60 return {
61 data,
62 isFresh: query.isSuccess,
63 notice:
64 data === null
65 ? null
66 : query.error
67 ? "Stack data may be stale. We couldn’t refresh it."
68 : !query.isSuccess || query.isPending
69 ? "Refreshing stack… Showing saved data."
70 : null,
71 };
72}