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