import type { ScopedThreadRef, ThreadPullRequestLink } from "@t3tools/contracts"; import { resolveThreadPullRequestChains, visibleThreadPullRequests, } from "@t3tools/shared/threadPullRequests"; import { ArrowUpRightIcon, LinkIcon, MoreHorizontalIcon, PlusIcon } from "lucide-react"; import { useCallback, useMemo } from "react"; import { writeTextToClipboard } from "~/hooks/useCopyToClipboard"; import { useOpenPrLink } from "~/lib/openPullRequestLink"; import { cn } from "~/lib/utils"; import { useServerConfigs, useThreadShell } from "~/state/entities"; import { PullRequestsUnavailableState } from "./PullRequestsUnavailableState"; import { threadEnvironment } from "~/state/threads"; import { useAtomCommand } from "~/state/use-atom-command"; import { formatRelativeTimeLabel } from "~/timestampFormat"; import { Button } from "../ui/button"; import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; import { ScrollArea } from "../ui/scroll-area"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { openLinkPullRequestDialog } from "./LinkPullRequestDialog"; import { pullRequestListLines, type PullRequestListLine } from "./pullRequestListLines"; import { PULL_REQUEST_ROW_CLASS, PULL_REQUEST_ROW_NUMBER_CLASS, PullRequestRowAuthor, PullRequestRowBranches, PullRequestRowGlyph, PullRequestRowLines, } from "./PullRequestListRow"; import { PullRequestDiffStat, PullRequestReviewDecisionGlyph, pullRequestChecksStatePresentation, } from "./pullRequestPresentation"; import { PullRequestGlyph } from "./pullRequestIcons"; const SOURCE_LABELS: Record = { manual: "Linked by you", created: "Created from this thread", agent: "Linked by the agent", stack: "Found in the stack", "stack-dismissed": "Dismissed", }; function ChecksGlyph({ state, }: { state: NonNullable["checksState"] & string; }) { const presentation = pullRequestChecksStatePresentation(state); return ( }> {presentation.label} ); } function LinkRow({ line, threadRef, onUnlink, }: { line: PullRequestListLine; threadRef: ScopedThreadRef; onUnlink: (link: ThreadPullRequestLink) => void; }) { const openPrLink = useOpenPrLink(threadRef); const { link, depth, stack } = line; const snapshot = link.snapshot; return (
{depth > 0 ? : null} {snapshot === null ? ( ) : ( )} openPrLink(event, link.url, threadRef)} className="flex min-w-0 flex-1" > }> #{link.number} {SOURCE_LABELS[link.source]} · {formatRelativeTimeLabel(link.linkedAt)} } title={snapshot?.title ?? link.repository} signals={ snapshot?.state === "open" ? ( <> {snapshot.checksState ? : null} {snapshot.reviewDecision ? ( ) : null} ) : null } // Match the full PR list: diff counts up top, checks under the lifecycle glyph, the // verdict by the author. Each is absent rather than neutral when the host said // nothing, so a row without them reads as unknown, not as fine. status={ } meta={ <> {stack ? ( } > {stack.size} {stack.kind === "native" ? `GitHub stack of ${stack.size}: merging a layer lands the ones below it.` : `${stack.size} pull requests chained by base branch.`} ) : null} {snapshot?.author ? ( ) : null} {snapshot !== null ? ( ) : ( {link.host}/{link.repository} )} } updatedAt={snapshot?.updatedAt} /> {/* Out of the row's flow, so no row reserves a column for a button only the hovered one shows. It sits over the right end of the second line on the row's own hover color, fading in from the left, so it covers the time and leaves the diff counts alone. */}
); } export function ThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThreadRef }) { const configs = useServerConfigs(); if (configs.get(threadRef.environmentId)?.environment.capabilities.threadPullRequests !== true) { return ( ); } return ; } function EnabledThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThreadRef }) { const thread = useThreadShell(threadRef); const openLinkDialog = useCallback(() => openLinkPullRequestDialog(threadRef), [threadRef]); const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: true }); const links = useMemo(() => visibleThreadPullRequests(thread?.pullRequests ?? []), [thread]); const lines = useMemo(() => pullRequestListLines(resolveThreadPullRequestChains(links)), [links]); const handleUnlink = useCallback( (link: ThreadPullRequestLink) => { void unlink({ environmentId: threadRef.environmentId, input: { threadId: threadRef.threadId, host: link.host, repository: link.repository, number: link.number, }, }); }, [threadRef, unlink], ); const openCount = useMemo( () => links.filter((link) => link.snapshot === null || link.snapshot.state === "open").length, [links], ); const lastSynced = useMemo(() => { let latest: string | null = null; for (const link of links) { const at = link.snapshot?.syncedAt; if (at !== undefined && (latest === null || at > latest)) latest = at; } return latest; }, [links]); if (links.length === 0) { return (

No linked pull requests

Pull requests the agent opens from this thread land here. Link one yourself from a URL or a number.

); } return (
{lines.map((line) => ( ))}
); }