import { GitBranchIcon } from "@refs/design-system/icons"; import { pullLink, refsErrorCode } from "@refs/backend/contract"; import { Ago, LineBadge, TabCount, Tabs } from "@refs/design-system"; import { netDecisions, parseDecisions } from "@refs/protocol"; import { Outlet, data } from "react-router"; import { PendingView, under, useIntendedPath } from "../components/pending"; import { AppLink } from "../components/link"; import type { Route } from "./+types/pull-layout"; import { PermissionRequests } from "../components/permission-requests"; import { PullStatusIcon, STATUS_LABEL } from "../components/pull-status"; import { Opener, openerOf } from "../components/timeline"; import { failed, formText, found, signedIn, fullName, nowSeconds } from "../lib/session"; import { TitleEditor } from "../components/title-editor"; import { FinishReview } from "../components/finish-review"; import { decidePermission } from "../lib/permissions"; export const meta: Route.MetaFunction = ({ loaderData, params }) => [{ title: loaderData ? `${loaderData.pull.title} · Pull request #${params.number} · ${fullName(params)} · refs` : "Pull request · refs" }]; export async function loader({ params, context }: Route.LoaderArgs) { const refs = signedIn(context); const repo = await found(refs.repo(fullName(params))); const pull = await found(repo.pull(Number(params.number))); const [changes, permissions, hosted] = await Promise.all([ pull.changes().catch((error: unknown) => { if (refsErrorCode(error) === "not_found") return null; throw error; }), pull.permissions(), repo.hostedAgents().then((agents) => agents.list()).catch(() => []), ]); const summary = changes && { ...changes, files: changes.files.map((file) => ({ ...file, before: null, after: null })) }; const hostedAgents = hosted.filter((agent) => !agent.replaced).map(({ handle, description, scope, model }) => ({ handle, description, scope, model: model ? `${model.provider} · ${model.model}` : null })); return { pull: pull.detail, changes: summary, permissions, hostedAgents, human: refs.viewer.agent === null, now: nowSeconds() }; } export async function action({ request, params, context }: Route.ActionArgs) { const repo = await found(signedIn(context).repo(fullName(params))); const pull = await found(repo.pull(Number(params.number))); const form = await request.formData(); const intent = formText(form, "intent"); if (intent === "permission") { const outcome = await decidePermission(repo, form); return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent }); } if (intent !== "title") return data({ intent, error: "Unknown action." }, { status: 400 }); const outcome = await pull.rename(formText(form, "title")); return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent }); } type Tab = "reasoning" | "conversation" | "map" | "files"; const STATUS_TEXT = { open: "text-success", merged: "text-success", closed: "text-danger" } as const; export default function PullLayout({ loaderData }: Route.ComponentProps) { const { pull, changes, permissions, human, now } = loaderData; const pathname = useIntendedPath(); const base = pullLink(pull.repo, pull.number); const current: Tab = pathname.endsWith("/reasoning") ? "reasoning" : pathname.endsWith("/files") ? "files" : pathname.endsWith("/map") ? "map" : "conversation"; const hrefs: Record = { reasoning: `${base}/reasoning`, conversation: base, map: `${base}/map`, files: `${base}/files` }; const commits = changes?.commits.length ?? 0; const decisions = netDecisions(changes?.commits.flatMap((commit) => parseDecisions(commit.message ?? "", commit.hash).decisions.map((record) => ({ record }))) ?? []).length; const branch = (name: string) => ; const tab = (value: Tab, label: string, count: number) => ({ value, label: ( <> {label} {count > 0 && {count}} ), nativeButton: false, render: (props: React.AnchorHTMLAttributes) => , }); const meta = (edit: React.ReactNode) => (
{STATUS_LABEL[pull.status]} · {pull.status === "merged" ? "merged" : "wants to merge"} {commits} {commits === 1 ? "commit" : "commits"} into {branch(pull.base)} from {branch(pull.head)} {pull.status !== "open" && pull.closedAt && ( )} {changes && ( +{changes.additions} −{changes.deletions} )} {pull.branchedFrom && ( from #{pull.branchedFrom.number} at {pull.branchedFrom.commit.slice(0, 7)} )} {edit} {human && pull.status === "open" && changes && ( )}
); return ( <>
{permissions.length > 0 && (
)}
under(to, base)}> ); }