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

PullRequestChecksPopover.tsx

208 lines7.0 KB

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