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

PullRequestChecksPopover.tsx

134 lines4.9 KB

1import type {
2 EnvironmentId,
3 PullRequestCheck,
4 PullRequestChecksState,
5 PullRequestRef,
6} from "@t3tools/contracts";
7​
8import { readLocalApi } from "~/localApi";
9import { cn } from "~/lib/utils";
10import { pullRequestEnvironment } from "~/state/pullRequests";
11import { useEnvironmentQuery } from "~/state/query";
12​
13import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
14import {
15 PullRequestCheckStatusIcon,
16 pullRequestCheckStatusLabel,
17 pullRequestChecksStatePresentation,
18 summarizePullRequestChecks,
19} from "./pullRequestPresentation";
20​
21/**
22 * The checks behind the rollup, for a row that only carries the rollup. Mounted by the popup, so
23 * the read starts when somebody opens it rather than once per row of a listing — the detail read
24 * is a request per pull request, and a page of them at rest would be a hundred.
25 */
26function LazyChecksBody({
27 environmentId,
28 reference,
29}: {
30 environmentId: EnvironmentId;
31 reference: PullRequestRef;
32}) {
33 const detailQuery = useEnvironmentQuery(
34 pullRequestEnvironment.detail({ environmentId, input: reference }),
35 );
36 if (detailQuery.error !== null) {
37 return <p className="text-muted-foreground text-xs">{detailQuery.error}</p>;
38 }
39 if (detailQuery.data === null) {
40 return (
41 <p className="text-muted-foreground text-xs">
42 {detailQuery.isPending ? "Loading checks…" : "No checks reported"}
43 </p>
44 );
45 }
46 return <ChecksBody checks={detailQuery.data.checks} />;
48​
49function ChecksBody({ checks }: { checks: ReadonlyArray<PullRequestCheck> }) {
50 if (checks.length === 0) {
51 return <p className="text-muted-foreground text-xs">No checks reported</p>;
52 }
53 return (
54 <ul className="flex flex-col gap-1">
55 {/* Keyed by position as well as by name: the host is the one that decides how many runs
56 share a name, and a repeated key is a rendering fault rather than a wrong list. */}
57 {checks.map((check, index) => (
58 <li key={`${index}:${check.name}`} className="flex items-center gap-2 text-xs">
59 <PullRequestCheckStatusIcon status={check.status} />
60 <span className="min-w-0 flex-1 truncate" title={check.description ?? check.name}>
61 {check.name}
62 </span>
63 <span className="shrink-0 text-muted-foreground">
64 {pullRequestCheckStatusLabel(check.status)}
65 </span>
66 {check.url === null ? null : (
67 <button
68 type="button"
69 className="shrink-0 text-primary hover:underline"
70 onClick={() => void readLocalApi()?.shell.openExternal(check.url ?? "")}
71 >
72 Details
73 </button>
74 )}
75 </li>
76 ))}
77 </ul>
78 );
80​
81/**
82 * The checks indicator and what it opens, in both places a change request is shown: a listing
83 * row, which knows only the rollup, and the detail header, which is already holding every check.
84 *
85 * `checks` decides between the two. Given them, nothing is read; without them, the popup reads
86 * the detail itself, which is why the row must also say which environment it came from.
87 */
88export function PullRequestChecksPopover({
89 checksState,
90 checks,
91 environmentId,
92 reference,
93 className,
94}: {
95 checksState: PullRequestChecksState;
96 /** The checks already in hand, for the detail header. Absent on a listing row. */
97 checks?: ReadonlyArray<PullRequestCheck>;
98 environmentId?: EnvironmentId;
99 reference?: PullRequestRef;
100 className?: string;
101}) {
102 const presentation = pullRequestChecksStatePresentation(checksState);
103 // Counts beat the rollup's own wording where they are known, the way GitHub's own header reads.
104 const summary = checks === undefined ? null : summarizePullRequestChecks(checks);
105 return (
106 <Popover>
107 {/* A listing row is itself a button, so the trigger renders as a span: a nested button is
108 not valid inside one. The click is stopped here so opening the checks does not also
109 select the row it sits on. */}
110 <PopoverTrigger
111 render={
112 <span
113 role="button"
114 tabIndex={0}
115 aria-label={`Checks: ${presentation.label}`}
116 className={cn("inline-flex shrink-0 cursor-pointer items-center", className)}
117 />
118 }
119 onClick={(event) => event.stopPropagation()}
120 >
121 <presentation.Icon aria-hidden className={cn("size-3.5", presentation.toneClassName)} />
122 </PopoverTrigger>
123 <PopoverPopup align="start" className="w-80 max-w-full" side="bottom">
124 <p className="mb-2 font-medium text-sm">{presentation.label}</p>
125 {summary === null ? null : <p className="mb-2 text-muted-foreground text-xs">{summary}</p>}
126 {checks !== undefined ? (
127 <ChecksBody checks={checks} />
128 ) : environmentId !== undefined && reference !== undefined ? (
129 <LazyChecksBody environmentId={environmentId} reference={reference} />
130 ) : null}
131 </PopoverPopup>
132 </Popover>
133 );