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