Showing the changes to apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx (1 of 172 files). Show all
Unified · Split
| @@ -0,0 +1,296 @@ | ||
| 1 | +import type { ScopedThreadRef, ThreadPullRequestLink } from "@t3tools/contracts"; | |
| 2 | +import { | |
| 3 | + resolveThreadPullRequestChains, | |
| 4 | + visibleThreadPullRequests, | |
| 5 | +} from "@t3tools/shared/threadPullRequests"; | |
| 6 | +import { | |
| 7 | + GitPullRequestArrow, | |
| 8 | + LayersIcon, | |
| 9 | + LinkIcon, | |
| 10 | + MoreHorizontalIcon, | |
| 11 | + PlusIcon, | |
| 12 | +} from "lucide-react"; | |
| 13 | +import { useCallback, useMemo } from "react"; | |
| 14 | + | |
| 15 | +import { writeTextToClipboard } from "~/hooks/useCopyToClipboard"; | |
| 16 | +import { useOpenPrLink } from "~/lib/openPullRequestLink"; | |
| 17 | +import { cn } from "~/lib/utils"; | |
| 18 | +import { useServerConfigs, useThreadShell } from "~/state/entities"; | |
| 19 | +import { PullRequestsUnavailableState } from "./PullRequestsUnavailableState"; | |
| 20 | +import { threadEnvironment } from "~/state/threads"; | |
| 21 | +import { useAtomCommand } from "~/state/use-atom-command"; | |
| 22 | +import { formatRelativeTimeLabel } from "~/timestampFormat"; | |
| 23 | +import { Button } from "../ui/button"; | |
| 24 | +import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; | |
| 25 | +import { ScrollArea } from "../ui/scroll-area"; | |
| 26 | +import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; | |
| 27 | +import { openLinkPullRequestDialog } from "./LinkPullRequestDialog"; | |
| 28 | +import { pullRequestListLines, type PullRequestListLine } from "./pullRequestListLines"; | |
| 29 | +import { | |
| 30 | + PullRequestActorAvatar, | |
| 31 | + PullRequestDiffStat, | |
| 32 | + PullRequestStateGlyph, | |
| 33 | + pullRequestChecksStatePresentation, | |
| 34 | +} from "./pullRequestPresentation"; | |
| 35 | + | |
| 36 | +const SOURCE_LABELS: Record<ThreadPullRequestLink["source"], string> = { | |
| 37 | + manual: "Linked by you", | |
| 38 | + created: "Created from this thread", | |
| 39 | + agent: "Linked by the agent", | |
| 40 | + stack: "Found in the stack", | |
| 41 | + "stack-dismissed": "Dismissed", | |
| 42 | +}; | |
| 43 | + | |
| 44 | +function ChecksGlyph({ | |
| 45 | + state, | |
| 46 | +}: { | |
| 47 | + state: NonNullable<ThreadPullRequestLink["snapshot"]>["checksState"] & string; | |
| 48 | +}) { | |
| 49 | + const presentation = pullRequestChecksStatePresentation(state); | |
| 50 | + return ( | |
| 51 | + <Tooltip> | |
| 52 | + <TooltipTrigger render={<span className="inline-flex shrink-0" />}> | |
| 53 | + <presentation.Icon | |
| 54 | + role="img" | |
| 55 | + aria-label={presentation.label} | |
| 56 | + className={cn("size-3.5", presentation.toneClassName)} | |
| 57 | + /> | |
| 58 | + </TooltipTrigger> | |
| 59 | + <TooltipPopup>{presentation.label}</TooltipPopup> | |
| 60 | + </Tooltip> | |
| 61 | + ); | |
| 62 | +} | |
| 63 | + | |
| 64 | +function LinkRow({ | |
| 65 | + line, | |
| 66 | + threadRef, | |
| 67 | + onUnlink, | |
| 68 | +}: { | |
| 69 | + line: PullRequestListLine; | |
| 70 | + threadRef: ScopedThreadRef; | |
| 71 | + onUnlink: (link: ThreadPullRequestLink) => void; | |
| 72 | +}) { | |
| 73 | + const openPrLink = useOpenPrLink(threadRef); | |
| 74 | + const { link, depth, stack } = line; | |
| 75 | + const snapshot = link.snapshot; | |
| 76 | + return ( | |
| 77 | + <div | |
| 78 | + className="group/pr-row flex items-center gap-2 rounded-md py-1 pr-1 hover:bg-accent/60" | |
| 79 | + // Each layer steps in under the one it targets. The step is capped: beyond a few layers | |
| 80 | + // the indent only says "still in the stack", which the connector line already does, and | |
| 81 | + // a sixteen-layer stack would otherwise stair-step off the right edge. | |
| 82 | + style={{ paddingLeft: `${0.5 + Math.min(depth, 3) * 1.25}rem` }} | |
| 83 | + > | |
| 84 | + {depth > 0 ? <span aria-hidden className="-ml-2 h-6 w-px shrink-0 bg-border/70" /> : null} | |
| 85 | + {snapshot === null ? ( | |
| 86 | + <GitPullRequestArrow | |
| 87 | + aria-label="Waiting for host state" | |
| 88 | + className="size-4 shrink-0 text-muted-foreground" | |
| 89 | + /> | |
| 90 | + ) : ( | |
| 91 | + <PullRequestStateGlyph state={snapshot.state} isDraft={snapshot.isDraft} /> | |
| 92 | + )} | |
| 93 | + <a | |
| 94 | + href={link.url} | |
| 95 | + onClick={(event) => openPrLink(event, link.url, threadRef)} | |
| 96 | + className="min-w-0 flex-1" | |
| 97 | + > | |
| 98 | + <span className="flex min-w-0 items-center gap-1.5"> | |
| 99 | + <Tooltip> | |
| 100 | + <TooltipTrigger | |
| 101 | + render={ | |
| 102 | + <span className="shrink-0 font-mono text-xs tabular-nums text-muted-foreground" /> | |
| 103 | + } | |
| 104 | + > | |
| 105 | + #{link.number} | |
| 106 | + </TooltipTrigger> | |
| 107 | + <TooltipPopup> | |
| 108 | + {SOURCE_LABELS[link.source]} · {formatRelativeTimeLabel(link.linkedAt)} | |
| 109 | + </TooltipPopup> | |
| 110 | + </Tooltip> | |
| 111 | + <span className="min-w-0 flex-1 truncate text-sm"> | |
| 112 | + {snapshot?.title ?? link.repository} | |
| 113 | + </span> | |
| 114 | + {/* Right-aligned signals, in the order a reviewer scans them: are checks green, | |
| 115 | + has someone ruled, how big is it. Each is absent rather than neutral when the | |
| 116 | + host said nothing, so a row without them reads as unknown, not as fine. */} | |
| 117 | + <span className="ml-auto flex shrink-0 items-center gap-1.5 text-[11px]"> | |
| 118 | + {snapshot?.checksState ? <ChecksGlyph state={snapshot.checksState} /> : null} | |
| 119 | + {snapshot?.state === "open" && | |
| 120 | + (snapshot.reviewDecision === "approved" || | |
| 121 | + snapshot.reviewDecision === "changes-requested") ? ( | |
| 122 | + <span | |
| 123 | + className={cn( | |
| 124 | + snapshot.reviewDecision === "approved" | |
| 125 | + ? "text-emerald-600/90 dark:text-emerald-400/80" | |
| 126 | + : "text-amber-600/90 dark:text-amber-400/80", | |
| 127 | + )} | |
| 128 | + > | |
| 129 | + {snapshot.reviewDecision === "approved" ? "Approved" : "Changes requested"} | |
| 130 | + </span> | |
| 131 | + ) : null} | |
| 132 | + {snapshot?.state === "open" && snapshot.mergeability === "conflicting" ? ( | |
| 133 | + <span className="text-destructive">Conflicts</span> | |
| 134 | + ) : null} | |
| 135 | + <PullRequestDiffStat | |
| 136 | + additions={snapshot?.additions ?? 0} | |
| 137 | + deletions={snapshot?.deletions ?? 0} | |
| 138 | + className="font-mono" | |
| 139 | + /> | |
| 140 | + </span> | |
| 141 | + </span> | |
| 142 | + <span className="flex min-w-0 items-center gap-1.5 text-[11px] text-muted-foreground"> | |
| 143 | + {stack ? ( | |
| 144 | + <Tooltip> | |
| 145 | + <TooltipTrigger | |
| 146 | + render={ | |
| 147 | + <span className="inline-flex shrink-0 items-center gap-0.5 text-foreground/70" /> | |
| 148 | + } | |
| 149 | + > | |
| 150 | + {stack.kind === "native" ? ( | |
| 151 | + <LayersIcon aria-hidden className="size-3" /> | |
| 152 | + ) : ( | |
| 153 | + <GitPullRequestArrow aria-hidden className="size-3" /> | |
| 154 | + )} | |
| 155 | + {stack.size} | |
| 156 | + </TooltipTrigger> | |
| 157 | + <TooltipPopup> | |
| 158 | + {stack.kind === "native" | |
| 159 | + ? `GitHub stack of ${stack.size}: merging a layer lands the ones below it.` | |
| 160 | + : `${stack.size} pull requests chained by base branch.`} | |
| 161 | + </TooltipPopup> | |
| 162 | + </Tooltip> | |
| 163 | + ) : null} | |
| 164 | + {snapshot?.author ? ( | |
| 165 | + <span className="inline-flex shrink-0 items-center gap-1"> | |
| 166 | + <PullRequestActorAvatar actor={snapshot.author} className="size-3.5" /> | |
| 167 | + <span className="max-w-28 truncate">{snapshot.author.login}</span> | |
| 168 | + </span> | |
| 169 | + ) : null} | |
| 170 | + <span className="truncate font-mono"> | |
| 171 | + {snapshot !== null | |
| 172 | + ? `${snapshot.headBranch} → ${snapshot.baseBranch}` | |
| 173 | + : `${link.host}/${link.repository}`} | |
| 174 | + </span> | |
| 175 | + {snapshot?.updatedAt ? ( | |
| 176 | + <span className="shrink-0">· {formatRelativeTimeLabel(snapshot.updatedAt)}</span> | |
| 177 | + ) : null} | |
| 178 | + </span> | |
| 179 | + </a> | |
| 180 | + <Menu> | |
| 181 | + <MenuTrigger | |
| 182 | + render={ | |
| 183 | + <Button | |
| 184 | + variant="ghost" | |
| 185 | + size="icon-xs" | |
| 186 | + aria-label={`Actions for #${link.number}`} | |
| 187 | + className={cn( | |
| 188 | + "opacity-0 group-hover/pr-row:opacity-100 data-[popup-open]:opacity-100", | |
| 189 | + )} | |
| 190 | + > | |
| 191 | + <MoreHorizontalIcon className="size-3.5" /> | |
| 192 | + </Button> | |
| 193 | + } | |
| 194 | + /> | |
| 195 | + <MenuPopup align="end" side="bottom"> | |
| 196 | + <MenuItem onClick={() => void writeTextToClipboard(link.url, "link")}>Copy link</MenuItem> | |
| 197 | + <MenuItem onClick={(event) => openPrLink(event, link.url, threadRef)}>Open</MenuItem> | |
| 198 | + <MenuItem onClick={() => onUnlink(link)}> | |
| 199 | + {link.source === "stack" ? "Dismiss from thread" : "Unlink from thread"} | |
| 200 | + </MenuItem> | |
| 201 | + </MenuPopup> | |
| 202 | + </Menu> | |
| 203 | + </div> | |
| 204 | + ); | |
| 205 | +} | |
| 206 | + | |
| 207 | +export function ThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThreadRef }) { | |
| 208 | + const configs = useServerConfigs(); | |
| 209 | + if (configs.get(threadRef.environmentId)?.environment.capabilities.threadPullRequests !== true) { | |
| 210 | + return ( | |
| 211 | + <PullRequestsUnavailableState | |
| 212 | + title="Linked pull requests unavailable" | |
| 213 | + error="This environment does not support multiple linked pull requests." | |
| 214 | + /> | |
| 215 | + ); | |
| 216 | + } | |
| 217 | + return <EnabledThreadPullRequestsPanel threadRef={threadRef} />; | |
| 218 | +} | |
| 219 | + | |
| 220 | +function EnabledThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThreadRef }) { | |
| 221 | + const thread = useThreadShell(threadRef); | |
| 222 | + const openLinkDialog = useCallback(() => openLinkPullRequestDialog(threadRef), [threadRef]); | |
| 223 | + const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: true }); | |
| 224 | + const links = useMemo(() => visibleThreadPullRequests(thread?.pullRequests ?? []), [thread]); | |
| 225 | + const lines = useMemo(() => pullRequestListLines(resolveThreadPullRequestChains(links)), [links]); | |
| 226 | + const handleUnlink = useCallback( | |
| 227 | + (link: ThreadPullRequestLink) => { | |
| 228 | + void unlink({ | |
| 229 | + environmentId: threadRef.environmentId, | |
| 230 | + input: { | |
| 231 | + threadId: threadRef.threadId, | |
| 232 | + host: link.host, | |
| 233 | + repository: link.repository, | |
| 234 | + number: link.number, | |
| 235 | + }, | |
| 236 | + }); | |
| 237 | + }, | |
| 238 | + [threadRef, unlink], | |
| 239 | + ); | |
| 240 | + const openCount = useMemo( | |
| 241 | + () => links.filter((link) => link.snapshot === null || link.snapshot.state === "open").length, | |
| 242 | + [links], | |
| 243 | + ); | |
| 244 | + const lastSynced = useMemo(() => { | |
| 245 | + let latest: string | null = null; | |
| 246 | + for (const link of links) { | |
| 247 | + const at = link.snapshot?.syncedAt; | |
| 248 | + if (at !== undefined && (latest === null || at > latest)) latest = at; | |
| 249 | + } | |
| 250 | + return latest; | |
| 251 | + }, [links]); | |
| 252 | + | |
| 253 | + if (links.length === 0) { | |
| 254 | + return ( | |
| 255 | + <div className="flex h-full flex-col items-center justify-center gap-2 p-6 text-center"> | |
| 256 | + <LinkIcon aria-hidden className="size-6 text-muted-foreground/60" /> | |
| 257 | + <p className="text-sm font-medium">No linked pull requests</p> | |
| 258 | + <p className="max-w-60 text-xs text-muted-foreground"> | |
| 259 | + Pull requests the agent opens from this thread land here. Link one yourself from a URL or | |
| 260 | + a number. | |
| 261 | + </p> | |
| 262 | + <Button size="sm" variant="outline" onClick={openLinkDialog}> | |
| 263 | + <PlusIcon className="size-3.5" /> | |
| 264 | + Link pull request | |
| 265 | + </Button> | |
| 266 | + </div> | |
| 267 | + ); | |
| 268 | + } | |
| 269 | + | |
| 270 | + return ( | |
| 271 | + <div className="flex h-full min-h-0 flex-col"> | |
| 272 | + <ScrollArea className="min-h-0 flex-1"> | |
| 273 | + <div className="flex flex-col p-1.5"> | |
| 274 | + {lines.map((line) => ( | |
| 275 | + <LinkRow | |
| 276 | + key={`${line.link.host}/${line.link.repository}#${line.link.number}`} | |
| 277 | + line={line} | |
| 278 | + threadRef={threadRef} | |
| 279 | + onUnlink={handleUnlink} | |
| 280 | + /> | |
| 281 | + ))} | |
| 282 | + </div> | |
| 283 | + </ScrollArea> | |
| 284 | + <footer className="flex items-center justify-between border-t border-border/60 px-2 py-1.5 text-[.7rem] text-muted-foreground"> | |
| 285 | + <span> | |
| 286 | + {openCount} open · {links.length} linked | |
| 287 | + {lastSynced ? ` · synced ${formatRelativeTimeLabel(lastSynced)}` : ""} | |
| 288 | + </span> | |
| 289 | + <Button size="xs" variant="ghost" onClick={openLinkDialog}> | |
| 290 | + <PlusIcon className="size-3.5" /> | |
| 291 | + Link | |
| 292 | + </Button> | |
| 293 | + </footer> | |
| 294 | + </div> | |
| 295 | + ); | |
| 296 | +} | |