The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type * as React from "react"; |
| 2 | import type { |
| 3 | EnvironmentId, |
| 4 | PullRequestCheck, |
| 5 | PullRequestChecksState, |
| 6 | PullRequestRef, |
| 7 | ScopedThreadRef, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { msg, T, useGT, useMessages, Var } from "gt-react"; |
| 10 | import { useState } from "react"; |
| 11 | |
| 12 | import { useOpenLink } from "~/browser/useOpenLink"; |
| 13 | import { m } from "~/i18n/messages"; |
| 14 | import { cn } from "~/lib/utils"; |
| 15 | import { pullRequestEnvironment } from "~/state/pullRequests"; |
| 16 | import { useEnvironmentQuery } from "~/state/query"; |
| 17 | |
| 18 | import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover"; |
| 19 | import { Button } from "../ui/button"; |
| 20 | import { ScrollArea } from "../ui/scroll-area"; |
| 21 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 22 | import { toastManager } from "../ui/toast"; |
| 23 | import { groupPullRequestChecks } from "./pullRequestDetail.logic"; |
| 24 | import { |
| 25 | PullRequestCheckStatusIcon, |
| 26 | pullRequestCheckStatusLabel, |
| 27 | pullRequestChecksStatePresentation, |
| 28 | summarizePullRequestChecks, |
| 29 | } from "./pullRequestPresentation"; |
| 30 | |
| 31 | // Fetch only on open to avoid per-row requests. |
| 32 | function LazyChecksBody({ |
| 33 | environmentId, |
| 34 | reference, |
| 35 | threadRef, |
| 36 | }: { |
| 37 | environmentId: EnvironmentId; |
| 38 | reference: PullRequestRef; |
| 39 | threadRef: ScopedThreadRef | null; |
| 40 | }) { |
| 41 | const gt = useGT(); |
| 42 | const detailQuery = useEnvironmentQuery( |
| 43 | pullRequestEnvironment.detail({ environmentId, input: reference }), |
| 44 | useMessages(), |
| 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 ? gt("Loading checks…") : gt("No checks reported")} |
| 53 | </p> |
| 54 | ); |
| 55 | } |
| 56 | return <ChecksBody checks={detailQuery.data.checks} threadRef={threadRef} />; |
| 57 | } |
| 58 | |
| 59 | function ChecksBody({ |
| 60 | checks, |
| 61 | threadRef, |
| 62 | }: { |
| 63 | checks: ReadonlyArray<PullRequestCheck>; |
| 64 | threadRef: ScopedThreadRef | null; |
| 65 | }) { |
| 66 | const gt = useGT(); |
| 67 | const resolve = useMessages(); |
| 68 | const openLink = useOpenLink(threadRef); |
| 69 | const [showAll, setShowAll] = useState(false); |
| 70 | const { attention, running, completed } = groupPullRequestChecks(checks); |
| 71 | const canCollapse = attention.length + running.length > 0 && completed.length > 0; |
| 72 | const visibleChecks = [...attention, ...running, ...(showAll || !canCollapse ? completed : [])]; |
| 73 | const hasDetails = visibleChecks.some((check) => check.url !== null); |
| 74 | if (checks.length === 0) { |
| 75 | return ( |
| 76 | <p className="text-muted-foreground text-xs"> |
| 77 | <T>No checks reported</T> |
| 78 | </p> |
| 79 | ); |
| 80 | } |
| 81 | return ( |
| 82 | <> |
| 83 | <ScrollArea className="-me-3 w-auto max-h-64"> |
| 84 | <ul |
| 85 | className={cn( |
| 86 | "grid gap-x-2 gap-y-1 pe-3", |
| 87 | hasDetails |
| 88 | ? "grid-cols-[auto_minmax(0,1fr)_auto_auto]" |
| 89 | : "grid-cols-[auto_minmax(0,1fr)_auto]", |
| 90 | )} |
| 91 | > |
| 92 | {visibleChecks.map( |
| 93 | (check, index) => ( |
| 94 | /* oxlint-disable react/no-array-index-key -- Check names can repeat; these status rows have no local state. */ <li |
| 95 | key={`${index}:${check.name}`} |
| 96 | className="col-span-full grid grid-cols-subgrid items-center text-xs" |
| 97 | > |
| 98 | <PullRequestCheckStatusIcon status={check.status} /> |
| 99 | <Tooltip> |
| 100 | <TooltipTrigger render={<span className="min-w-0 truncate">{check.name}</span>} /> |
| 101 | <TooltipPopup side="top">{check.description ?? check.name}</TooltipPopup> |
| 102 | </Tooltip> |
| 103 | <span className="justify-self-end whitespace-nowrap text-muted-foreground"> |
| 104 | {pullRequestCheckStatusLabel(check, resolve)} |
| 105 | </span> |
| 106 | {check.url === null ? null : ( |
| 107 | <button |
| 108 | type="button" |
| 109 | className="text-primary hover:underline" |
| 110 | onClick={() => { |
| 111 | if (!check.url) return; |
| 112 | void openLink(check.url).catch((error: unknown) => { |
| 113 | console.error(error); |
| 114 | toastManager.add({ |
| 115 | type: "error", |
| 116 | title: m(msg("Unable to open check details")), |
| 117 | }); |
| 118 | }); |
| 119 | }} |
| 120 | > |
| 121 | <T>Details</T> |
| 122 | </button> |
| 123 | )} |
| 124 | </li> |
| 125 | ) /* oxlint-enable react/no-array-index-key */, |
| 126 | )} |
| 127 | </ul> |
| 128 | </ScrollArea> |
| 129 | {canCollapse ? ( |
| 130 | <Button |
| 131 | variant="ghost" |
| 132 | size="xs" |
| 133 | aria-expanded={showAll} |
| 134 | onClick={() => setShowAll(!showAll)} |
| 135 | > |
| 136 | {showAll ? gt("Show less") : gt("Show all")} |
| 137 | </Button> |
| 138 | ) : null} |
| 139 | </> |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | export function PullRequestChecksPopover({ |
| 144 | checksState, |
| 145 | checks, |
| 146 | stale = false, |
| 147 | environmentId, |
| 148 | reference, |
| 149 | threadRef = null, |
| 150 | variant = "icon", |
| 151 | className, |
| 152 | render, |
| 153 | }: { |
| 154 | checksState: PullRequestChecksState; |
| 155 | checks?: ReadonlyArray<PullRequestCheck>; |
| 156 | stale?: boolean; |
| 157 | environmentId?: EnvironmentId; |
| 158 | reference?: PullRequestRef; |
| 159 | threadRef?: ScopedThreadRef | null; |
| 160 | variant?: "icon" | "count"; |
| 161 | className?: string; |
| 162 | render?: React.ReactElement; |
| 163 | }) { |
| 164 | const gt = useGT(); |
| 165 | const resolve = useMessages(); |
| 166 | const presentation = pullRequestChecksStatePresentation(checksState, resolve); |
| 167 | const incompleteCount = |
| 168 | checks?.filter((check) => check.status === "pending" || check.status === "action-required") |
| 169 | .length ?? 0; |
| 170 | const failedCount = |
| 171 | checks?.filter((check) => check.status === "failure" || check.status === "cancelled").length ?? |
| 172 | 0; |
| 173 | const summary = |
| 174 | checks === undefined || stale ? null : summarizePullRequestChecks(checks, resolve); |
| 175 | return ( |
| 176 | <Popover> |
| 177 | <PopoverTrigger |
| 178 | nativeButton={variant === "count"} |
| 179 | aria-label={ |
| 180 | variant === "count" |
| 181 | ? gt("Open checks: {summary}", { summary: summary ?? presentation.label }) |
| 182 | : gt("Checks: {summary}", { summary: presentation.label }) |
| 183 | } |
| 184 | render={ |
| 185 | variant === "count" ? ( |
| 186 | (render ?? <Button variant="ghost" size="sm" className={className} />) |
| 187 | ) : ( |
| 188 | <span |
| 189 | role="button" |
| 190 | tabIndex={0} |
| 191 | className={cn("inline-flex shrink-0 cursor-pointer items-center", className)} |
| 192 | /> |
| 193 | ) |
| 194 | } |
| 195 | onClick={(event) => event.stopPropagation()} |
| 196 | > |
| 197 | <presentation.Icon aria-hidden className={cn("size-3.5", presentation.toneClassName)} /> |
| 198 | {variant === "count" ? ( |
| 199 | <> |
| 200 | {checks !== undefined && incompleteCount > 0 ? ( |
| 201 | // Clip the second line so "checks" disappears whole when the count must shrink. |
| 202 | <span className="h-lh min-w-0 overflow-hidden text-left whitespace-normal tabular-nums"> |
| 203 | <T> |
| 204 | <span className="whitespace-nowrap"> |
| 205 | <Var>{checks.length - incompleteCount}</Var>/<Var>{checks.length}</Var> |
| 206 | </span>{" "} |
| 207 | checks |
| 208 | </T> |
| 209 | </span> |
| 210 | ) : failedCount > 0 ? ( |
| 211 | <span className="min-w-0 truncate tabular-nums"> |
| 212 | {gt("{count, plural, one {{count} failed} other {{count} failed}}", { |
| 213 | count: failedCount, |
| 214 | })} |
| 215 | </span> |
| 216 | ) : null} |
| 217 | </> |
| 218 | ) : null} |
| 219 | </PopoverTrigger> |
| 220 | <PopoverPopup align="start" width="md" side="bottom"> |
| 221 | <p className="mb-2 font-medium text-sm">{presentation.label}</p> |
| 222 | {summary === null ? null : <p className="mb-2 text-muted-foreground text-xs">{summary}</p>} |
| 223 | {stale ? ( |
| 224 | <p className="text-muted-foreground text-xs"> |
| 225 | <T>Check details are out of date. Refresh the pull request to update them.</T> |
| 226 | </p> |
| 227 | ) : checks !== undefined ? ( |
| 228 | <ChecksBody checks={checks} threadRef={threadRef} /> |
| 229 | ) : environmentId !== undefined && reference !== undefined ? ( |
| 230 | <LazyChecksBody |
| 231 | environmentId={environmentId} |
| 232 | reference={reference} |
| 233 | threadRef={threadRef} |
| 234 | /> |
| 235 | ) : null} |
| 236 | </PopoverPopup> |
| 237 | </Popover> |
| 238 | ); |
| 239 | } |