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

thread-pr-presentation.ts

196 lines6.7 KB

1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import type {
3 PullRequestSummary,
4 ThreadPullRequestKey,
5 ThreadPullRequestLink,
6 VcsStatusResult,
7} from "@t3tools/contracts";
8import {
9 sourceControlClients,
10 type ChangeRequestTerminology,
11} from "@t3tools/client-runtime/source-control-clients";
12​
13import {
14 legacyThreadPullRequestKey,
15 resolveThreadCurrentPullRequestLink,
16 resolveThreadPullRequestBadge,
17 threadPullRequestKeyOf,
18} from "@t3tools/client-runtime/thread-pull-requests";
19​
20export type ThreadPr = NonNullable<VcsStatusResult["pr"]>;
21​
22export interface ThreadPrPresentation {
23 readonly number: number;
24 readonly state: ThreadPr["state"] | null;
25 readonly kind: "pull-request" | "stack";
26 readonly others: number;
27 readonly isDraft: boolean;
28 readonly updatedAt: string | null;
29 readonly url: string;
30 readonly changeRequest: ChangeRequestTerminology;
31 readonly label: string;
32 readonly accessibilityLabel: string;
33 readonly textClassName: string;
35​
36const PR_STATE_TEXT_CLASS: Record<ThreadPr["state"], string> = {
37 open: "text-adaptive-emerald-600-400",
38 merged: "text-adaptive-violet-600-400",
39 closed: "text-adaptive-rose-600-400",
40};
41​
42export function presentThreadPr(
43 pr: ThreadPr,
44 provider: VcsStatusResult["sourceControlProvider"] | null | undefined,
45): ThreadPrPresentation {
46 const { changeRequest } = sourceControlClients.get(provider?.kind);
47 const isDraft = pr.state === "open" && pr.isDraft === true;
48 return {
49 kind: "pull-request",
50 others: 0,
51 number: pr.number,
52 state: pr.state,
53 isDraft,
54 updatedAt: pr.updatedAt ?? null,
55 url: pr.url,
56 changeRequest,
57 label: String(pr.number),
58 accessibilityLabel: `#${pr.number} ${changeRequest.singular} ${isDraft ? "draft" : pr.state}`,
59 textClassName: isDraft ? "text-foreground-muted" : PR_STATE_TEXT_CLASS[pr.state],
60 };
62​
63export function presentThreadLinkedPullRequests(
64 links: ReadonlyArray<ThreadPullRequestLink>,
65): ThreadPrPresentation | null {
66 const link = resolveThreadCurrentPullRequestLink(links);
67 const badge = resolveThreadPullRequestBadge(links);
68 if (link === null || badge === null) return null;
69 const { changeRequest } =
70 sourceControlClients.findByChangeRequestUrl(link.url) ?? sourceControlClients.get(undefined);
71 const snapshot = link.snapshot;
72 const linkedCount = badge.kind === "pull-request" && badge.others > 0 ? badge.others + 1 : null;
73 const isMultiple = badge.kind === "stack" || linkedCount !== null;
74 const state = isMultiple
75 ? badge.state === "draft"
76 ? "open"
77 : badge.state
78 : (snapshot?.state ?? null);
79 const isDraft = isMultiple
80 ? badge.state === "draft"
81 : snapshot?.isDraft === true && state === "open";
82 const label =
83 badge.kind === "stack"
84 ? String(badge.layers)
85 : linkedCount !== null
86 ? `+${linkedCount}`
87 : String(link.number);
88 return {
89 kind: badge.kind,
90 others: badge.kind === "pull-request" ? badge.others : 0,
91 number: link.number,
92 state,
93 isDraft,
94 updatedAt: snapshot?.updatedAt ?? null,
95 url: link.url,
96 changeRequest,
97 label,
98 accessibilityLabel:
99 badge.kind === "stack"
100 ? `${badge.layers} ${changeRequest.singular}s in stack, ${isDraft ? "draft" : (state ?? "status pending")}`
101 : linkedCount !== null
102 ? `${linkedCount} linked ${changeRequest.plural}, overall ${badge.state}`
103 : `#${link.number} ${changeRequest.singular} ${state === null ? "status pending" : isDraft ? "draft" : state}`,
104 textClassName: state === null || isDraft ? "text-foreground-muted" : PR_STATE_TEXT_CLASS[state],
105 };
107​
108export function resolveThreadPrSource(
109 thread: Pick<EnvironmentThreadShell, "pullRequests" | "linkedPullRequest" | "branchPullRequest">,
110 /** False until the server config loads; the stored references serve until then. */
111 supportsSnapshots: boolean,
112) {
113 const linkedPresentation = supportsSnapshots
114 ? presentThreadLinkedPullRequests(thread.pullRequests)
115 : null;
116 const pullRequestRef =
117 linkedPresentation !== null
118 ? null
119 : ((supportsSnapshots
120 ? thread.branchPullRequest
121 : (thread.linkedPullRequest ?? thread.branchPullRequest)) ?? null);
122 return { linkedPresentation, pullRequestRef };
124​
125export function resolveThreadPrBadgeAction(pr: ThreadPrPresentation) {
126 return pr.kind === "stack" || pr.others > 0
127 ? { kind: "list" as const, accessibilityLabel: `Show ${pr.accessibilityLabel}` }
128 : { kind: "open" as const, accessibilityLabel: `Open ${pr.accessibilityLabel}` };
130​
131/** Records `value` as the newest entry, evicting the least recently answered beyond `limit`. */
132export function rememberBounded<V>(
133 current: ReadonlyMap<string, V>,
134 key: string,
135 value: V,
136 limit: number,
137): ReadonlyMap<string, V> {
138 const next = new Map(current);
139 next.delete(key);
140 next.set(key, value);
141 for (const oldest of next.keys()) {
142 if (next.size <= limit) break;
143 next.delete(oldest);
144 }
145 return next;
147​
148/** The newest summary held for `pullRequest`, matched strictly by host, repository and number. */
149export function knownPullRequestSummary(
150 pullRequest: ThreadPullRequestKey,
151 threads: Iterable<Pick<EnvironmentThreadShell, "projectId" | "pullRequests">>,
152 summaries: Iterable<PullRequestSummary>,
153): PullRequestSummary | undefined {
154 const key = threadPullRequestKeyOf(pullRequest);
155 let newest:
156 | { readonly summary: PullRequestSummary; readonly updatedAt: number; readonly readAt: number }
157 | undefined;
158 // Checks and reviews change without moving `updatedAt`, so equal versions prefer the later read.
159 const consider = (summary: PullRequestSummary, readAt = Number.NEGATIVE_INFINITY) => {
160 const updatedAt = Date.parse(summary.updatedAt);
161 if (
162 newest === undefined ||
163 updatedAt > newest.updatedAt ||
164 (updatedAt === newest.updatedAt && readAt > newest.readAt)
165 ) {
166 newest = { summary, updatedAt, readAt };
167 }
168 };
169 for (const summary of summaries) {
170 if (threadPullRequestKeyOf(legacyThreadPullRequestKey(summary)) === key) {
171 consider(summary, summary.observedAt);
172 }
173 }
174 for (const thread of threads) {
175 for (const link of thread.pullRequests) {
176 const snapshot = link.snapshot;
177 if (snapshot?.updatedAt == null || threadPullRequestKeyOf(link) !== key) continue;
178 consider(
179 {
180 ...snapshot,
181 updatedAt: snapshot.updatedAt,
182 provider: (
183 sourceControlClients.findByChangeRequestUrl(link.url) ??
184 sourceControlClients.get(undefined)
185 ).kind,
186 projectId: thread.projectId,
187 repository: link.repository,
188 number: link.number,
189 url: link.url,
190 },
191 Date.parse(snapshot.syncedAt),
192 );
193 }
194 }
195 return newest?.summary;