g1t/apps/web/app/routes/new.tsx
| 1 | import { Box, Download, GitBranch, Sparkles } from "lucide-react"; |
| 2 | import { useState } from "react"; |
| 3 | import { Form, redirect, useNavigation } from "react-router"; |
| 4 | |
| 5 | import type { Route } from "./+types/new"; |
| 6 | import { page } from "../lib/meta"; |
| 7 | import { Button, ErrorText, Field, Input } from "../components/ui"; |
| 8 | import { repos } from "../lib/services.server"; |
| 9 | import { assertSameOrigin, requireUser } from "../lib/session.server"; |
| 10 | |
| 11 | export function meta(args: Route.MetaArgs) { |
| 12 | return page(args, { title: "New project · g1t" }); |
| 13 | } |
| 14 | |
| 15 | export function loader({ request, context }: Route.LoaderArgs) { |
| 16 | const user = requireUser(context, request); |
| 17 | const workspaces = (user.workspaces ?? []).map((membership) => membership.slug); |
| 18 | // Projects live in a workspace, so there has to be one first. |
| 19 | if (workspaces.length === 0) throw redirect("/workspaces/new"); |
| 20 | const asked = new URL(request.url).searchParams.get("workspace"); |
| 21 | return { |
| 22 | workspaces, |
| 23 | selected: asked && workspaces.includes(asked) ? asked : workspaces[0], |
| 24 | }; |
| 25 | } |
| 26 | |
| 27 | export async function action({ request, context }: Route.ActionArgs) { |
| 28 | assertSameOrigin(request); |
| 29 | const user = requireUser(context, request); |
| 30 | const form = await request.formData(); |
| 31 | // A project hosted on g1t: its repository is made with it, and the |
| 32 | // project takes the repository's name. |
| 33 | const result = await repos.create(user, { |
| 34 | namespace: String(form.get("workspace") ?? ""), |
| 35 | name: String(form.get("name") ?? ""), |
| 36 | description: String(form.get("description") ?? ""), |
| 37 | isPrivate: form.get("visibility") === "private", |
| 38 | importUrl: form.get("source") === "import" ? String(form.get("importUrl") ?? "").trim() || undefined : undefined, |
| 39 | }); |
| 40 | if (!result.ok) return { error: result.error.message }; |
| 41 | throw redirect(`/${result.value.namespace}/${result.value.name}`); |
| 42 | } |
| 43 | |
| 44 | type Source = "empty" | "import"; |
| 45 | |
| 46 | const SOURCES: { id: Source | "mirror"; title: string; text: string; icon: React.ReactNode; soon?: boolean }[] = [ |
| 47 | { |
| 48 | id: "empty", |
| 49 | title: "Start empty", |
| 50 | text: "A new repository on g1t. Push to it, or hand an agent the first issue.", |
| 51 | icon: <Sparkles size={16} />, |
| 52 | }, |
| 53 | { |
| 54 | id: "import", |
| 55 | title: "Import code", |
| 56 | text: "Copy a public repository from GitHub or any git host into a new one on g1t.", |
| 57 | icon: <Download size={16} />, |
| 58 | }, |
| 59 | { |
| 60 | id: "mirror", |
| 61 | title: "Mirror GitHub, GitLab or Bitbucket", |
| 62 | text: "Keep the code where it is and get g1t's deployments and agents on it.", |
| 63 | icon: <GitBranch size={16} />, |
| 64 | soon: true, |
| 65 | }, |
| 66 | ]; |
| 67 | |
| 68 | export default function NewProject({ loaderData, actionData }: Route.ComponentProps) { |
| 69 | const [source, setSource] = useState<Source>("empty"); |
| 70 | const busy = useNavigation().state === "submitting"; |
| 71 | return ( |
| 72 | <main className="mx-auto max-w-2xl px-4 py-12"> |
| 73 | <span className="flex size-10 items-center justify-center rounded-xl bg-accent/10 text-accent ring-1 ring-accent/30"> |
| 74 | <Box size={18} /> |
| 75 | </span> |
| 76 | <h1 className="mt-4 text-2xl font-semibold tracking-tight">New project</h1> |
| 77 | <p className="mt-1.5 text-sm text-muted"> |
| 78 | A project is what you build and run. Its code lives in its source; its deployments, environments, secrets and |
| 79 | variables belong to the project. |
| 80 | </p> |
| 81 | |
| 82 | <Form method="post" className="mt-8 space-y-8"> |
| 83 | <fieldset> |
| 84 | <legend className="mb-3 text-sm font-medium text-muted">Where its code comes from</legend> |
| 85 | <div className="grid gap-3 sm:grid-cols-3"> |
| 86 | {SOURCES.map((option) => { |
| 87 | const chosen = source === option.id; |
| 88 | return ( |
| 89 | <label |
| 90 | key={option.id} |
| 91 | className={`relative rounded-xl border p-4 transition-colors ${ |
| 92 | option.soon |
| 93 | ? "cursor-not-allowed border-dashed border-line opacity-60" |
| 94 | : chosen |
| 95 | ? "cursor-pointer border-accent bg-accent/5" |
| 96 | : "cursor-pointer border-line hover:border-line-strong" |
| 97 | }`} |
| 98 | > |
| 99 | <input |
| 100 | type="radio" |
| 101 | name="source" |
| 102 | value={option.id} |
| 103 | checked={chosen} |
| 104 | disabled={option.soon} |
| 105 | onChange={() => !option.soon && setSource(option.id as Source)} |
| 106 | className="sr-only" |
| 107 | /> |
| 108 | <span className={chosen ? "text-accent" : "text-muted"}>{option.icon}</span> |
| 109 | <span className="mt-2 block text-sm font-medium">{option.title}</span> |
| 110 | <span className="mt-1 block text-xs text-muted">{option.text}</span> |
| 111 | {option.soon && ( |
| 112 | <span className="absolute top-3 right-3 rounded-full bg-raised px-2 py-0.5 text-[0.6875rem] text-muted ring-1 ring-line"> |
| 113 | Soon |
| 114 | </span> |
| 115 | )} |
| 116 | </label> |
| 117 | ); |
| 118 | })} |
| 119 | </div> |
| 120 | </fieldset> |
| 121 | |
| 122 | {source === "import" && ( |
| 123 | <Field |
| 124 | label="Repository to import" |
| 125 | hint="The address of a public repository. Its default branch is copied, up to 40 MB." |
| 126 | > |
| 127 | <Input name="importUrl" type="url" required placeholder="https://github.com/owner/repo" /> |
| 128 | </Field> |
| 129 | )} |
| 130 | |
| 131 | <Field label="Name" hint="The project and its repository share it: g1t.sh/<workspace>/<name>."> |
| 132 | <div className="flex items-center gap-2 font-mono text-sm"> |
| 133 | <select |
| 134 | name="workspace" |
| 135 | defaultValue={loaderData.selected} |
| 136 | aria-label="Workspace" |
| 137 | className="rounded-md border border-line bg-bg px-2 py-2 text-sm" |
| 138 | > |
| 139 | {loaderData.workspaces.map((slug) => ( |
| 140 | <option key={slug} value={slug}> |
| 141 | {slug} |
| 142 | </option> |
| 143 | ))} |
| 144 | </select> |
| 145 | <span className="text-muted">/</span> |
| 146 | <Input name="name" required autoFocus maxLength={100} placeholder="my-app" /> |
| 147 | </div> |
| 148 | </Field> |
| 149 | <Field label="Description (optional)"> |
| 150 | <Input name="description" maxLength={200} placeholder="What it is, in a line" /> |
| 151 | </Field> |
| 152 | <fieldset className="space-y-2 text-sm"> |
| 153 | <legend className="mb-2 font-medium text-muted">Who can see it</legend> |
| 154 | <label className="flex items-center gap-2"> |
| 155 | <input type="radio" name="visibility" value="public" defaultChecked className="accent-accent" /> |
| 156 | Public: anyone can see its code and open issues |
| 157 | </label> |
| 158 | <label className="flex items-center gap-2"> |
| 159 | <input type="radio" name="visibility" value="private" className="accent-accent" /> |
| 160 | Private: only members of the workspace |
| 161 | </label> |
| 162 | </fieldset> |
| 163 | <ErrorText>{actionData?.error}</ErrorText> |
| 164 | <Button type="submit" variant="accent" disabled={busy}> |
| 165 | Create project |
| 166 | </Button> |
| 167 | </Form> |
| 168 | </main> |
| 169 | ); |
| 170 | } |