Skip to content

g1t/apps/web/app/routes/workspace/team-new.tsx

119 lines5,526 bytesCodeBlameRaw
1import { ArrowLeft } from "lucide-react";
2import { useState } from "react";
3import { Form, Link, data, redirect } from "react-router";
4
5import { TEAM_VISIBILITY_SUMMARIES, teamSlug } from "@g1t/contracts";
6
7import type { Route } from "./+types/team-new";
8import { page } from "../../lib/meta";
9import { ErrorText, Field, Input, SubmitButton } from "../../components/ui";
10import { Textarea } from "../../components/ui/textarea";
11import { RadioGroup, RadioOption } from "../../components/ui/radio-group";
12import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../components/ui/select";
13import { newTeamFromForm, parentChoices, teamPath } from "../../lib/teams";
14import { identity } from "../../lib/services.server";
15import { assertSameOrigin, requireUser, roleIn, unwrap } from "../../lib/session.server";
16
17/** The parent select's value for "no parent": Radix selects have no empty value. */
18const NO_PARENT = "-";
19
20export function meta({ params, ...args }: Route.MetaArgs) {
21 return page(args, { title: `New team · ${params.owner} · g1t` });
22}
23
24export async function loader({ params, context, request }: Route.LoaderArgs) {
25 const viewer = requireUser(context, request);
26 if (!roleIn(viewer, params.owner)) throw data(null, { status: 404 });
27 const teams = unwrap(await identity.listTeams(viewer, params.owner));
28 // A new team goes under a team its creator may manage.
29 const parents = parentChoices(teams, null).filter((team) => team.can_manage);
30 const wanted = new URL(request.url).searchParams.get("parent");
31 return { parents, parent: parents.some((team) => team.slug === wanted) ? wanted : null };
32}
33
34export async function action({ request, params, context }: Route.ActionArgs) {
35 assertSameOrigin(request);
36 const user = requireUser(context, request);
37 const form = await request.formData();
38 const team = newTeamFromForm(form);
39 if (team.parent === NO_PARENT) team.parent = null;
40 const made = await identity.createTeam(user, params.owner, team);
41 if (!made.ok) return { error: made.error.message };
42 throw redirect(teamPath(made.value.workspace, made.value.slug));
43}
44
45export default function NewTeam({ loaderData, actionData, params }: Route.ComponentProps) {
46 const { parents, parent } = loaderData;
47 const [name, setName] = useState("");
48 const [slug, setSlug] = useState("");
49 const [visibility, setVisibility] = useState("visible");
50 const handle = slug || teamSlug(name) || "team";
51 return (
52 <div className="max-w-2xl">
53 <Link to={`/${params.owner}/-/teams`} className="inline-flex items-center gap-1.5 text-sm text-muted hover:text-fg">
54 <ArrowLeft size={14} />
55 Teams
56 </Link>
57 <h1 className="mt-4 text-2xl font-semibold tracking-tight">New team</h1>
58 <p className="mt-1.5 text-sm text-muted">
59 A group of {params.owner}'s members, given roles on repositories together. You become its maintainer.
60 </p>
61
62 <Form method="post" className="mt-8 space-y-6">
63 <Field label="Name">
64 <Input name="name" required maxLength={80} value={name} onChange={(event) => setName(event.target.value)} placeholder="Backend" />
65 </Field>
66 <Field label="Slug" hint={`Mentioned as @${params.owner}/${handle}. Made from the name unless you choose one.`}>
67 <Input
68 name="slug"
69 maxLength={60}
70 value={slug}
71 onChange={(event) => setSlug(event.target.value.toLowerCase())}
72 placeholder={teamSlug(name) ?? "backend"}
73 pattern="[a-z0-9]+(-[a-z0-9]+)*"
74 />
75 </Field>
76 <Field label="Description" hint="What the team does, in a line.">
77 <Textarea name="description" rows={2} maxLength={280} />
78 </Field>
79
80 <fieldset>
81 <legend className="mb-2 text-sm font-medium text-muted">Visibility</legend>
82 <RadioGroup name="visibility" value={visibility} onValueChange={setVisibility} className="space-y-3">
83 <RadioOption value="visible" label="Visible" description={TEAM_VISIBILITY_SUMMARIES.visible} />
84 <RadioOption value="secret" label="Secret" description={TEAM_VISIBILITY_SUMMARIES.secret} />
85 </RadioGroup>
86 </fieldset>
87
88 {visibility === "visible" && parents.length > 0 && (
89 <div>
90 <span className="mb-1.5 block text-sm font-medium text-muted">Parent team</span>
91 <Select name="parent" defaultValue={parent ?? NO_PARENT}>
92 <SelectTrigger aria-label="Parent team" className="sm:max-w-72">
93 <SelectValue />
94 </SelectTrigger>
95 <SelectContent>
96 <SelectItem value={NO_PARENT}>No parent</SelectItem>
97 {parents.map((team) => (
98 <SelectItem key={team.slug} value={team.slug}>
99 {team.name}
100 </SelectItem>
101 ))}
102 </SelectContent>
103 </Select>
104 <span className="mt-1.5 block text-xs text-faint">
105 A child team has its parent's roles on repositories, and hears what is asked of its parent.
106 </span>
107 </div>
108 )}
109
110 <Field label="Members" hint="Usernames of members of the workspace, separated by spaces or commas. You can add more later.">
111 <Input name="members" placeholder="ana bo" />
112 </Field>
113
114 <ErrorText>{actionData?.error ?? null}</ErrorText>
115 <SubmitButton pending="Creating…">Create team</SubmitButton>
116 </Form>
117 </div>
118 );
119}