Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

105 linesCodeBlameRaw
1import { ArrowLeft, Building2 } from "lucide-react";
2import { data, Link, redirect } from "react-router";
3
4import type { Route } from "./+types/new-enterprise";
5import { Avatar, Button, Field, Input, Notice, Section, Textarea } from "~/components/ui";
6import { fields, parseSlugList, text } from "~/lib/forms";
7import { admin, identity } from "~/lib/services.server";
8import { requireStaff } from "~/lib/staff";
9
10export const meta: Route.MetaFunction = () => [{ title: "New enterprise · sudo" }, { name: "robots", content: "noindex, nofollow" }];
11
12export async function loader({ context }: Route.LoaderArgs) {
13 requireStaff(context);
14 return null;
15}
16
17type ActionData = { error: string; values: Record<string, string> } | { review: { name: string; workspaces: string[]; values: Record<string, string> } };
18
19export async function action({ request, context }: Route.ActionArgs) {
20 const staff = requireStaff(context);
21 const form = await request.formData();
22 const values = fields(form, "name", "workspaces");
23 const fail = (error: string) => data<ActionData>({ error, values }, { status: 422 });
24
25 const name = values.name.replace(/\s+/g, " ");
26 if (!name) return fail("Give the enterprise a name.");
27 if (name.length > 100) return fail("Keep the name under 100 characters.");
28 const workspaces = parseSlugList(values.workspaces);
29 if (!workspaces.ok) return fail(workspaces.error);
30 if (workspaces.value.length === 0) return fail("Name at least one workspace for it to pay for.");
31 if (workspaces.value.length > 100) return fail("At most 100 workspaces at once.");
32 const found = await Promise.all(workspaces.value.map((slug) => identity.workspace(slug)));
33 const unknown = workspaces.value.filter((_, index) => !found[index]);
34 if (unknown.length > 0) return fail(`No workspace called ${unknown.join(", ")}.`);
35
36 // Moving workspaces onto it changes who pays for them: confirm first.
37 if (text(form, "confirm") !== "yes") {
38 return { review: { name, workspaces: workspaces.value, values } } satisfies ActionData;
39 }
40 const result = await admin.createEnterprise(name, workspaces.value, staff.email);
41 if (!result.ok) return fail(result.error.message);
42 return redirect(`/enterprises/${encodeURIComponent(result.value.id)}?done=created#top`);
43}
44
45export default function NewEnterprise({ actionData }: Route.ComponentProps) {
46 const result = actionData as ActionData | undefined;
47 const review = result && "review" in result ? result.review : null;
48 const error = result && "error" in result ? result : null;
49 const values = review?.values ?? error?.values;
50
51 return (
52 <main className="mx-auto max-w-2xl px-4 py-8 sm:py-10">
53 <Link to="/enterprises" className="inline-flex items-center gap-1.5 text-sm text-muted hover:text-fg">
54 <ArrowLeft size={14} />
55 Enterprises
56 </Link>
57 <h1 className="mt-4 text-2xl font-semibold tracking-tight">New enterprise</h1>
58 <p className="mt-1 text-sm text-muted">
59 One customer that pays for several workspaces: one bill, one limit, one set of terms. Set its terms once it exists.
60 </p>
61
62 {review && (
63 <section id="review" className="mt-6 scroll-mt-20 rounded-lg border border-accent/40 bg-accent/5 p-4 sm:p-5">
64 <h2 className="font-semibold tracking-tight">Create {review.name}?</h2>
65 <p className="mt-1 text-sm text-muted">These workspaces will be billed to it from now on, under its limit and terms instead of their own:</p>
66 <ul className="mt-3 flex flex-wrap gap-2">
67 {review.workspaces.map((slug) => (
68 <li key={slug} className="inline-flex items-center gap-1.5 rounded-md border border-line bg-bg px-2 py-1 font-mono text-xs">
69 <Avatar name={slug} size={14} />
70 {slug}
71 </li>
72 ))}
73 </ul>
74 <form method="post" action="/enterprises/new" className="mt-4 flex flex-wrap items-center gap-2">
75 <input type="hidden" name="name" value={review.values.name} />
76 <input type="hidden" name="workspaces" value={review.values.workspaces} />
77 <input type="hidden" name="confirm" value="yes" />
78 <Button type="submit" variant="lavender">
79 <Building2 size={14} />
80 Create enterprise
81 </Button>
82 <Link to="/enterprises/new" className="px-2 text-sm text-muted hover:text-fg">
83 Cancel
84 </Link>
85 </form>
86 </section>
87 )}
88
89 <Section title="Enterprise" className="mt-6">
90 <form method="post" action="/enterprises/new#review" className="space-y-4">
91 {error && <Notice tone="error">{error.error}</Notice>}
92 <Field label="Name" hint="As it should appear on the bill.">
93 <Input name="name" required maxLength={100} placeholder="Acme Corporation" defaultValue={values?.name ?? ""} />
94 </Field>
95 <Field label="Workspaces" hint="Slugs, separated by commas or one per line.">
96 <Textarea name="workspaces" required rows={4} placeholder={"acme\nacme-labs"} defaultValue={values?.workspaces ?? ""} className="font-mono" />
97 </Field>
98 <div className="flex justify-end">
99 <Button type="submit">Review</Button>
100 </div>
101 </form>
102 </Section>
103 </main>
104 );
105}