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