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