g1t/apps/web/app/routes/new.tsx

220 lines9,268 bytesCodeBlame
1import { Box, Download, GitBranch, Globe, Lock, Sparkles } from "lucide-react";
2import { useState } from "react";
3import { Form, redirect, useNavigate } from "react-router";
4
5import type { Route } from "./+types/new";
6import { page } from "../lib/meta";
7import { Avatar, ErrorText, SubmitButton } from "../components/ui";
8import { Field, FieldDescription, FieldLabel, FieldLegend, FieldSet } from "../components/ui/field";
9import { Input, InputAddon, InputGroup } from "../components/ui/input";
10import { RadioCard, RadioGroup } from "../components/ui/radio-group";
11import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue } from "../components/ui/select";
12import { repos } from "../lib/services.server";
13import { GithubMark } from "../components/github";
14import { githubApp } from "../lib/github.server";
15import { assertSameOrigin, requireUser } from "../lib/session.server";
16
17export function meta(args: Route.MetaArgs) {
18 return page(args, { title: "New project · g1t" });
19}
20
21export async function loader({ request, context }: Route.LoaderArgs) {
22 const user = requireUser(context, request);
23 const workspaces = (user.workspaces ?? []).map((membership) => ({
24 slug: membership.slug,
25 role: membership.role,
26 name: membership.name ?? membership.slug,
27 avatar: membership.avatar ?? null,
28 }));
29 // Projects live in a workspace, so there has to be one first.
30 if (workspaces.length === 0) throw redirect("/workspaces/new");
31 const asked = new URL(request.url).searchParams.get("workspace");
32 const selected = asked && workspaces.some((workspace) => workspace.slug === asked) ? asked : workspaces[0].slug;
33 // With g1t's GitHub App configured, repositories can come from GitHub.
34 const github = await githubApp.status(user, selected).catch(() => null);
35 return { workspaces, selected, github: Boolean(github?.ok && github.value.configured) };
36}
37
38export async function action({ request, context }: Route.ActionArgs) {
39 assertSameOrigin(request);
40 const user = requireUser(context, request);
41 const form = await request.formData();
42 // A project hosted on g1t: its repository is made with it, and the
43 // project takes the repository's name.
44 const result = await repos.create(user, {
45 namespace: String(form.get("workspace") ?? ""),
46 name: String(form.get("name") ?? ""),
47 description: String(form.get("description") ?? ""),
48 isPrivate: form.get("visibility") === "private",
49 importUrl: form.get("source") === "import" ? String(form.get("importUrl") ?? "").trim() || undefined : undefined,
50 });
51 if (!result.ok) return { error: result.error.message };
52 throw redirect(`/${result.value.namespace}/${result.value.name}`);
53}
54
55type Source = "empty" | "import";
56
57const SOURCES: { id: Source | "mirror"; title: string; text: string; icon: React.ReactNode; soon?: boolean }[] = [
58 {
59 id: "empty",
60 title: "Start empty",
61 text: "A new repository on g1t. Push to it, or hand an agent the first issue.",
62 icon: <Sparkles />,
63 },
64 {
65 id: "import",
66 title: "Import code",
67 text: "Copy a public repository from GitHub or any git host into a new one on g1t.",
68 icon: <Download />,
69 },
70 {
71 id: "mirror",
72 title: "Mirror GitHub, GitLab or Bitbucket",
73 text: "Keep the code where it is and work on it with g1t. Deployments are yours to turn on.",
74 icon: <GitBranch />,
75 soon: true,
76 },
77];
78
79const ROLE_LABELS: Record<string, string> = { owner: "Owner", admin: "Admin", member: "Member" };
80
81/** Replaces the mirror card when g1t's GitHub App is configured. */
82const GITHUB_SOURCE = {
83 id: "github" as const,
84 title: "Import from GitHub",
85 text: "Import, mirror or move repositories you can reach on GitHub, private ones too.",
86 icon: <GithubMark />,
87 soon: false,
88};
89
90export default function NewProject({ loaderData, actionData }: Route.ComponentProps) {
91 const [source, setSource] = useState<Source>("empty");
92 const navigate = useNavigate();
93 const sources = loaderData.github ? SOURCES.map((option) => (option.id === "mirror" ? GITHUB_SOURCE : option)) : SOURCES;
94 const [workspace, setWorkspace] = useState(loaderData.selected);
95 const [name, setName] = useState("");
96 return (
97 <main className="mx-auto max-w-2xl px-4 py-12">
98 <span className="flex size-10 items-center justify-center rounded-xl bg-accent/10 text-accent ring-1 ring-accent/30">
99 <Box size={18} />
100 </span>
101 <h1 className="mt-4 text-2xl font-semibold tracking-tight">New project</h1>
102 <p className="mt-1.5 text-sm text-muted">
103 A project is what you build and run. Its code lives in its source; its deployments, environments, secrets and
104 variables belong to the project.
105 </p>
106
107 <Form method="post" className="mt-8 space-y-8">
108 <FieldSet>
109 <FieldLegend>Where its code comes from</FieldLegend>
110 <RadioGroup
111 name="source"
112 value={source}
113 onValueChange={(value) =>
114 value === "github" ? navigate(`/new/github?workspace=${workspace}`) : setSource(value as Source)
115 }
116 aria-label="Where its code comes from"
117 className="gap-3 sm:grid-cols-3"
118 >
119 {sources.map((option) => (
120 <RadioCard
121 key={option.id}
122 value={option.id}
123 title={option.title}
124 description={option.text}
125 icon={option.icon}
126 disabled={option.soon}
127 badge={option.soon ? "Soon" : undefined}
128 />
129 ))}
130 </RadioGroup>
131 </FieldSet>
132
133 {source === "import" && (
134 <Field className="animate-fade-in">
135 <FieldLabel htmlFor="importUrl">Repository to import</FieldLabel>
136 <Input id="importUrl" name="importUrl" type="url" required placeholder="https://github.com/owner/repo" />
137 <FieldDescription>The address of a public repository. Its default branch is copied, up to 40 MB.</FieldDescription>
138 </Field>
139 )}
140
141 <Field>
142 <FieldLabel htmlFor="name">Name</FieldLabel>
143 <InputGroup className="h-10">
144 <Select name="workspace" value={workspace} onValueChange={setWorkspace}>
145 <SelectTrigger
146 aria-label="Workspace"
147 className="h-full w-auto max-w-[55%] shrink-0 rounded-none border-0 bg-surface px-2.5 font-medium hover:bg-raised focus-visible:ring-0 data-[state=open]:bg-raised data-[state=open]:ring-0 sm:max-w-[45%]"
148 >
149 <SelectValue />
150 </SelectTrigger>
151 <SelectContent align="start" className="min-w-64">
152 <SelectGroup>
153 <SelectLabel>Workspaces</SelectLabel>
154 {loaderData.workspaces.map((option) => (
155 <SelectItem
156 key={option.slug}
157 value={option.slug}
158 icon={<Avatar name={option.name} image={option.avatar} size={18} square />}
159 description={`g1t.sh/${option.slug} · ${ROLE_LABELS[option.role] ?? option.role}`}
160 >
161 {option.name}
162 </SelectItem>
163 ))}
164 </SelectGroup>
165 </SelectContent>
166 </Select>
167 <InputAddon className="border-l border-line px-2 font-mono text-base text-faint">/</InputAddon>
168 <Input
169 id="name"
170 name="name"
171 required
172 autoFocus
173 maxLength={100}
174 placeholder="my-app"
175 value={name}
176 onChange={(event) => setName(event.target.value)}
177 className="pl-1 font-mono"
178 />
179 </InputGroup>
180 <FieldDescription>
181 The project and its repository share it:{" "}
182 <span className="font-mono text-muted">
183 g1t.sh/{workspace}/<span className={name ? "text-fg" : undefined}>{name || "name"}</span>
184 </span>
185 </FieldDescription>
186 </Field>
187
188 <Field>
189 <FieldLabel htmlFor="description">
190 Description <span className="font-normal text-faint">(optional)</span>
191 </FieldLabel>
192 <Input id="description" name="description" maxLength={200} placeholder="What it is, in a line" />
193 </Field>
194
195 <FieldSet>
196 <FieldLegend>Who can see it</FieldLegend>
197 <RadioGroup name="visibility" defaultValue="public" aria-label="Who can see it" className="gap-3 sm:grid-cols-2">
198 <RadioCard
199 value="public"
200 icon={<Globe />}
201 title="Public"
202 description="Anyone can see its code and open issues."
203 />
204 <RadioCard
205 value="private"
206 icon={<Lock />}
207 title="Private"
208 description="Only members of the workspace."
209 />
210 </RadioGroup>
211 </FieldSet>
212
213 <ErrorText>{actionData?.error}</ErrorText>
214 <SubmitButton variant="accent" pending={source === "import" ? "Importing…" : "Creating…"}>
215 Create project
216 </SubmitButton>
217 </Form>
218 </main>
219 );
220}