Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
"use client";
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { toast } from "sonner";
import {
CheckCircle2,
Circle,
CircleDashed,
CircleSlash,
Clock,
GitMerge,
LoaderCircle,
MoreHorizontal,
Unlink,
GitPullRequest,
GitPullRequestClosed,
GitPullRequestDraft,
TriangleAlert,
XCircle,
} from "@multica/ui/icons";
import {
issuePullRequestsOptions,
derivePullRequestVerdict,
formatPullRequestDiffCount,
shouldShowPullRequestStats,
stripIssueKeyFromTitle,
useLinkIssuePullRequest,
useSetIssuePRAutoComplete,
useUnlinkIssuePullRequest,
type PullRequestVerdict,
} from "@multica/core/github";
import { useWorkspaceId } from "@multica/core/hooks";
import { useIssueStatuses } from "@multica/core/issue-statuses/hooks";
import type {
GitHubPullRequest,
PRAutoComplete,
} from "@multica/core/types";
import { IconButton } from "@multica/ui/components/common/icon-button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@multica/ui/components/ui/tooltip";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuTrigger,
} from "@multica/ui/components/ui/dropdown-menu";
import { cn } from "@multica/ui/lib/utils";
import { useT, useTimeAgo } from "../../i18n";
import { useStatusLabel } from "../utils/status-label";
import { StatusIcon } from "./status-icon";
import { PULL_REQUEST_STATE_ICON } from "./pull-request-chips";
type IssuesT = ReturnType<typeof useT<"issues">>["t"];
// Keep the existing sidebar density: show the first 3 PR rows inline, then
// collapse the rest once the section reaches 4 rows.
const PR_LIMIT_BEFORE_COLLAPSE = 4;
export function PullRequestList({
issueId,
identifier = "",
}: {
issueId: string;
/** The issue's identifier, for the "linked by MUL-1 in the title" label. */
identifier?: string;
}) {
const { t } = useT("issues");
const [expanded, setExpanded] = useState(false);
const { data, isLoading, isError, refetch } = useQuery(issuePullRequestsOptions(issueId));
const prs = data?.pull_requests ?? [];
// Older backends send no auto-complete block and have no link/unlink
// endpoints, so the row actions stay hidden with it.
const autoComplete = data?.auto_complete ?? null;
const rowActions = autoComplete ? { issueId, identifier, autoComplete } : null;
if (isLoading) {
return <p className="text-label text-muted-foreground px-2">{t(($) => $.detail.pull_requests_loading)}</p>;
}
if (isError) {
return <button type="button" className="flex min-h-8 w-full items-center rounded-md px-2 py-1.5 text-left text-label text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring" onClick={() => void refetch()}>
{t(($) => $.detail.section_load_failed_retry)}
</button>;
}
if (prs.length === 0) {
return null;
}
// Render rule:
// - < PR_LIMIT_BEFORE_COLLAPSE: every PR row is visible.
// - >= PR_LIMIT_BEFORE_COLLAPSE: first (LIMIT - 1) rows are visible and
// the remainder sits behind a toggle.
const useCollapse = prs.length >= PR_LIMIT_BEFORE_COLLAPSE;
const expandedHead = useCollapse ? prs.slice(0, PR_LIMIT_BEFORE_COLLAPSE - 1) : prs;
const collapsedTail = useCollapse ? prs.slice(PR_LIMIT_BEFORE_COLLAPSE - 1) : [];
// The repo name only tells rows apart when the PRs span repos; otherwise it
// spends the width the diff needs. The row tooltip keeps owner/repo#number.
const showRepo = new Set(prs.map((pr) => `${pr.repo_owner}/${pr.repo_name}`)).size > 1;
// A lone PR the merge is waiting on carries the outcome as its own last line.
const inlineWaiting =
autoComplete?.state === "waiting" &&
prs.length === 1 &&
autoComplete.pull_request_ids.includes(prs[0]!.id);
return (
<div className="space-y-1">
{expandedHead.map((pr) => (
<PullRequestRow
key={pr.id}
pr={pr}
identifier={identifier}
showRepo={showRepo}
actions={rowActions}
mergeTarget={inlineWaiting && autoComplete ? mergeTarget(autoComplete) : undefined}
/>
))}
{useCollapse ? (
<div className="space-y-1">
{expanded
? collapsedTail.map((pr) => (
<PullRequestRow key={pr.id} pr={pr} identifier={identifier} showRepo={showRepo} actions={rowActions} />
))
: null}
<button
type="button"
onClick={() => setExpanded((v) => !v)}
className="block w-full rounded-md px-2 py-2 text-left text-label text-muted-foreground hover:bg-accent/50 hover:text-foreground transition-colors"
>
{expanded
? t(($) => $.detail.pull_request_card_show_less)
: t(($) => $.detail.pull_request_card_show_more, { count: collapsedTail.length })}
</button>
</div>
) : null}
{autoComplete && !inlineWaiting ? (
<AutoCompleteLine issueId={issueId} prs={prs} autoComplete={autoComplete} />
) : null}
</div>
);
}
interface RowActions {
issueId: string;
identifier: string;
autoComplete: PRAutoComplete;
}
const prLabel = (pr: Pick<GitHubPullRequest, "number">) => `#${pr.number}`;
/** Where a merge moves the issue; older backends only ever moved it to Done. */
const mergeTarget = (autoComplete: PRAutoComplete) => autoComplete.target_status ?? "done";
/** States in which the merge has nothing left to do for this issue. */
const SETTLED = new Set(["terminal", "at_target"]);
/**
* Remove a PR from the issue. The server remembers the removal (webhooks will
* not link it again) and treats it as a PR event, so removing the last
* unmerged PR can move the issue — the toast says so, and offers undo only
* when nothing else changed.
*/
function useUnlinkPullRequest(issueId: string, before: PRAutoComplete) {
const { t } = useT("issues");
const statusLabel = useStatusLabel(useWorkspaceId());
const unlink = useUnlinkIssuePullRequest(issueId);
const relink = useLinkIssuePullRequest(issueId);
return (pr: GitHubPullRequest) => {
unlink.mutate(pr.id, {
onSuccess: (after) => {
const moved =
!SETTLED.has(before.state) && !!after.auto_complete && SETTLED.has(after.auto_complete.state);
if (moved) {
toast.success(
t(($) => $.pr_automation.unlinked_moved, {
pr: prLabel(pr),
status: statusLabel(mergeTarget(after.auto_complete!)),
}),
);
return;
}
toast.success(t(($) => $.pr_automation.unlinked, { pr: prLabel(pr) }), {
action: {
label: t(($) => $.pr_automation.undo),
onClick: () => relink.mutate({ pull_request_id: pr.id }),
},
});
},
onError: () => toast.error(t(($) => $.pr_automation.unlink_failed)),
});
};
}
function linkSourceLabel(pr: GitHubPullRequest, identifier: string, t: IssuesT): string {
switch (pr.link_source) {
case "manual":
return t(($) => $.pr_automation.source_manual);
case "title":
return t(($) => $.pr_automation.source_title, { identifier });
case "branch":
return t(($) => $.pr_automation.source_branch);
default:
return t(($) => $.pr_automation.source_auto);
}
}
function PullRequestRowMenu({ pr, actions }: { pr: GitHubPullRequest; actions: RowActions }) {
const { t } = useT("issues");
const unlink = useUnlinkPullRequest(actions.issueId, actions.autoComplete);
return (
<DropdownMenu>
<DropdownMenuTrigger
render={
<IconButton
variant="ghost"
size="icon-xs"
label={t(($) => $.pr_automation.row_menu)}
className="absolute top-0.5 right-0 opacity-0 group-hover/pr:opacity-100 group-has-[:focus-visible]/pr:opacity-100 data-popup-open:opacity-100 [@media(pointer:coarse)]:opacity-100"
>
<MoreHorizontal />
</IconButton>
}
/>
<DropdownMenuContent align="end" className="w-max max-w-(--available-width)">
<DropdownMenuGroup>
<DropdownMenuLabel className="font-normal">
{linkSourceLabel(pr, actions.identifier, t)}
</DropdownMenuLabel>
<DropdownMenuItem onClick={() => unlink(pr)}>
<Unlink />
{t(($) => $.pr_automation.unlink)}
</DropdownMenuItem>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
);
}
// Splits a translated sentence around the target status so each language keeps
// its own word order and the status renders as a chip, not plain text.
const STATUS_SLOT = "\u2063status\u2063";
function TargetSentence({ sentence, target }: { sentence: string; target: string }) {
const wsId = useWorkspaceId();
const catalog = useIssueStatuses(wsId);
const statusLabel = useStatusLabel(wsId);
const [before, after = ""] = sentence.split(STATUS_SLOT);
return (
<span>
{before}
<span className="inline-flex items-center gap-1 whitespace-nowrap align-middle font-medium text-foreground">
<StatusIcon
status={target}
category={catalog.categoryOf(target)}
color={catalog.colorOf(target)}
icon={catalog.iconOf(target)}
className="size-3"
/>
{statusLabel(target)}
</span>
{after}
</span>
);
}
/**
* One line under the PR list saying what "every linked PR merged → move the
* issue to the workspace's target status" will do for this issue, straight
* from the server's decision. It speaks only when a merge would move the issue
* or this issue opted out: a workspace that leaves status alone, a finished or
* triaged issue, one already in the target, and unknown states render nothing.
*/
function AutoCompleteLine({
issueId,
prs,
autoComplete,
}: {
issueId: string;
prs: GitHubPullRequest[];
autoComplete: PRAutoComplete;
}) {
const { t } = useT("issues");
const unlink = useUnlinkPullRequest(issueId, autoComplete);
const setAutoComplete = useSetIssuePRAutoComplete(issueId);
const named = autoComplete.pull_request_ids
.map((id) => prs.find((pr) => pr.id === id))
.filter((pr): pr is GitHubPullRequest => !!pr);
const list = named.map(prLabel).join(", ");
const action = (label: string, onClick: () => void) => (
<button
type="button"
onClick={onClick}
className="font-medium text-foreground underline decoration-border underline-offset-2 hover:decoration-foreground"
>
{label}
</button>
);
const target = mergeTarget(autoComplete);
const withTarget = (sentence: string) => <TargetSentence sentence={sentence} target={target} />;
let icon: React.ReactNode;
let body: React.ReactNode;
switch (autoComplete.state) {
case "waiting":
if (named.length === 0) return null;
icon = <CircleDashed className="text-muted-foreground" />;
body = withTarget(t(($) => $.pr_automation.waiting_to, { count: named.length, prs: list, status: STATUS_SLOT }));
break;
case "not_merged": {
if (named.length === 0) return null;
const only = named.length === 1 ? named[0] : undefined;
icon = <TriangleAlert className="text-amber-600 dark:text-amber-400" />;
body = (
<>
{t(($) => $.pr_automation.not_merged, { prs: list })}
{only ? <> · {action(t(($) => $.pr_automation.not_merged_action), () => unlink(only))}</> : null}
</>
);
break;
}
case "all_merged":
icon = <CheckCircle2 className="text-muted-foreground" />;
body = withTarget(t(($) => $.pr_automation.all_merged_to, { status: STATUS_SLOT }));
break;
case "issue_disabled":
icon = <CircleSlash className="text-muted-foreground" />;
body = (
<>
{t(($) => $.pr_automation.issue_disabled)} ·{" "}
{action(t(($) => $.pr_automation.issue_disabled_action), () =>
setAutoComplete.mutate(false, {
onError: () => toast.error(t(($) => $.pr_automation.update_failed)),
}),
)}
</>
);
break;
default:
return null;
}
return (
<p
data-testid="pr-auto-complete-line"
className="flex items-start gap-2.5 px-2 py-2 text-caption text-muted-foreground [&>svg]:mt-px [&>svg]:size-3.5 [&>svg]:shrink-0"
>
{icon}
<span className="min-w-0">{body}</span>
</p>
);
}
// At most this many problem lines under a failed PR; the rest fold into "+N more".
const PROBLEM_LIMIT = 3;
type VerdictTone = "red" | "amber" | "green" | "violet" | "muted";
// Pills reuse the state icons' palette. Their text sits one shade darker so
// 11px copy on a 10% tint keeps AA contrast. Merged is a pull request state,
// so it follows the theme's --pr-merged.
const TONE_CLASS: Record<VerdictTone, string> = {
red: "bg-rose-600/10 text-rose-700 dark:bg-rose-400/15 dark:text-rose-400",
amber: "bg-amber-600/10 text-amber-700 dark:bg-amber-400/15 dark:text-amber-400",
green: "bg-emerald-600/10 text-emerald-700 dark:bg-emerald-400/15 dark:text-emerald-400",
violet: "bg-pr-merged/10 text-pr-merged-text dark:bg-pr-merged-dark/15 dark:text-pr-merged-dark",
muted: "bg-muted text-muted-foreground",
};
interface VerdictPillConfig {
icon: React.ComponentType<{ className?: string }>;
tone: VerdictTone;
label: string;
/** The longer form of the label, on hover. */
title?: string;
/** The icon spins while the work it reports is live. */
spin?: boolean;
}
/**
* One PR in the sidebar: the title, then `#number` (`repo#number` when the
* list spans repos) with one verdict pill aligned right, so several PRs scan
* as a column. A failed PR also lists what failed. Owner, author, the full
* title and the diff size live in the row's tooltip.
*/
function PullRequestRow({
pr,
identifier,
showRepo,
actions,
mergeTarget: onMergeTarget,
}: {
pr: GitHubPullRequest;
identifier: string;
showRepo: boolean;
actions: RowActions | null;
/** The status merging this PR moves the issue to, said on the row's last line. */
mergeTarget?: string;
}) {
const { t } = useT("issues");
const timeAgo = useTimeAgo();
const cfg = PULL_REQUEST_STATE_ICON[pr.state] ?? { icon: GitPullRequest, className: "" };
const StateIcon = cfg.icon;
const verdict = derivePullRequestVerdict(pr);
const pill = getVerdictPill(verdict, t);
const showStats = shouldShowPullRequestStats(pr);
// A stale snapshot (GitHub outage / revoked key) greys out the verdict and
// shows the snapshot age instead of hiding the last-known data.
const isTerminal = pr.state === "merged" || pr.state === "closed";
const stale = !isTerminal && pr.snapshot_stale === true;
const staleTitle = stale
? pr.snapshot_fetched_at
? t(($) => $.detail.pull_request_snapshot_stale, { time: timeAgo(pr.snapshot_fetched_at) })
: t(($) => $.detail.pull_request_snapshot_stale_unknown)
: undefined;
const endedAt = pr.state === "merged" ? pr.merged_at : pr.state === "closed" ? pr.closed_at : null;
let meta: React.ReactNode = null;
if (isTerminal) {
meta = endedAt ? <span className="truncate">{timeAgo(endedAt)}</span> : null;
} else if (stale && pr.snapshot_fetched_at) {
meta = (
<span className="inline-flex min-w-0 items-center gap-1" title={staleTitle}>
<Clock className="size-3 shrink-0" />
<span className="truncate">{timeAgo(pr.snapshot_fetched_at)}</span>
</span>
);
}
// The row link and its menu are siblings: a button inside an anchor is
// invalid HTML, and the menu must not open the PR.
return (
<div className="group/pr relative rounded-md transition-colors hover:bg-accent/50">
<Tooltip>
<TooltipTrigger
render={
<a
data-testid="pull-request-row"
href={pr.html_url}
target="_blank"
rel="noreferrer noopener"
className="flex items-start gap-2.5 rounded-md px-2 py-2"
/>
}
>
<StateIcon className={cn("mt-px size-3.5 shrink-0", cfg.className)} />
<div className="min-w-0 flex-1">
<p className={cn("truncate text-label font-medium", actions ? "pr-5" : null)}>
{stripIssueKeyFromTitle(pr.title, identifier)}
</p>
<div className="mt-1 flex items-center gap-2">
{/* The number and the pill never yield. The diff and the trailing
meta are each one unit: short of room, they wrap whole onto
the clipped second line (the tooltip keeps them) instead of
being cut mid-word or drawn over the number. The repo name
takes no room of its own (zero basis), only what is left
over, so it truncates before the diff wraps; its negative
margin cancels the gap so it reads `repo#number`. */}
<p className="flex h-lh min-w-0 flex-1 flex-wrap items-center gap-x-1.5 overflow-hidden whitespace-nowrap text-caption text-muted-foreground tabular-nums">
{showRepo ? (
<span className="-me-1.5 min-w-0 max-w-fit grow basis-0 truncate">{pr.repo_name}</span>
) : null}
<span className="shrink-0">#{pr.number}</span>
{showStats ? (
<span data-testid="pull-request-diff" className="inline-flex shrink-0 items-center gap-1.5">
<span aria-hidden="true" className="text-faint-foreground">
·
</span>
<span className="inline-flex items-center gap-1">
<span className="text-success">+{formatPullRequestDiffCount(pr.additions ?? 0)}</span>
<span className="text-destructive">−{formatPullRequestDiffCount(pr.deletions ?? 0)}</span>
</span>
</span>
) : null}
{meta ? (
<span className="inline-flex min-w-0 items-center gap-1.5">
<span aria-hidden="true" className="text-faint-foreground">
·
</span>
{meta}
</span>
) : null}
</p>
{pill ? <VerdictPill pill={pill} stale={stale} title={staleTitle} /> : null}
</div>
{verdict.kind === "failed" ? <PullRequestProblems verdict={verdict} stale={stale} /> : null}
{onMergeTarget ? (
<p data-testid="pr-merge-target" className="mt-1 truncate text-caption text-muted-foreground">
<TargetSentence sentence={t(($) => $.pr_automation.on_merge_to, { status: STATUS_SLOT })} target={onMergeTarget} />
</p>
) : null}
</div>
</TooltipTrigger>
<TooltipContent side="left" align="start" className="flex-col items-start gap-0.5">
<span className="font-medium">{pr.title}</span>
<span className="text-muted-foreground">
{pr.repo_owner}/{pr.repo_name}#{pr.number}
{pr.author_login ? ` · @${pr.author_login}` : null}
</span>
{showStats ? (
<span className="text-muted-foreground tabular-nums">
+{(pr.additions ?? 0).toLocaleString()} −{(pr.deletions ?? 0).toLocaleString()} ·{" "}
{t(($) => $.detail.pull_request_card_files_count, { count: pr.changed_files ?? 0 })}
</span>
) : null}
</TooltipContent>
</Tooltip>
{actions ? <PullRequestRowMenu pr={pr} actions={actions} /> : null}
</div>
);
}
/**
* One PR on a single line — state icon, `#number`, verdict pill — for
* surfaces with no room for PullRequestRow, such as the issue hover card.
* A plain link to the PR on GitHub, like PullRequestRow, so the browser owns
* Enter, modifier and middle clicks. The hover card stops click bubbling, so
* opening the PR never also follows the mention it hangs off.
*/
export function PullRequestInlineSummary({ pr }: { pr: GitHubPullRequest }) {
const { t } = useT("issues");
const cfg = PULL_REQUEST_STATE_ICON[pr.state] ?? { icon: GitPullRequest, className: "" };
const StateIcon = cfg.icon;
const pill = getVerdictPill(derivePullRequestVerdict(pr), t);
const isTerminal = pr.state === "merged" || pr.state === "closed";
const stale = !isTerminal && pr.snapshot_stale === true;
return (
<a
data-testid="pull-request-inline-summary"
href={pr.html_url}
target="_blank"
rel="noreferrer noopener"
title={pr.title}
className="-mx-1 flex min-w-0 items-center gap-1.5 rounded-sm px-1 transition-colors hover:bg-accent/50 focus-visible:outline-2 focus-visible:outline-ring"
>
<StateIcon className={cn("size-3.5 shrink-0", cfg.className)} />
<span className="shrink-0 font-medium tabular-nums">{prLabel(pr)}</span>
{pill ? <VerdictPill pill={pill} stale={stale} inline /> : null}
</a>
);
}
function VerdictPill({
pill,
stale,
title,
inline = false,
}: {
pill: VerdictPillConfig;
stale: boolean;
title?: string;
inline?: boolean;
}) {
const Icon = pill.icon;
return (
<span
data-testid="pull-request-verdict"
title={title ?? pill.title}
className={cn(
inline ? null : "ml-auto",
"inline-flex h-4.5 shrink-0 items-center gap-1 whitespace-nowrap rounded-full px-1.5 text-micro font-medium tabular-nums",
TONE_CLASS[pill.tone],
stale ? "opacity-60" : null,
)}
>
{/* A stale snapshot can't vouch that the checks are still running. */}
<Icon className={cn("size-3 shrink-0", pill.spin && !stale ? "motion-safe:animate-spin" : null)} />
{pill.label}
</span>
);
}
/** What stands in the way of a failed PR: a conflict, then each failing check. */
function PullRequestProblems({
verdict,
stale,
}: {
verdict: Extract<PullRequestVerdict, { kind: "failed" }>;
stale: boolean;
}) {
const { t } = useT("issues");
const items = [
...(verdict.conflicting
? [{ key: "conflict", icon: TriangleAlert, className: "text-amber-600 dark:text-amber-400", label: t(($) => $.detail.pull_request_merge_conflicting) }]
: []),
...verdict.names.map((name) => ({ key: `check:${name}`, icon: XCircle, className: "text-rose-600 dark:text-rose-400", label: name })),
];
if (items.length === 0) return null;
const shown = items.slice(0, PROBLEM_LIMIT);
const remaining = items.length - shown.length;
return (
<ul className={cn("mt-1.5 rounded-md bg-muted/70 px-2 py-1 text-micro", stale ? "opacity-60" : null)}>
{shown.map(({ key, icon: Icon, className, label }) => (
<li key={key} className="flex items-center gap-1.5 py-px">
<Icon className={cn("size-3 shrink-0", className)} />
<span className="truncate">{label}</span>
</li>
))}
{remaining > 0 ? (
<li className="py-px pl-4.5 text-muted-foreground">
{t(($) => $.detail.pull_request_checks_more, { count: remaining })}
</li>
) : null}
</ul>
);
}
function getVerdictPill(verdict: PullRequestVerdict, t: IssuesT): VerdictPillConfig | null {
switch (verdict.kind) {
case "merged":
return { icon: GitMerge, tone: "violet", label: t(($) => $.detail.pull_request_state_merged) };
case "closed":
return { icon: GitPullRequestClosed, tone: "muted", label: t(($) => $.detail.pull_request_state_closed) };
case "failed":
return {
icon: XCircle,
tone: "red",
label: t(($) => $.detail.pull_request_checks_failed_count, {
failed: verdict.failed,
total: verdict.total,
}),
};
case "conflicting":
return {
icon: TriangleAlert,
tone: "amber",
label: t(($) => $.detail.pull_request_verdict_conflicting),
title: t(($) => $.detail.pull_request_merge_conflicting),
};
case "running":
return {
icon: LoaderCircle,
tone: "amber",
spin: true,
label: `${verdict.passed}/${verdict.total}`,
title: t(($) => $.detail.pull_request_checks_running, {
passed: verdict.passed,
total: verdict.total,
running: verdict.running,
}),
};
case "behind":
return { icon: CircleSlash, tone: "muted", label: t(($) => $.detail.pull_request_merge_behind) };
case "blocked":
return { icon: CircleSlash, tone: "muted", label: t(($) => $.detail.pull_request_merge_blocked) };
case "draft":
return { icon: GitPullRequestDraft, tone: "muted", label: t(($) => $.detail.pull_request_state_draft) };
case "ready":
return {
icon: CheckCircle2,
tone: "green",
label: t(($) => $.detail.pull_request_verdict_ready),
title: t(($) => $.detail.pull_request_merge_ready),
};
case "passed":
return {
icon: CheckCircle2,
tone: "green",
label: t(($) => $.detail.pull_request_verdict_passed),
title: t(($) => $.detail.pull_request_checks_all_passed, { total: verdict.total }),
};
case "no_checks":
return { icon: Circle, tone: "muted", label: t(($) => $.detail.pull_request_checks_none) };
case "unknown":
return null;
}
}