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

PullRequestChecksPopover.tsx

217 lines7.5 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// Fetch only on open to avoid per-row requests.
30function LazyChecksBody({
31 environmentId,
32 reference,
33 threadRef,
34}: {
35 environmentId: EnvironmentId;
36 reference: PullRequestRef;
37 threadRef: ScopedThreadRef | null;
38}) {
39 const detailQuery = useEnvironmentQuery(
40 pullRequestEnvironment.detail({ environmentId, input: reference }),
41 );
42 if (detailQuery.error !== null) {
43 return <p className="text-muted-foreground text-xs">{detailQuery.error}</p>;
44 }
45 if (detailQuery.data === null) {
46 return (
47 <p className="text-muted-foreground text-xs">
48 {detailQuery.isPending ? "Loading checks…" : "No checks reported"}
49 </p>
50 );
51 }
52 return <ChecksBody checks={detailQuery.data.checks} threadRef={threadRef} />;
54​
55function ChecksBody({
56 checks,
57 threadRef,
58}: {
59 checks: ReadonlyArray<PullRequestCheck>;
60 threadRef: ScopedThreadRef | null;
61}) {
62 const openLink = useOpenLink(threadRef);
63 const [showAll, setShowAll] = useState(false);
64 const { attention, running, completed } = groupPullRequestChecks(checks);
65 const canCollapse = attention.length + running.length > 0 && completed.length > 0;
66 const visibleChecks = [...attention, ...running, ...(showAll || !canCollapse ? completed : [])];
67 const hasDetails = visibleChecks.some((check) => check.url !== null);
68 if (checks.length === 0) {
69 return <p className="text-muted-foreground text-xs">No checks reported</p>;
70 }
71 return (
72 <>
73 <ScrollArea className="-me-3 w-auto max-h-64">
74 <ul
75 className={cn(
76 "grid gap-x-2 gap-y-1 pe-3",
77 hasDetails
78 ? "grid-cols-[auto_minmax(0,1fr)_auto_auto]"
79 : "grid-cols-[auto_minmax(0,1fr)_auto]",
80 )}
81 >
82 {visibleChecks.map(
83 (check, index) => (
84 /* oxlint-disable react/no-array-index-key -- Check names can repeat; these status rows have no local state. */ <li
85 key={`${index}:${check.name}`}
86 className="col-span-full grid grid-cols-subgrid items-center text-xs"
87 >
88 <PullRequestCheckStatusIcon status={check.status} />
89 <Tooltip>
90 <TooltipTrigger render={<span className="min-w-0 truncate">{check.name}</span>} />
91 <TooltipPopup side="top">{check.description ?? check.name}</TooltipPopup>
92 </Tooltip>
93 <span className="justify-self-end whitespace-nowrap text-muted-foreground">
94 {pullRequestCheckStatusLabel(check)}
95 </span>
96 {check.url === null ? null : (
97 <button
98 type="button"
99 className="text-primary hover:underline"
100 onClick={() => {
101 if (!check.url) return;
102 void openLink(check.url).catch((error: unknown) => {
103 console.error(error);
104 toastManager.add({ type: "error", title: "Unable to open check details" });
105 });
106 }}
107 >
108 Details
109 </button>
110 )}
111 </li>
112 ) /* oxlint-enable react/no-array-index-key */,
113 )}
114 </ul>
115 </ScrollArea>
116 {canCollapse ? (
117 <Button
118 variant="ghost"
119 size="xs"
120 aria-expanded={showAll}
121 onClick={() => setShowAll(!showAll)}
122 >
123 {showAll ? "Show less" : "Show all"}
124 </Button>
125 ) : null}
126 </>
127 );
129​
130export function PullRequestChecksPopover({
131 checksState,
132 checks,
133 stale = false,
134 environmentId,
135 reference,
136 threadRef = null,
137 variant = "icon",
138 className,
139 render,
140}: {
141 checksState: PullRequestChecksState;
142 checks?: ReadonlyArray<PullRequestCheck>;
143 stale?: boolean;
144 environmentId?: EnvironmentId;
145 reference?: PullRequestRef;
146 threadRef?: ScopedThreadRef | null;
147 variant?: "icon" | "count";
148 className?: string;
149 render?: React.ReactElement;
150}) {
151 const presentation = pullRequestChecksStatePresentation(checksState);
152 const incompleteCount =
153 checks?.filter((check) => check.status === "pending" || check.status === "action-required")
154 .length ?? 0;
155 const failedCount =
156 checks?.filter((check) => check.status === "failure" || check.status === "cancelled").length ??
157 0;
158 const summary = checks === undefined || stale ? null : summarizePullRequestChecks(checks);
159 return (
160 <Popover>
161 <PopoverTrigger
162 nativeButton={variant === "count"}
163 aria-label={
164 variant === "count"
165 ? `Open checks: ${summary ?? presentation.label}`
166 : `Checks: ${presentation.label}`
167 }
168 render={
169 variant === "count" ? (
170 (render ?? <Button variant="ghost" size="sm" className={className} />)
171 ) : (
172 <span
173 role="button"
174 tabIndex={0}
175 className={cn("inline-flex shrink-0 cursor-pointer items-center", className)}
176 />
177 )
178 }
179 onClick={(event) => event.stopPropagation()}
180 >
181 <presentation.Icon aria-hidden className={cn("size-3.5", presentation.toneClassName)} />
182 {variant === "count" ? (
183 <>
184 {checks !== undefined && incompleteCount > 0 ? (
185 // Clip the second line so "checks" disappears whole when the count must shrink.
186 <span className="h-lh min-w-0 overflow-hidden text-left whitespace-normal tabular-nums">
187 <span className="whitespace-nowrap">
188 {checks.length - incompleteCount}/{checks.length}
189 </span>{" "}
190 checks
191 </span>
192 ) : failedCount > 0 ? (
193 <span className="min-w-0 truncate tabular-nums">{failedCount} failed</span>
194 ) : null}
195 </>
196 ) : null}
197 </PopoverTrigger>
198 <PopoverPopup align="start" width="md" side="bottom">
199 <p className="mb-2 font-medium text-sm">{presentation.label}</p>
200 {summary === null ? null : <p className="mb-2 text-muted-foreground text-xs">{summary}</p>}
201 {stale ? (
202 <p className="text-muted-foreground text-xs">
203 Check details are out of date. Refresh the pull request to update them.
204 </p>
205 ) : checks !== undefined ? (
206 <ChecksBody checks={checks} threadRef={threadRef} />
207 ) : environmentId !== undefined && reference !== undefined ? (
208 <LazyChecksBody
209 environmentId={environmentId}
210 reference={reference}
211 threadRef={threadRef}
212 />
213 ) : null}
214 </PopoverPopup>
215 </Popover>
216 );