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

PullRequestChecksPopover.tsx

217 lines7.3 KB

1import type * as React from "react";
2import type {
3 EnvironmentId,
4 PullRequestCheck,
5 PullRequestChecksState,
6 PullRequestRef,
7 ScopedThreadRef,
8} from "@t3tools/contracts";
9import { msg, T, useGT, useMessages } from "gt-react";
10import { useState } from "react";
11​
12import { useOpenLink } from "~/browser/useOpenLink";
13import { m } from "~/i18n/messages";
14import { cn } from "~/lib/utils";
15import { pullRequestEnvironment } from "~/state/pullRequests";
16import { useEnvironmentQuery } from "~/state/query";
17​
18import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
19import { Button } from "../ui/button";
20import { ScrollArea } from "../ui/scroll-area";
21import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
22import { toastManager } from "../ui/toast";
23import { groupPullRequestChecks } from "./pullRequestDetail.logic";
24import {
25 PullRequestCheckStatusIcon,
26 pullRequestChecksCountLabel,
27 pullRequestCheckStatusLabel,
28 pullRequestChecksStatePresentation,
29 summarizePullRequestChecks,
30} from "./pullRequestPresentation";
31​
32// Fetch only on open to avoid per-row requests.
33function LazyChecksBody({
34 environmentId,
35 reference,
36 threadRef,
37}: {
38 environmentId: EnvironmentId;
39 reference: PullRequestRef;
40 threadRef: ScopedThreadRef | null;
41}) {
42 const gt = useGT();
43 const detailQuery = useEnvironmentQuery(
44 pullRequestEnvironment.detail({ environmentId, input: reference }),
45 useMessages(),
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 ? gt("Loading checks…") : gt("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 gt = useGT();
68 const resolve = useMessages();
69 const openLink = useOpenLink(threadRef);
70 const [showAll, setShowAll] = useState(false);
71 const { attention, running, completed } = groupPullRequestChecks(checks);
72 const canCollapse = attention.length + running.length > 0 && completed.length > 0;
73 const visibleChecks = [...attention, ...running, ...(showAll || !canCollapse ? completed : [])];
74 const hasDetails = visibleChecks.some((check) => check.url !== null);
75 if (checks.length === 0) {
76 return (
77 <p className="text-muted-foreground text-xs">
78 <T>No checks reported</T>
79 </p>
80 );
81 }
82 return (
83 <>
84 <ScrollArea className="-me-3 w-auto max-h-64">
85 <ul
86 className={cn(
87 "grid gap-x-2 gap-y-1 pe-3",
88 hasDetails
89 ? "grid-cols-[auto_minmax(0,1fr)_auto_auto]"
90 : "grid-cols-[auto_minmax(0,1fr)_auto]",
91 )}
92 >
93 {visibleChecks.map(
94 (check, index) => (
95 /* oxlint-disable react/no-array-index-key -- Check names can repeat; these status rows have no local state. */ <li
96 key={`${index}:${check.name}`}
97 className="col-span-full grid grid-cols-subgrid items-center text-xs"
98 >
99 <PullRequestCheckStatusIcon status={check.status} />
100 <Tooltip>
101 <TooltipTrigger render={<span className="min-w-0 truncate">{check.name}</span>} />
102 <TooltipPopup side="top">{check.description ?? check.name}</TooltipPopup>
103 </Tooltip>
104 <span className="justify-self-end whitespace-nowrap text-muted-foreground">
105 {pullRequestCheckStatusLabel(check, resolve)}
106 </span>
107 {check.url === null ? null : (
108 <button
109 type="button"
110 className="text-primary hover:underline"
111 onClick={() => {
112 if (!check.url) return;
113 void openLink(check.url).catch((error: unknown) => {
114 console.error(error);
115 toastManager.add({
116 type: "error",
117 title: m(msg("Unable to open check details")),
118 });
119 });
120 }}
121 >
122 <T>Details</T>
123 </button>
124 )}
125 </li>
126 ) /* oxlint-enable react/no-array-index-key */,
127 )}
128 </ul>
129 </ScrollArea>
130 {canCollapse ? (
131 <Button
132 variant="ghost"
133 size="xs"
134 aria-expanded={showAll}
135 onClick={() => setShowAll(!showAll)}
136 >
137 {showAll ? gt("Show less") : gt("Show all")}
138 </Button>
139 ) : null}
140 </>
141 );
143​
144export function PullRequestChecksPopover({
145 checksState,
146 checks,
147 stale = false,
148 environmentId,
149 reference,
150 threadRef = null,
151 variant = "icon",
152 className,
153 render,
154}: {
155 checksState: PullRequestChecksState;
156 checks?: ReadonlyArray<PullRequestCheck>;
157 stale?: boolean;
158 environmentId?: EnvironmentId;
159 reference?: PullRequestRef;
160 threadRef?: ScopedThreadRef | null;
161 variant?: "icon" | "count";
162 className?: string;
163 render?: React.ReactElement;
164}) {
165 const gt = useGT();
166 const resolve = useMessages();
167 const presentation = pullRequestChecksStatePresentation(checksState, resolve);
168 const countLabel = checks === undefined ? null : pullRequestChecksCountLabel(checks, resolve);
169 const summary =
170 checks === undefined || stale ? null : summarizePullRequestChecks(checks, resolve);
171 return (
172 <Popover>
173 <PopoverTrigger
174 nativeButton={variant === "count"}
175 aria-label={
176 variant === "count"
177 ? gt("Open checks: {summary}", { summary: summary ?? presentation.label })
178 : gt("Checks: {summary}", { summary: presentation.label })
179 }
180 render={
181 variant === "count" ? (
182 (render ?? <Button variant="ghost" size="sm" className={className} />)
183 ) : (
184 <span
185 role="button"
186 tabIndex={0}
187 className={cn("inline-flex shrink-0 cursor-pointer items-center", className)}
188 />
189 )
190 }
191 onClick={(event) => event.stopPropagation()}
192 >
193 <presentation.Icon aria-hidden className={cn("size-3.5", presentation.toneClassName)} />
194 {variant === "count" && countLabel !== null ? (
195 <span className="min-w-0 truncate tabular-nums">{countLabel}</span>
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 <T>Check details are out of date. Refresh the pull request to update them.</T>
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 );