Skip to content
refs
89 lines · 4.4 KBRaw
1import { Banner, Empty } from "@refs/design-system";
2import { GitDiffIcon } from "@refs/design-system/icons";
3import { isReviewVerdict, pullLink, refsErrorCode } from "@refs/backend/contract";
4import { redirect, useRouteLoaderData } from "react-router";
5import type { Route } from "./+types/pull-files";
6import type { loader as pullLoader } from "./pull-layout";
7import { DiffView } from "../components/diff-view";
8import { ReviewThreadCard, type ThreadComment } from "../components/review-thread";
9import { THREAD_INTENTS, threadAction } from "../lib/threads";
10import { failed, formText, found, signedIn, fullName, nowSeconds } from "../lib/session";
11
12export const meta: Route.MetaFunction = ({ params }) => [{ title: `Files changed · Pull request #${params.number} · ${fullName(params)} · refs` }];
13
14export async function loader({ params, context }: Route.LoaderArgs) {
15 const refs = signedIn(context);
16 const repo = await found(refs.repo(fullName(params)));
17 const pull = await found(repo.pull(Number(params.number)));
18 const [agents, choices, threads, timeline, mentions, changes] = await Promise.all([
19 repo.agents(),
20 refs.taskChoices(),
21 pull.threads(),
22 pull.timeline(),
23 pull.mentions(),
24 pull.changes().catch((error: unknown) => {
25 if (refsErrorCode(error) === "not_found") return null;
26 throw error;
27 }),
28 ]);
29 const comments = timeline.filter((event): event is ThreadComment => event.kind === "pull.commented" && event.data.thread !== undefined);
30 return { canResolve: refs.viewer.agent === null && repo.summary.can.write, agents, people: choices.people, threads, comments, mentions, changes, now: nowSeconds() };
31}
32
33export async function action({ request, params, context }: Route.ActionArgs) {
34 const refs = signedIn(context);
35 const repo = await found(refs.repo(fullName(params)));
36 const pull = await found(repo.pull(Number(params.number)));
37 const link = pullLink(pull.detail.repo, pull.detail.number);
38 const form = await request.formData();
39 const intent = formText(form, "intent");
40 const revision = formText(form, "revision");
41 if (THREAD_INTENTS.has(intent)) {
42 const outcome = await threadAction(pull, intent, form);
43 return outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent });
44 }
45 if (intent === "inline-comment") {
46 const side = formText(form, "side") === "deletions" ? "deletions" : "additions";
47 const endSide = formText(form, "endSide") === "deletions" ? "deletions" : "additions";
48 const range = { start: Number(formText(form, "start")), side, end: Number(formText(form, "end")), endSide } as const;
49 const outcome = await pull.reviewComment({ revision, path: formText(form, "path"), range, body: formText(form, "body"), assign: formText(form, "assign") === "1" });
50 return outcome.ok ? redirect(`${link}/files`) : failed(outcome.problem, { intent });
51 }
52 if (intent === "review") {
53 const verdict = formText(form, "verdict");
54 if (!isReviewVerdict(verdict)) return failed({ code: "invalid", message: "Choose comment, approve or request changes." }, { intent });
55 const outcome = await found(pull.review({ revision, verdict, body: formText(form, "body") }));
56 return outcome.ok ? redirect(link) : failed(outcome.problem, { intent });
57 }
58 return failed({ code: "invalid", message: "Unknown review action." }, { intent });
59}
60
61export default function PullFiles({ loaderData, actionData }: Route.ComponentProps) {
62 const { canResolve, agents, people, threads, comments, mentions, changes, now } = loaderData;
63 const { pull } = useRouteLoaderData<typeof pullLoader>("routes/pull-layout")!;
64 if (!changes) return <Empty icon={<GitDiffIcon size={40} />} title="Changes unavailable" description={`The ${pull.head} or ${pull.base} branch no longer exists.`} />;
65 return (
66 <div className="flex flex-col gap-4">
67 {actionData?.error && <Banner variant="error" description={actionData.error} />}
68 <DiffView
69 changes={changes}
70 review={{
71 agents,
72 people,
73 threads,
74 renderThread: (thread) => (
75 <ReviewThreadCard
76 thread={thread}
77 comments={comments.filter((comment) => comment.data.thread === thread.id)}
78 pullBase={pullLink(pull.repo, pull.number)}
79 agents={agents}
80 canResolve={canResolve}
81 mentionsOf={(comment) => mentions.filter((mention) => mention.comment === comment)}
82 now={now}
83 />
84 ),
85 }}
86 />
87 </div>
88 );
89}