Open History to see the last commit
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} />;
57}
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 const hasDetails = visibleChecks.some((check) => check.url !== null);
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="-me-3 w-auto max-h-64">
78 <ul
79 className={cn(
80 "grid gap-x-2 gap-y-1 pe-3",
81 hasDetails
82 ? "grid-cols-[auto_minmax(0,1fr)_auto_auto]"
83 : "grid-cols-[auto_minmax(0,1fr)_auto]",
84 )}
85 >
86 {visibleChecks.map((check, index) => (
87 <li
88 key={`${index}:${check.name}`}
89 className="col-span-full grid grid-cols-subgrid items-center text-xs"
90 >
91 <PullRequestCheckStatusIcon status={check.status} />
92 <Tooltip>
93 <TooltipTrigger render={<span className="min-w-0 truncate">{check.name}</span>} />
94 <TooltipPopup side="top">{check.description ?? check.name}</TooltipPopup>
95 </Tooltip>
96 <span className="justify-self-end whitespace-nowrap text-muted-foreground">
97 {pullRequestCheckStatusLabel(check)}
98 </span>
99 {check.url === null ? null : (
100 <button
101 type="button"
102 className="text-primary hover:underline"
103 onClick={() => {
104 if (!check.url) return;
105 void openLink(check.url).catch((error: unknown) => {
106 console.error(error);
107 toastManager.add({ type: "error", title: "Unable to open check details" });
108 });
109 }}
110 >
111 Details
112 </button>
113 )}
114 </li>
115 ))}
116 </ul>
117 </ScrollArea>
118 {canCollapse ? (
119 <Button
120 variant="ghost"
121 size="xs"
122 aria-expanded={showAll}
123 onClick={() => setShowAll(!showAll)}
124 >
125 {showAll ? "Show less" : "Show all"}
126 </Button>
127 ) : null}
128 </>
129 );
130}
131
132export function PullRequestChecksPopover({
133 checksState,
134 checks,
135 stale = false,
136 environmentId,
137 reference,
138 threadRef = null,
139 variant = "icon",
140 className,
141 render,
142}: {
143 checksState: PullRequestChecksState;
144 checks?: ReadonlyArray<PullRequestCheck>;
145 stale?: boolean;
146 environmentId?: EnvironmentId;
147 reference?: PullRequestRef;
148 threadRef?: ScopedThreadRef | null;
149 variant?: "icon" | "count";
150 className?: string;
151 render?: React.ReactElement;
152}) {
153 const presentation = pullRequestChecksStatePresentation(checksState);
154 const incompleteCount =
155 checks?.filter((check) => check.status === "pending" || check.status === "action-required")
156 .length ?? 0;
157 const failedCount =
158 checks?.filter((check) => check.status === "failure" || check.status === "cancelled").length ??
159 0;
160 const summary = checks === undefined || stale ? null : summarizePullRequestChecks(checks);
161 return (
162 <Popover>
163 {/* A listing row is itself a button, so the trigger renders as a span: a nested button is
164 not valid inside one. The click is stopped here so opening the checks does not also
165 select the row it sits on. */}
166 <PopoverTrigger
167 nativeButton={variant === "count"}
168 aria-label={
169 variant === "count"
170 ? `Open checks: ${summary ?? presentation.label}`
171 : `Checks: ${presentation.label}`
172 }
173 render={
174 variant === "count" ? (
175 (render ?? <Button variant="ghost" size="sm" className={className} />)
176 ) : (
177 <span
178 role="button"
179 tabIndex={0}
180 className={cn("inline-flex shrink-0 cursor-pointer items-center", className)}
181 />
182 )
183 }
184 onClick={(event) => event.stopPropagation()}
185 >
186 <presentation.Icon aria-hidden className={cn("size-3.5", presentation.toneClassName)} />
187 {variant === "count" ? (
188 <>
189 {checks !== undefined && incompleteCount > 0 ? (
190 <span className="tabular-nums">
191 {checks.length - incompleteCount}/{checks.length}
192 </span>
193 ) : failedCount > 0 ? (
194 <span className="tabular-nums">{failedCount} failed</span>
195 ) : null}
196 </>
197 ) : null}
198 </PopoverTrigger>
199 <PopoverPopup align="start" width="md" side="bottom">
200 <p className="mb-2 font-medium text-sm">{presentation.label}</p>
201 {summary === null ? null : <p className="mb-2 text-muted-foreground text-xs">{summary}</p>}
202 {stale ? (
203 <p className="text-muted-foreground text-xs">
204 Check details are out of date. Refresh the pull request to update them.
205 </p>
206 ) : checks !== undefined ? (
207 <ChecksBody checks={checks} threadRef={threadRef} />
208 ) : environmentId !== undefined && reference !== undefined ? (
209 <LazyChecksBody
210 environmentId={environmentId}
211 reference={reference}
212 threadRef={threadRef}
213 />
214 ) : null}
215 </PopoverPopup>
216 </Popover>
217 );
218}