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 { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
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 readonly updatedAt: string | null;
23 readonly url: string;
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 label: String(pr.number),
50 accessibilityLabel: `#${pr.number} ${changeRequest.singular} ${isDraft ? "draft" : pr.state}`,
51 textClassName: isDraft ? "text-foreground-muted" : PR_STATE_TEXT_CLASS[pr.state],
52 };
53}
54
55export function presentThreadLinkedPullRequests(
56 links: ReadonlyArray<ThreadPullRequestLink>,
57): ThreadPrPresentation | null {
58 const link = resolveThreadCurrentPullRequestLink(links);
59 const badge = resolveThreadPullRequestBadge(links);
60 if (link === null || badge === null) return null;
61 const snapshot = link.snapshot;
62 const linkedCount = badge.kind === "pull-request" && badge.others > 0 ? badge.others + 1 : null;
63 const isMultiple = badge.kind === "stack" || linkedCount !== null;
64 const state = isMultiple
65 ? badge.state === "draft"
66 ? "open"
67 : badge.state
68 : (snapshot?.state ?? null);
69 const isDraft = isMultiple
70 ? badge.state === "draft"
71 : snapshot?.isDraft === true && state === "open";
72 const label =
73 badge.kind === "stack"
74 ? String(badge.layers)
75 : linkedCount !== null
76 ? `+${linkedCount}`
77 : String(link.number);
78 return {
79 kind: badge.kind,
80 others: badge.kind === "pull-request" ? badge.others : 0,
81 number: link.number,
82 state,
83 isDraft,
84 updatedAt: snapshot?.updatedAt ?? null,
85 url: link.url,
86 label,
87 accessibilityLabel:
88 badge.kind === "stack"
89 ? `${badge.layers} pull requests in stack, ${isDraft ? "draft" : (state ?? "status pending")}`
90 : linkedCount !== null
91 ? `${linkedCount} linked pull requests, overall ${badge.state}`
92 : `#${link.number} pull request ${state === null ? "status pending" : isDraft ? "draft" : state}`,
93 textClassName:
94 state === null || isDraft
95 ? "text-foreground-muted"
96 : isMultiple && state === "closed"
97 ? "text-adaptive-rose-600-400"
98 : PR_STATE_TEXT_CLASS[state],
99 };
100}
101
102export function resolveThreadPrSource(
103 thread: Pick<EnvironmentThreadShell, "pullRequests" | "linkedPullRequest" | "branchPullRequest">,
104 capabilities:
105 | Pick<ExecutionEnvironmentCapabilities, "threadPullRequests" | "threadPullRequestLinking">
106 | undefined,
107) {
108 const supportsSnapshots = capabilities?.threadPullRequests === true;
109 const linkedPresentation = supportsSnapshots
110 ? presentThreadLinkedPullRequests(thread.pullRequests)
111 : null;
112 const pullRequestRef =
113 linkedPresentation !== null
114 ? null
115 : ((supportsSnapshots
116 ? thread.branchPullRequest
117 : (thread.linkedPullRequest ?? thread.branchPullRequest)) ?? null);
118 return { linkedPresentation, pullRequestRef };
119}
120
121export function resolveThreadPrBadgeAction(pr: ThreadPrPresentation) {
122 return pr.kind === "stack" || pr.others > 0
123 ? { kind: "list" as const, accessibilityLabel: `Show ${pr.accessibilityLabel}` }
124 : { kind: "open" as const, accessibilityLabel: `Open ${pr.accessibilityLabel}` };
125}