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

thread-pr-presentation.ts

133 lines4.6 KB

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