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