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

PullRequestChecksPopover.tsx

216 lines7.8 KB

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