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