import { ThreadDetailsControl } from "./ThreadDetailsControl"; import { PullRequestGlyph } from "../pullRequest/pullRequestIcons"; /** * The thread details panel's pull request row: what the thread's pull request is, and the one * thing worth doing to it right now. * * The row itself opens the pull request in the right panel, exactly as it always has. Once the * host answers, the title gets the full first line and a second line carries the number, checks, * watch, and a single action slot, ranked by what unblocks the merge next: "Resolve" on * conflicts, "Ready" on a draft, "Fix" under failing checks, and "Merge" only once the branch is * clean and its checks pass. While checks run the slot reports * that instead — a merge offered mid-run would race the very runs that gate it. Everything else * the host knows (title, state, checks tally, diff size) lives in the row's tooltip, so a hover * answers what previously took opening the panel. * * Until the detail arrives — or where pull requests are not supported at all — the row renders * from the linked snapshot or branch summary, or just the link when status is unavailable. */ import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import type { EnvironmentId, ProjectId, PullRequestRef } from "@t3tools/contracts"; import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl"; import { ArrowUpRightIcon, EyeIcon, EyeOffIcon, FileDiffIcon, GitBranchIcon, TriangleAlertIcon, } from "lucide-react"; import { useState, type MouseEvent as ReactMouseEvent } from "react"; import { useLiveRefresh } from "~/hooks/useLiveRefresh"; import { usePullRequestChecksRefresh } from "~/hooks/usePullRequestChecksRefresh"; import { cn } from "~/lib/utils"; import { useServerConfigs } from "~/state/entities"; import { pullRequestEnvironment } from "~/state/pullRequests"; import { useEnvironmentQuery } from "~/state/query"; import { buildFixFindingsHandoff, buildResolveConflictsPrompt, classifyPullRequestChecks, describePullRequestChecks, isPullRequestConflicting, resolveSelectedMergeMethod, allowedPullRequestMergeMethods, resolveThreadPanelPullRequestAction, } from "../pullRequest/pullRequestDetail.logic"; import { PullRequestChecksPopover } from "../pullRequest/PullRequestChecksPopover"; import { pullRequestChecksState, PullRequestCheckStatusIcon, PullRequestDiffStat, resolvePullRequestState, } from "../pullRequest/pullRequestPresentation"; import { usePullRequestActionRunner, usePullRequestHandoffs, } from "../pullRequest/usePullRequestActions"; import { ChangeRequestStatusIcon, type PrStatusIndicator, type ThreadPr, } from "../ThreadStatusIndicators"; import { AlertDialog, AlertDialogClose, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogPopup, AlertDialogTitle, } from "../ui/alert-dialog"; import { Button } from "../ui/button"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { THREAD_DETAILS_PANEL_ICON_CLASS, THREAD_DETAILS_PANEL_LABEL_CLASS, THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS, THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS, } from "./threadDetailsPanelStyles"; export function ThreadDetailsPrRow({ environmentId, pr, number, reference: linkedReference, status, project, label, openAriaLabel, onOpen, onActed, onStopWatching, }: { environmentId: EnvironmentId; pr: ThreadPr; number: number; reference?: Pick | null; status: PrStatusIndicator | null; /** The thread's project, which is what the pull request is read through on the host. */ project: EnvironmentProject | null; label: string; openAriaLabel: string; onOpen: (event: ReactMouseEvent) => void; /** An action changed the pull request on the host, so the vcs status behind the row is stale. */ onActed?: () => void; /** Set while the server watches this pull request for the thread; stops the watch. */ onStopWatching?: (() => void) | undefined; }) { const serverConfigs = useServerConfigs(); const supportsPullRequests = serverConfigs.get(environmentId)?.environment.capabilities.pullRequests === true; const repository = sourceControlRepositorySelector(project?.repositoryIdentity); const reference: PullRequestRef | null = supportsPullRequests && project !== null ? linkedReference ? { ...linkedReference, projectId: project.id as ProjectId } : repository !== null ? { projectId: project.id as ProjectId, repository, number } : null : null; const detailQuery = useEnvironmentQuery( reference === null ? null : pullRequestEnvironment.detail({ environmentId, input: { ...reference, allowStale: false }, }), ); const supportsChecks = serverConfigs.get(environmentId)?.environment.capabilities.pullRequestChecks === true; const checksQuery = useEnvironmentQuery( supportsChecks && reference !== null && detailQuery.data !== null ? pullRequestEnvironment.checks({ environmentId, input: reference }) : null, ); const detail = detailQuery.data === null ? null : checksQuery.data !== null && checksQuery.dataUpdatedAt >= detailQuery.dataUpdatedAt ? { ...detailQuery.data, ...checksQuery.data } : detailQuery.data; const open = reference !== null && (detail?.state ?? pr?.state) === "open"; const refreshKey = `${environmentId}:${project?.id}:${reference?.host}:${reference?.repository}:${number}`; useLiveRefresh(detailQuery.isPending ? null : detailQuery.refresh, { enabled: open, key: `workspace-pr:${refreshKey}`, intervalMs: 10 * 60_000, }); usePullRequestChecksRefresh({ refresh: checksQuery.isPending || detailQuery.isPending ? null : checksQuery.refresh, enabled: open && supportsChecks && !(checksQuery.isSuccess && checksQuery.data === null), key: `workspace-pr-checks:${refreshKey}`, checks: detail?.checks ?? [], }); const { actionPending, perform } = usePullRequestActionRunner({ environmentId, reference, onSuccess: () => { detailQuery.refresh(); checksQuery.refresh(); onActed?.(); }, }); const { handoff, startHandoff } = usePullRequestHandoffs({ environmentId, detail }); const [confirmingMerge, setConfirmingMerge] = useState(false); const rowAction = resolveThreadPanelPullRequestAction(detail); if (confirmingMerge && rowAction !== "merge") { setConfirmingMerge(false); } const conflicting = isPullRequestConflicting(detail); const checksState = detail === null ? "none" : classifyPullRequestChecks(detail.checks); const checksRollup = detail === null ? null : pullRequestChecksState(detail.checks); const selectedMergeMethod = resolveSelectedMergeMethod( allowedPullRequestMergeMethods(detail), "merge", ); const startResolveConflicts = () => { if (detail === null) return; void startHandoff("conflicts", { prompt: buildResolveConflictsPrompt({ number: detail.number, url: detail.url, headBranch: detail.headBranch, baseBranch: detail.baseBranch, }), }); }; const startFixChecks = () => { if (detail === null) return; // The compact row fetches no conversation, so the handoff carries the failing checks alone; // review threads keep arriving through the full panel's richer version of this action. void startHandoff( "findings", buildFixFindingsHandoff({ number: detail.number, title: detail.title, url: detail.url, headBranch: detail.headBranch, baseBranch: detail.baseBranch, reviewThreads: [], comments: [], checks: detail.checks, commentsTruncated: false, }), ); }; // Host details distinguish drafts; all panels share the same PR-state glyph. const statePresentation = detail === null ? null : resolvePullRequestState({ state: detail.state, isDraft: detail.isDraft, }); const icon = statePresentation ? ( ) : pr && status ? ( ) : ( ); // Everything the host reported, at a glance. The rows stay short; the tooltip is where the // rest of the answer lives — styled like the sidebar's thread tooltip, title above icon-led // detail rows, so the two read as one family. const rowTooltip = detail === null || statePresentation === null ? ( {status?.tooltip ?? `Pull request #${number}`} ) : (
{detail.title} #{detail.number}
{statePresentation.label}
{detail.baseBranch} ← {detail.headBranch}
{detail.state === "open" && checksState !== "none" ? (
{describePullRequestChecks(detail.checks)}
) : null} {detail.isDraft && conflicting ? (
Merge conflicts with {detail.baseBranch}
) : null}
{detail.changedFiles.toLocaleString()}{" "} {detail.changedFiles === 1 ? "file" : "files"}
); const trailingAction = rowAction === "resolve" ? { label: "Resolve", pendingLabel: "Preparing...", pending: handoff === "conflicts", destructive: true, suffix: , tooltip: "Check the branch out and resolve the conflicts in a new thread", onClick: startResolveConflicts, } : rowAction === "ready" ? { label: "Ready", pendingLabel: "Marking...", pending: actionPending, destructive: false, suffix: null, tooltip: "Mark this pull request as ready for review", onClick: () => void perform("ready"), } : rowAction === "fix" ? { label: "Fix", pendingLabel: "Preparing...", pending: handoff === "findings", destructive: true, suffix: , tooltip: "Fix the failing checks in a new thread", onClick: startFixChecks, } : rowAction === "merge" ? { label: "Merge", pendingLabel: "Merging...", pending: actionPending, destructive: false, suffix: null, tooltip: `Merge this pull request (${selectedMergeMethod})`, onClick: () => setConfirmingMerge(true), } : null; // The server ends a watch when the pull request closes, so only an open one shows the eye. const watching = onStopWatching !== undefined && (detail?.state ?? pr?.state ?? "open") === "open"; const watchControl = (part: "secondary" | "icon") => watching ? ( } > Watching: the agent wakes when checks finish, someone comments, or the branch conflicts. Click to stop. ) : null; const openRow = (text: string, part: "row" | "primary") => ( } > {icon} {text} {rowTooltip} ); const showChecks = detail !== null && checksRollup !== null && !conflicting && !detail.isDraft; return ( <> {detail !== null && (showChecks || trailingAction !== null || watching) ? ( // Two lines so the title keeps the full row width: the number, checks, watch, and the // next action sit underneath, aligned with the title text. The second line hugs its title // and the block keeps space below, so stacked pull requests read as separate pairs.
{openRow(detail.title, "row")}
#{number} {showChecks && checksRollup !== null ? ( } /> ) : null} {watchControl("icon")} {trailingAction ? ( } > {trailingAction.pending ? trailingAction.pendingLabel : trailingAction.label} {trailingAction.suffix} {trailingAction.tooltip} ) : null}
) : watching ? (
{openRow(label, "primary")}
) : ( openRow(label, "row") )} {rowAction === "merge" ? ( setConfirmingMerge(open)}> Merge pull request? This merges #{number} using {selectedMergeMethod}. }> Cancel ) : null} ); }