Skip to content
refs
History
101 lines · 5.3 KBRaw
1import { Banner, Button, Input, InputArea, LayerCard } from "@refs/design-system";
2import { ArrowLeftIcon, GitPullRequestIcon } from "@refs/design-system/icons";
3import { MAX_BODY_LENGTH, MAX_TITLE_LENGTH, pullLink, refsErrorCode, refsErrorDescription } from "@refs/backend/contract";
4import { Form, redirect, useSubmit } from "react-router";
5import type { Route } from "./+types/repo-compare";
6import { DiffView } from "../components/diff-view";
7import { failed, formText, found, signedIn, fullName } from "../lib/session";
8
9export const meta: Route.MetaFunction = ({ params }) => [{ title: `Open a pull request · ${fullName(params)} · refs` }];
10
11export async function loader({ request, params, context }: Route.LoaderArgs) {
12 const search = new URL(request.url).searchParams;
13 const repo = await found(signedIn(context).repo(fullName(params)));
14 const refs = await found(repo.refs());
15 const base = search.get("base") || refs.defaultRef || refs.branches[0] || "";
16 const head = search.get("head") || "";
17 let changes = null;
18 let problem: string | null = null;
19 if (base && head && base !== head) {
20 changes = await repo.compare(base, head).catch((error: unknown) => {
21 if (refsErrorCode(error) !== "not_found") throw error;
22 problem = refsErrorDescription(error);
23 return null;
24 });
25 }
26 return { repo: fullName(params), branches: refs.branches, base, head, changes, problem };
27}
28
29export async function action({ request, params, context }: Route.ActionArgs) {
30 const repo = await found(signedIn(context).repo(fullName(params)));
31 const form = await request.formData();
32 const outcome = await repo.openPull({ title: formText(form, "title"), body: formText(form, "body"), base: formText(form, "base"), head: formText(form, "head") });
33 return outcome.ok ? redirect(`${pullLink(fullName(params), outcome.value.number)}/reasoning`) : failed(outcome.problem, {});
34}
35
36function BranchSelect({ name, label, value, branches }: { name: string; label: string; value: string; branches: readonly string[] }) {
37 return (
38 <label className="flex min-w-48 flex-col gap-1.5">
39 <span className="caption text-muted">{label}</span>
40 <select name={name} defaultValue={value} className="h-8 rounded-control bg-field px-2.5 font-mono text-compact text-ink shadow-[inset_0_0_0_1px_var(--line)] focus-visible:outline-2 focus-visible:outline-accent-hover">
41 {!value && <option value="">Choose a branch</option>}
42 {branches.map((branch) => (
43 <option key={branch} value={branch}>
44 {branch}
45 </option>
46 ))}
47 </select>
48 </label>
49 );
50}
51
52export default function RepoCompare({ loaderData, actionData }: Route.ComponentProps) {
53 const { branches, base, head, changes, problem } = loaderData;
54 const submit = useSubmit();
55 const empty = changes !== null && changes.commits.length === 0;
56 return (
57 <div className="flex flex-col gap-6">
58 <header>
59 <h1 className="m-0 text-heading leading-tight font-semibold">Open a pull request</h1>
60 <p className="mt-1 mb-0 text-compact text-muted">Choose the branch to merge and the branch it merges into.</p>
61 </header>
62 <Form method="get" onChange={(event) => submit(event.currentTarget)} className="flex flex-wrap items-end gap-3 rounded-panel bg-panel p-4 shadow-card">
63 <BranchSelect name="base" label="Base" value={base} branches={branches} />
64 <ArrowLeftIcon size={16} className="mb-2 text-faint" aria-label="merges into" />
65 <BranchSelect name="head" label="Compare" value={head} branches={branches} />
66 </Form>
67
68 {problem && <Banner variant="error" description={problem} />}
69 {base && head && base === head && <Banner variant="error" description="Choose two different branches." />}
70 {empty && <Banner description={`There isn't anything to compare: ${base} already contains every commit on ${head}.`} />}
71
72 {changes && !empty && (
73 <>
74 <LayerCard>
75 <LayerCard.Primary>
76 <Form method="post" className="flex flex-col gap-4">
77 <input type="hidden" name="base" value={base} />
78 <input type="hidden" name="head" value={head} />
79 <Input label="Title" name="title" defaultValue={changes.commits.length === 1 ? changes.commits[0].title : ""} maxLength={MAX_TITLE_LENGTH} required autoFocus />
80 <InputArea label="Description" name="body" rows={6} maxLength={MAX_BODY_LENGTH} description="Markdown is supported." />
81 {actionData?.error && <Banner variant="error" description={actionData.error} />}
82 <div className="flex justify-end">
83 <Button type="submit" variant="primary" icon={<GitPullRequestIcon />}>
84 Create pull request
85 </Button>
86 </div>
87 </Form>
88 </LayerCard.Primary>
89 </LayerCard>
90 <p className="m-0 text-compact text-muted tabular-nums">
91 {changes.commits.length} {changes.commits.length === 1 ? "commit" : "commits"} · {changes.files.length} {changes.files.length === 1 ? "file" : "files"} changed ·{" "}
92 <span className="font-mono">
93 <span className="text-success">+{changes.additions}</span> <span className="text-danger">−{changes.deletions}</span>
94 </span>
95 </p>
96 <DiffView changes={changes} />
97 </>
98 )}
99 </div>
100 );
101}