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.

199 linesCodeBlameRaw
1import { Box, Download, 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 { 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 { projects, repos } from "../lib/services.server";
12import { GithubMark } from "../components/github";
13import { githubApp } from "../lib/github.server";
14import { assertSameOrigin, requireUser } from "../lib/session.server";
15import { currentWorkspace } from "../lib/current-workspace.server";
16import { Unavailable } from "../components/boundary";
17import { isFailed, part } from "../lib/partial";
18
19export function meta(args: Route.MetaArgs) {
20 return page(args, { title: "New project · g1t" });
21}
22
23export async function loader({ request, context }: Route.LoaderArgs) {
24 const user = requireUser(context, request);
25 // Projects live in a workspace, so there has to be one first; a new one
26 // goes in the workspace you're in.
27 const current = currentWorkspace(user, request);
28 if (!current) throw redirect("/workspaces/new");
29 const selected = current.slug;
30 // With g1t's GitHub App configured, repositories can come from GitHub.
31 // Not asked is not "not set up": the other sources stay, and the picker
32 // says that importing from GitHub could not be checked.
33 const github = await part(githubApp.status(user, selected), "Importing from GitHub");
34 return {
35 selected,
36 name: current.name?.trim() || current.slug,
37 github: !isFailed(github) && github.ok && github.value.configured,
38 githubUnknown: isFailed(github),
39 };
40}
41
42export async function action({ request, context }: Route.ActionArgs) {
43 assertSameOrigin(request);
44 const user = requireUser(context, request);
45 const form = await request.formData();
46 // A project hosted on g1t: its repository is made with it, and the
47 // project takes the repository's name.
48 const result = await repos.create(user, {
49 namespace: String(form.get("workspace") ?? ""),
50 name: String(form.get("name") ?? ""),
51 isPrivate: form.get("visibility") === "private",
52 importUrl: form.get("source") === "import" ? String(form.get("importUrl") ?? "").trim() || undefined : undefined,
53 });
54 if (!result.ok) return { error: result.error.message };
55 // A repository has no description: the one given is its project's.
56 const description = String(form.get("description") ?? "").trim();
57 if (description) {
58 const described = await projects.describe(user, result.value.id, description).catch((error: unknown) => String(error));
59 if (typeof described === "string" || !described.ok) console.error("new repository without its description", result.value.id, described);
60 }
61 throw redirect(`/${result.value.namespace}/${result.value.name}`);
62}
63
64type Source = "empty" | "import";
65
66const SOURCES: { id: Source | "github"; title: string; text: string; icon: React.ReactNode }[] = [
67 {
68 id: "empty",
69 title: "Start empty",
70 text: "A new repository on g1t. Push to it, or hand your coding agent the first issue.",
71 icon: <Sparkles />,
72 },
73 {
74 id: "import",
75 title: "Import code",
76 text: "Copy a public repository from GitHub or any git host into a new one on g1t.",
77 icon: <Download />,
78 },
79];
80
81/** Shown when g1t's GitHub App is configured. */
82const GITHUB_SOURCE = {
83 id: "github" as const,
84 title: "Import from GitHub",
85 text: "Import repositories you can reach on GitHub, private ones too, with their issues.",
86 icon: <GithubMark />,
87};
88
89export default function NewProject({ loaderData, actionData }: Route.ComponentProps) {
90 const [source, setSource] = useState<Source>("empty");
91 const navigate = useNavigate();
92 const sources = loaderData.github ? [...SOURCES, GITHUB_SOURCE] : SOURCES;
93 const workspace = loaderData.selected;
94 const [name, setName] = useState("");
95 return (
96 <main className="mx-auto max-w-2xl px-4 py-12">
97 <span className="flex size-10 items-center justify-center rounded-xl bg-accent/10 text-accent ring-1 ring-accent/30">
98 <Box size={18} />
99 </span>
100 <h1 className="mt-4 text-2xl font-semibold tracking-tight">New project</h1>
101 <p className="mt-1.5 text-sm text-muted">
102 A project is what you build and run. Its code lives in its source; its deployments, environments, secrets and
103 variables belong to the project.
104 </p>
105
106 <Form method="post" className="mt-8 space-y-8">
107 <FieldSet>
108 <FieldLegend>Where its code comes from</FieldLegend>
109 <RadioGroup
110 name="source"
111 value={source}
112 onValueChange={(value) =>
113 value === "github" ? navigate("/new/github") : setSource(value as Source)
114 }
115 aria-label="Where its code comes from"
116 className="gap-3 sm:grid-cols-3"
117 >
118 {sources.map((option) => (
119 <RadioCard
120 key={option.id}
121 value={option.id}
122 title={option.title}
123 description={option.text}
124 icon={option.icon}
125 />
126 ))}
127 </RadioGroup>
128 {loaderData.githubUnknown && <Unavailable what="Importing from GitHub" compact />}
129 </FieldSet>
130
131 {source === "import" && (
132 <Field className="animate-fade-in">
133 <FieldLabel htmlFor="importUrl">Repository to import</FieldLabel>
134 <Input id="importUrl" name="importUrl" type="url" required placeholder="https://github.com/owner/repo" />
135 <FieldDescription>The address of a public repository. Its default branch is copied, up to 40 MB.</FieldDescription>
136 </Field>
137 )}
138
139 <Field>
140 <FieldLabel htmlFor="name">Name</FieldLabel>
141 <InputGroup className="h-10">
142 <input type="hidden" name="workspace" value={workspace} />
143 <InputAddon className="max-w-[55%] shrink-0 gap-2 bg-surface px-2.5 font-medium text-fg sm:max-w-[45%]">
144 <span className="truncate">{loaderData.name}</span>
145 </InputAddon>
146 <InputAddon className="border-l border-line px-2 font-mono text-base text-faint">/</InputAddon>
147 <Input
148 id="name"
149 name="name"
150 required
151 autoFocus
152 maxLength={100}
153 placeholder="my-app"
154 value={name}
155 onChange={(event) => setName(event.target.value)}
156 className="pl-1 font-mono"
157 />
158 </InputGroup>
159 <FieldDescription>
160 The project and its repository share it:{" "}
161 <span className="font-mono text-muted">
162 g1t.sh/{workspace}/<span className={name ? "text-fg" : undefined}>{name || "name"}</span>
163 </span>
164 </FieldDescription>
165 </Field>
166
167 <Field>
168 <FieldLabel htmlFor="description">
169 Description <span className="font-normal text-faint">(optional)</span>
170 </FieldLabel>
171 <Input id="description" name="description" maxLength={200} placeholder="What it is, in a line" />
172 </Field>
173
174 <FieldSet>
175 <FieldLegend>Who can see it</FieldLegend>
176 <RadioGroup name="visibility" defaultValue="public" aria-label="Who can see it" className="gap-3 sm:grid-cols-2">
177 <RadioCard
178 value="public"
179 icon={<Globe />}
180 title="Public"
181 description="Anyone can see its code and open issues."
182 />
183 <RadioCard
184 value="private"
185 icon={<Lock />}
186 title="Private"
187 description="Only members of the workspace."
188 />
189 </RadioGroup>
190 </FieldSet>
191
192 <ErrorText>{actionData?.error}</ErrorText>
193 <SubmitButton variant="accent" pending={source === "import" ? "Importing…" : "Creating…"}>
194 Create project
195 </SubmitButton>
196 </Form>
197 </main>
198 );
199}