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.

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