Open History to see the last commit
1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import type { ThreadPullRequestLink, VcsStatusResult } from "@t3tools/contracts";
3import {
4 sourceControlClients,
5 type ChangeRequestTerminology,
6} from "@t3tools/client-runtime/source-control-clients";
7
8import {
9 resolveThreadCurrentPullRequestLink,
10 resolveThreadPullRequestBadge,
11} from "@t3tools/client-runtime/thread-pull-requests";
12
13export type ThreadPr = NonNullable<VcsStatusResult["pr"]>;
14
15export interface ThreadPrPresentation {
16 readonly number: number;
17 readonly state: ThreadPr["state"] | null;
18 readonly kind: "pull-request" | "stack";
19 readonly others: number;
20 readonly isDraft: boolean;
21 readonly updatedAt: string | null;
22 readonly url: string;
23 readonly changeRequest: ChangeRequestTerminology;
24 readonly label: string;
25 readonly accessibilityLabel: string;
26 readonly textClassName: string;
27}
28
29const PR_STATE_TEXT_CLASS: Record<ThreadPr["state"], string> = {
30 open: "text-adaptive-emerald-600-400",
31 merged: "text-adaptive-violet-600-400",
32 closed: "text-foreground-muted",
33};
34
35export function presentThreadPr(
36 pr: ThreadPr,
37 provider: VcsStatusResult["sourceControlProvider"] | null | undefined,
38): ThreadPrPresentation {
39 const { changeRequest } = sourceControlClients.get(provider?.kind);
40 const isDraft = pr.state === "open" && pr.isDraft === true;
41 return {
42 kind: "pull-request",
43 others: 0,
44 number: pr.number,
45 state: pr.state,
46 isDraft,
47 updatedAt: pr.updatedAt ?? null,
48 url: pr.url,
49 changeRequest,
50 label: String(pr.number),
51 accessibilityLabel: `#${pr.number} ${changeRequest.singular} ${isDraft ? "draft" : pr.state}`,
52 textClassName: isDraft ? "text-foreground-muted" : PR_STATE_TEXT_CLASS[pr.state],
53 };
54}
55
56export function presentThreadLinkedPullRequests(
57 links: ReadonlyArray<ThreadPullRequestLink>,
58): ThreadPrPresentation | null {
59 const link = resolveThreadCurrentPullRequestLink(links);
60 const badge = resolveThreadPullRequestBadge(links);
61 if (link === null || badge === null) return null;
62 const { changeRequest } =
63 sourceControlClients.findByChangeRequestUrl(link.url) ?? sourceControlClients.get(undefined);
64 const snapshot = link.snapshot;
65 const linkedCount = badge.kind === "pull-request" && badge.others > 0 ? badge.others + 1 : null;
66 const isMultiple = badge.kind === "stack" || linkedCount !== null;
67 const state = isMultiple
68 ? badge.state === "draft"
69 ? "open"
70 : badge.state
71 : (snapshot?.state ?? null);
72 const isDraft = isMultiple
73 ? badge.state === "draft"
74 : snapshot?.isDraft === true && state === "open";
75 const label =
76 badge.kind === "stack"
77 ? String(badge.layers)
78 : linkedCount !== null
79 ? `+${linkedCount}`
80 : String(link.number);
81 return {
82 kind: badge.kind,
83 others: badge.kind === "pull-request" ? badge.others : 0,
84 number: link.number,
85 state,
86 isDraft,
87 updatedAt: snapshot?.updatedAt ?? null,
88 url: link.url,
89 changeRequest,
90 label,
91 accessibilityLabel:
92 badge.kind === "stack"
93 ? `${badge.layers} ${changeRequest.singular}s in stack, ${isDraft ? "draft" : (state ?? "status pending")}`
94 : linkedCount !== null
95 ? `${linkedCount} linked ${changeRequest.plural}, overall ${badge.state}`
96 : `#${link.number} ${changeRequest.singular} ${state === null ? "status pending" : isDraft ? "draft" : state}`,
97 textClassName:
98 state === null || isDraft
99 ? "text-foreground-muted"
100 : isMultiple && state === "closed"
101 ? "text-adaptive-rose-600-400"
102 : PR_STATE_TEXT_CLASS[state],
103 };
104}
105
106export function resolveThreadPrSource(
107 thread: Pick<EnvironmentThreadShell, "pullRequests" | "linkedPullRequest" | "branchPullRequest">,
108 /** False until the server config loads; the stored references serve until then. */
109 supportsSnapshots: boolean,
110) {
111 const linkedPresentation = supportsSnapshots
112 ? presentThreadLinkedPullRequests(thread.pullRequests)
113 : null;
114 const pullRequestRef =
115 linkedPresentation !== null
116 ? null
117 : ((supportsSnapshots
118 ? thread.branchPullRequest
119 : (thread.linkedPullRequest ?? thread.branchPullRequest)) ?? null);
120 return { linkedPresentation, pullRequestRef };
121}
122
123export function resolveThreadPrBadgeAction(pr: ThreadPrPresentation) {
124 return pr.kind === "stack" || pr.others > 0
125 ? { kind: "list" as const, accessibilityLabel: `Show ${pr.accessibilityLabel}` }
126 : { kind: "open" as const, accessibilityLabel: `Open ${pr.accessibilityLabel}` };
127}
128
129/** Records `value` as the newest entry, evicting the least recently answered beyond `limit`. */
130export function rememberBounded<V>(
131 current: ReadonlyMap<string, V>,
132 key: string,
133 value: V,
134 limit: number,
135): ReadonlyMap<string, V> {
136 const next = new Map(current);
137 next.delete(key);
138 next.set(key, value);
139 for (const oldest of next.keys()) {
140 if (next.size <= limit) break;
141 next.delete(oldest);
142 }
143 return next;
144}