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

PullRequestChecksPopover.tsx

169 lines6.0 KB

1import type {
2 EnvironmentId,
3 PullRequestCheck,
4 PullRequestChecksState,
5 PullRequestRef,
6 ScopedThreadRef,
7} from "@t3tools/contracts";
8​
9import { useOpenLink } from "~/browser/useOpenLink";
10import { cn } from "~/lib/utils";
11import { pullRequestEnvironment } from "~/state/pullRequests";
12import { useEnvironmentQuery } from "~/state/query";
13​
14import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
15import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
16import { toastManager } from "../ui/toast";
17import {
18 PullRequestCheckStatusIcon,
19 pullRequestCheckStatusLabel,
20 pullRequestChecksStatePresentation,
21 summarizePullRequestChecks,
22} from "./pullRequestPresentation";
23​
24/**
25 * The checks behind the rollup, for a row that only carries the rollup. Mounted by the popup, so
26 * the read starts when somebody opens it rather than once per row of a listing — the detail read
27 * is a request per pull request, and a page of them at rest would be a hundred.
28 */
29function LazyChecksBody({
30 environmentId,
31 reference,
32 threadRef,
33}: {
34 environmentId: EnvironmentId;
35 reference: PullRequestRef;
36 threadRef: ScopedThreadRef | null;
37}) {
38 const detailQuery = useEnvironmentQuery(
39 pullRequestEnvironment.detail({ environmentId, input: reference }),
40 );
41 if (detailQuery.error !== null) {
42 return <p className="text-muted-foreground text-xs">{detailQuery.error}</p>;
43 }
44 if (detailQuery.data === null) {
45 return (
46 <p className="text-muted-foreground text-xs">
47 {detailQuery.isPending ? "Loading checks…" : "No checks reported"}
48 </p>
49 );
50 }
51 return <ChecksBody checks={detailQuery.data.checks} threadRef={threadRef} />;
53​
54function ChecksBody({
55 checks,
56 threadRef,
57}: {
58 checks: ReadonlyArray<PullRequestCheck>;
59 threadRef: ScopedThreadRef | null;
60}) {
61 const openLink = useOpenLink(threadRef);
62 if (checks.length === 0) {
63 return <p className="text-muted-foreground text-xs">No checks reported</p>;
64 }
65 return (
66 <ul className="flex flex-col gap-1">
67 {/* Keyed by position as well as by name: the host is the one that decides how many runs
68 share a name, and a repeated key is a rendering fault rather than a wrong list. */}
69 {checks.map((check, index) => (
70 <li key={`${index}:${check.name}`} className="flex items-center gap-2 text-xs">
71 <PullRequestCheckStatusIcon status={check.status} />
72 <Tooltip>
73 <TooltipTrigger
74 render={<span className="min-w-0 flex-1 truncate">{check.name}</span>}
75 />
76 <TooltipPopup side="top">{check.description ?? check.name}</TooltipPopup>
77 </Tooltip>
78 <span className="shrink-0 text-muted-foreground">
79 {pullRequestCheckStatusLabel(check)}
80 </span>
81 {check.url === null ? null : (
82 <button
83 type="button"
84 className="shrink-0 text-primary hover:underline"
85 onClick={() => {
86 if (!check.url) return;
87 void openLink(check.url).catch((error: unknown) => {
88 console.error(error);
89 toastManager.add({ type: "error", title: "Unable to open check details" });
90 });
91 }}
92 >
93 Details
94 </button>
95 )}
96 </li>
97 ))}
98 </ul>
99 );
101​
102/**
103 * The checks indicator and what it opens, in both places a change request is shown: a listing
104 * row, which knows only the rollup, and the detail header, which is already holding every check.
105 *
106 * `checks` decides between the two. Given them, nothing is read; without them, the popup reads
107 * the detail itself, which is why the row must also say which environment it came from.
108 */
109export function PullRequestChecksPopover({
110 checksState,
111 checks,
112 stale = false,
113 environmentId,
114 reference,
115 threadRef = null,
116 className,
117}: {
118 checksState: PullRequestChecksState;
119 /** The checks already in hand, for the detail header. Absent on a listing row. */
120 checks?: ReadonlyArray<PullRequestCheck>;
121 stale?: boolean;
122 environmentId?: EnvironmentId;
123 reference?: PullRequestRef;
124 /** Thread the popover sits beside; a listing row has none. */
125 threadRef?: ScopedThreadRef | null;
126 className?: string;
127}) {
128 const presentation = pullRequestChecksStatePresentation(checksState);
129 // Counts beat the rollup's own wording where they are known, the way GitHub's own header reads.
130 const summary = checks === undefined || stale ? null : summarizePullRequestChecks(checks);
131 return (
132 <Popover>
133 {/* A listing row is itself a button, so the trigger renders as a span: a nested button is
134 not valid inside one. The click is stopped here so opening the checks does not also
135 select the row it sits on. */}
136 <PopoverTrigger
137 nativeButton={false}
138 render={
139 <span
140 role="button"
141 tabIndex={0}
142 aria-label={`Checks: ${presentation.label}`}
143 className={cn("inline-flex shrink-0 cursor-pointer items-center", className)}
144 />
145 }
146 onClick={(event) => event.stopPropagation()}
147 >
148 <presentation.Icon aria-hidden className={cn("size-3.5", presentation.toneClassName)} />
149 </PopoverTrigger>
150 <PopoverPopup align="start" width="md" side="bottom">
151 <p className="mb-2 font-medium text-sm">{presentation.label}</p>
152 {summary === null ? null : <p className="mb-2 text-muted-foreground text-xs">{summary}</p>}
153 {stale ? (
154 <p className="text-muted-foreground text-xs">
155 Check details are out of date. Refresh the pull request to update them.
156 </p>
157 ) : checks !== undefined ? (
158 <ChecksBody checks={checks} threadRef={threadRef} />
159 ) : environmentId !== undefined && reference !== undefined ? (
160 <LazyChecksBody
161 environmentId={environmentId}
162 reference={reference}
163 threadRef={threadRef}
164 />
165 ) : null}
166 </PopoverPopup>
167 </Popover>
168 );