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