Skip to content
refs
History
31 lines · 1.5 KBRaw
1import { redirect } from "react-router";
2import type { Route } from "./+types/new";
3import { RepoForm, readRepoForm } from "../components/repo-form";
4import { AppShell, PageHeader } from "../components/shell";
5import { failed, formText, signedIn } from "../lib/session";
6
7export const meta: Route.MetaFunction = () => [{ title: "New repository · refs" }];
8
9export function loader({ context }: Route.LoaderArgs) {
10 const { viewer } = signedIn(context);
11 const current = viewer.namespaces.find((namespace) => namespace.slug === viewer.namespace?.slug);
12 return { owners: viewer.namespaces, owner: current?.slug ?? viewer.namespaces[0]?.slug };
13}
14
15export async function action({ request, context }: Route.ActionArgs) {
16 const refs = signedIn(context);
17 const form = await request.formData();
18 const values = { ...readRepoForm(form), importUrl: formText(form, "import_url"), setUp: formText(form, "set_up") === "1" };
19 const outcome = await refs.createRepo({ ...values, importFrom: values.importUrl || undefined });
20 if (!outcome.ok) return failed(outcome.problem, { values });
21 throw redirect(`/${outcome.value.name}`);
22}
23
24export default function NewRepository({ loaderData, actionData }: Route.ComponentProps) {
25 return (
26 <AppShell>
27 <PageHeader title="New repository" description="Start an empty repository, or import one from a public Git URL." />
28 <RepoForm submit="Create repository" error={actionData?.error} values={actionData?.values ?? { owner: loaderData.owner }} owners={loaderData.owners} allowImport />
29 </AppShell>
30 );
31}