Open History to see the last commit
1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import type {
3 ExecutionEnvironmentCapabilities,
4 ThreadPullRequestLink,
5 VcsStatusResult,
6} from "@t3tools/contracts";
7import { resolveChangeRequestPresentation } from "@t3tools/shared/sourceControl";
8
9import {
10 resolveThreadCurrentPullRequestLink,
11 resolveThreadPullRequestBadge,
12} from "@t3tools/shared/threadPullRequests";
13
14export type ThreadPr = NonNullable<VcsStatusResult["pr"]>;
15
16export interface ThreadPrPresentation {
17 readonly number: number;
18 readonly state: ThreadPr["state"] | null;
19 readonly kind: "pull-request" | "stack";
20 readonly others: number;
21 readonly isDraft: boolean;
22 /** Provider-side last activity, bounding when a terminal state landed. */
23 readonly updatedAt: string | null;
24 readonly url: string;
25 /** Compact pull request number or linked count, e.g. "3774" or "+2". */
26 readonly label: string;
27 /** Full, provider-aware label for assistive technologies. */
28 readonly accessibilityLabel: string;
29 readonly textClassName: string;
30}
31
32const PR_STATE_TEXT_CLASS: Record<ThreadPr["state"], string> = {
33 open: "text-adaptive-emerald-600-400",
34 merged: "text-adaptive-violet-600-400",
35 closed: "text-foreground-muted",
36};
37
38export function presentThreadPr(
39 pr: ThreadPr,
40 provider: VcsStatusResult["sourceControlProvider"] | null | undefined,
41): ThreadPrPresentation {
42 const presentation = resolveChangeRequestPresentation(provider);
43 const isDraft = pr.state === "open" && pr.isDraft === true;
44 return {
45 kind: "pull-request",
46 others: 0,
47 number: pr.number,
48 state: pr.state,
49 isDraft,
50 updatedAt: pr.updatedAt ?? null,
51 url: pr.url,
52 label: String(pr.number),
53 accessibilityLabel: `#${pr.number} ${presentation.longName} ${isDraft ? "draft" : pr.state}`,
54 textClassName: isDraft ? "text-foreground-muted" : PR_STATE_TEXT_CLASS[pr.state],
55 };
56}
57
58/** Persisted links render immediately, including links awaiting their first host sync. */
59export function presentThreadLinkedPullRequests(
60 links: ReadonlyArray<ThreadPullRequestLink>,
61): ThreadPrPresentation | null {
62 const link = resolveThreadCurrentPullRequestLink(links);
63 const badge = resolveThreadPullRequestBadge(links);
64 if (link === null || badge === null) return null;
65 const snapshot = link.snapshot;
66 const state = badge.kind === "stack" ? badge.state : (snapshot?.state ?? null);
67 const isDraft = snapshot?.isDraft === true && state === "open";
68 const linkedCount = badge.kind === "pull-request" && badge.others > 0 ? badge.others + 1 : null;
69 const label =
70 badge.kind === "stack"
71 ? String(badge.layers)
72 : linkedCount !== null
73 ? `+${linkedCount}`
74 : String(link.number);
75 return {
76 kind: badge.kind,
77 others: badge.kind === "pull-request" ? badge.others : 0,
78 number: link.number,
79 state,
80 isDraft,
81 updatedAt: snapshot?.updatedAt ?? null,
82 url: link.url,
83 label,
84 accessibilityLabel:
85 badge.kind === "stack"
86 ? `${badge.layers} pull requests in stack, ${state ?? "status pending"}`
87 : `#${link.number} pull request ${state === null ? "status pending" : isDraft ? "draft" : state}${badge.others > 0 ? `, ${badge.others} more linked` : ""}`,
88 textClassName:
89 linkedCount !== null || state === null || isDraft
90 ? "text-foreground-muted"
91 : PR_STATE_TEXT_CLASS[state],
92 };
93}
94
95/** Only the array capability replaces legacy references with persisted snapshots. */
96export function resolveThreadPrSource(
97 thread: Pick<EnvironmentThreadShell, "pullRequests" | "linkedPullRequest" | "branchPullRequest">,
98 capabilities:
99 | Pick<ExecutionEnvironmentCapabilities, "threadPullRequests" | "threadPullRequestLinking">
100 | undefined,
101) {
102 const supportsSnapshots = capabilities?.threadPullRequests === true;
103 const linkedPresentation = supportsSnapshots
104 ? presentThreadLinkedPullRequests(thread.pullRequests)
105 : null;
106 const pullRequestRef =
107 linkedPresentation !== null
108 ? null
109 : ((supportsSnapshots
110 ? thread.branchPullRequest
111 : (thread.linkedPullRequest ?? thread.branchPullRequest)) ?? null);
112 return { linkedPresentation, pullRequestRef };
113}