import { Banner, Button, Empty, InputArea } from "@refs/design-system"; import { LayerCard } from "@refs/design-system"; import { CheckCircleIcon, GitDiffIcon, XCircleIcon } from "@refs/design-system/icons"; import { isReviewVerdict, pullLink, refsErrorCode } from "@refs/backend/contract"; import { redirect, useRouteLoaderData } from "react-router"; import type { Route } from "./+types/pull-files"; import type { loader as pullLoader } from "./pull-layout"; import { DiffView } from "../components/diff-view"; import { ReviewThreadCard, type ThreadComment } from "../components/review-thread"; import { THREAD_INTENTS, threadAction } from "../lib/threads"; import { failed, formText, found, signedIn, fullName, nowSeconds } from "../lib/session"; export const meta: Route.MetaFunction = ({ params }) => [{ title: `Files changed · Pull request #${params.number} · ${fullName(params)} · 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 [agents, choices, threads, timeline, mentions, changes] = await Promise.all([ repo.agents(), refs.taskChoices(), pull.threads(), pull.timeline(), pull.mentions(), pull.changes().catch((error: unknown) => { if (refsErrorCode(error) === "not_found") return null; throw error; }), ]); const comments = timeline.filter((event): event is ThreadComment => event.kind === "pull.commented" && event.data.thread !== undefined); return { human: refs.viewer.agent === null, canResolve: refs.viewer.agent === null && repo.summary.can.write, agents, people: choices.people, threads, comments, mentions, changes, now: nowSeconds() }; } export async function action({ request, params, context }: Route.ActionArgs) { const refs = signedIn(context); const repo = await found(refs.repo(fullName(params))); const pull = await found(repo.pull(Number(params.number))); const link = pullLink(pull.detail.repo, pull.detail.number); const form = await request.formData(); const intent = formText(form, "intent"); const revision = formText(form, "revision"); if (THREAD_INTENTS.has(intent)) { const outcome = await threadAction(pull, intent, form); return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent }); } if (intent === "inline-comment") { const side = formText(form, "side") === "deletions" ? "deletions" : "additions"; const endSide = formText(form, "endSide") === "deletions" ? "deletions" : "additions"; const range = { start: Number(formText(form, "start")), side, end: Number(formText(form, "end")), endSide } as const; const outcome = await pull.reviewComment({ revision, path: formText(form, "path"), range, body: formText(form, "body"), assign: formText(form, "assign") === "1" }); return outcome.ok ? redirect(`${link}/files`) : failed(outcome.problem, { intent }); } if (intent === "review") { const verdict = formText(form, "verdict"); if (!isReviewVerdict(verdict)) return failed({ code: "invalid", message: "Choose comment, approve or request changes." }, { intent }); const outcome = await found(pull.review({ revision, verdict, body: formText(form, "body") })); return outcome.ok ? redirect(link) : failed(outcome.problem, { intent }); } return failed({ code: "invalid", message: "Unknown review action." }, { intent }); } export default function PullFiles({ loaderData, actionData }: Route.ComponentProps) { const { human, canResolve, agents, people, threads, comments, mentions, changes, now } = loaderData; const { pull } = useRouteLoaderData("routes/pull-layout")!; if (!changes) return } title="Changes unavailable" description={`The ${pull.head} or ${pull.base} branch no longer exists.`} />; return (
{actionData?.error && } ( comment.data.thread === thread.id)} pullBase={pullLink(pull.repo, pull.number)} agents={agents} canResolve={canResolve} mentionsOf={(comment) => mentions.filter((mention) => mention.comment === comment)} now={now} /> ), }} /> {human && ( Submit review
)}
); }