Skip to content

g1t/apps/web/app/routes/repo/settings.tsx

208 lines10,024 bytesCodeBlame
1import { Box, Code2, GitBranch, Link2, Rocket } from "lucide-react";
2import { useState } from "react";
3import { Form, Link } from "react-router";
4
5import type { Route } from "./+types/settings";
6import { page } from "../../lib/meta";
7import { KindFields, LinkFields } from "../../components/project-about";
8import { RepoSettingsHeading } from "../../components/repo-settings-heading";
9import { ErrorText, Field, Input, SubmitButton, TimeAgo } from "../../components/ui";
10import { KIND_CHOICE_SETS, type KindChoice, choiceOf, linksFromForm, neverDeploys } from "../../lib/project-kind";
11import { deployments, identity, projects } from "../../lib/services.server";
12import { assertSameOrigin, getViewer, requireUser, unwrap } from "../../lib/session.server";
13import { requireCapability, requireInsider } from "../../lib/access.server";
14
15export function meta({ params, ...args }: Route.MetaArgs) {
16 return page(args, { title: `Settings · ${params.owner}/${params.repo} · g1t` });
17}
18
19export async function loader({ params, context }: Route.LoaderArgs) {
20 const viewer = getViewer(context);
21 // Maintain and up; to anyone without a role here the page does not exist.
22 await requireInsider(context, params, "manage_settings");
23 const [project, deploys] = await Promise.all([
24 projects.get(params.owner, params.repo, viewer),
25 // Whether Deployments are on: needs Admin, so unknown (null) below it.
26 deployments.settings({ workspace: params.owner, slug: params.repo }, viewer).catch(() => null),
27 ]);
28 const found = unwrap(project);
29 // Who made it is kept as an id: named by identity, or left out if it cannot say.
30 const names = await identity.usernames([found.createdBy]).catch(() => ({}) as Record<string, string>);
31 return {
32 project: found,
33 deploymentsOn: deploys?.ok ? deploys.value.enabled : null,
34 mine: viewer?.id === found.createdBy,
35 creator: names[found.createdBy] ?? null,
36 };
37}
38
39export async function action({ request, params, context }: Route.ActionArgs) {
40 assertSameOrigin(request);
41 const user = requireUser(context, request);
42 await requireCapability(context, params, "manage_settings");
43 const form = await request.formData();
44 const choice = form.get("choice") as KindChoice | null;
45 const set = choice && choice in KIND_CHOICE_SETS ? KIND_CHOICE_SETS[choice] : null;
46 const saved = await projects.update(user, params.owner, params.repo, {
47 name: String(form.get("name") ?? ""),
48 // Blank, or the reset, and it follows the repository's description again.
49 description: form.get("inherit") === "description" ? null : String(form.get("description") ?? ""),
50 rootDir: String(form.get("rootDir") ?? ""),
51 ...(set ?? {}),
52 // Production's address is asked for beside Deployed elsewhere, and kept otherwise.
53 ...(form.has("productionUrl") ? { productionUrl: String(form.get("productionUrl")) } : {}),
54 homepage: String(form.get("homepage") ?? ""),
55 docsUrl: String(form.get("docsUrl") ?? ""),
56 links: linksFromForm(form),
57 });
58 return saved.ok ? { saved: true as const } : { error: saved.error.message };
59}
60
61export default function ProjectSettings({ loaderData, actionData, params }: Route.ComponentProps) {
62 const { project, deploymentsOn, mine, creator } = loaderData;
63 const [choice, setChoice] = useState<KindChoice | null>(choiceOf(project.setting));
64 // Something that never deploys, while Deployments are on, is refused: they are turned off first.
65 const stops = choice === "library" || choice === "tool" || choice === "other";
66 const blocked = stops && !neverDeploys(project) && deploymentsOn === true;
67 const base = `/${params.owner}/${params.repo}`;
68 const source = project.source.kind === "hosted" ? project.source : null;
69 return (
70 <div className="max-w-4xl">
71 <RepoSettingsHeading base={base} />
72 <Form method="post" className="space-y-8">
73 {/* Enter in a field saves: the form's first submit button is the one
74 Enter presses, and otherwise it would be the description reset. */}
75 <button type="submit" name="intent" value="save" tabIndex={-1} aria-hidden="true" className="absolute size-0 overflow-hidden opacity-0" />
76 <section className="space-y-4">
77 <h2 className="flex items-center gap-2 text-sm font-medium">
78 <Box size={15} className="text-accent" />
79 Project
80 </h2>
81 <div className="grid gap-4 md:grid-cols-2">
82 <Field label="Name" hint={`Its address stays g1t.sh/${project.workspace}/${project.slug}.`}>
83 <Input name="name" defaultValue={project.name} required maxLength={100} />
84 </Field>
85 <div>
86 <Field
87 label="Description"
88 hint={
89 project.descriptionInherited
90 ? "Follows the repository's description. Write one to give the project its own."
91 : "The project's own. Empty follows the repository's description."
92 }
93 >
94 <Input
95 key={`${project.descriptionInherited}:${project.description ?? ""}`}
96 name="description"
97 defaultValue={project.descriptionInherited ? "" : (project.description ?? "")}
98 maxLength={200}
99 placeholder={(project.descriptionInherited && project.description) || "What it is, in a line"}
100 />
101 </Field>
102 {!project.descriptionInherited && (
103 <SubmitButton
104 name="inherit"
105 value="description"
106 pending="Resetting…"
107 className="mt-1.5 inline-flex items-center gap-1.5 text-xs text-muted hover:text-fg disabled:opacity-50"
108 >
109 Use the repository's description
110 </SubmitButton>
111 )}
112 </div>
113 </div>
114 </section>
115
116 <section className="space-y-4">
117 <h2 className="flex items-center gap-2 text-sm font-medium">
118 <Code2 size={15} className="text-accent" />
119 Source
120 </h2>
121 {source && (
122 <div className="rounded-xl border border-line bg-surface p-4 text-sm">
123 <div className="flex flex-wrap items-center justify-between gap-3">
124 <div>
125 <Link to={`${base}/code`} className="font-mono hover:text-accent">
126 {source.repo.namespace}/{source.repo.name}
127 </Link>
128 <p className="mt-0.5 text-xs text-muted">
129 Hosted on g1t · <GitBranch size={11} className="inline" /> {source.defaultBranch}
130 </p>
131 </div>
132 <Link to={`${base}/settings/repository`} className="text-xs text-muted hover:text-fg">
133 Repository settings
134 </Link>
135 </div>
136 <p className="mt-3 text-xs text-faint">
137 Mirroring a repository from GitHub, GitLab or Bitbucket as a project's source is coming next.
138 </p>
139 </div>
140 )}
141 <Field
142 label="Root directory"
143 hint="Where in the repository this project lives, such as apps/web. Builds run there. Empty for the whole repository."
144 >
145 <Input name="rootDir" defaultValue={source?.rootDir ?? ""} placeholder="apps/web" className="font-mono" />
146 </Field>
147 </section>
148
149 <section className="scroll-mt-20 space-y-4" id="kind">
150 {/* Old links to #deploys land here too. */}
151 <span id="deploys" />
152 <h2 className="flex items-center gap-2 text-sm font-medium">
153 <Rocket size={15} className="text-accent" />
154 What it is
155 </h2>
156 <p className="max-w-2xl text-xs text-muted">
157 Its overview follows: production for what is deployed, packages and releases for a library or a tool, and
158 the steps that apply to it. Only an app or site deployed on g1t is asked to turn on Deployments.
159 </p>
160 <KindFields project={project} choice={choice} onChoice={setChoice} />
161 {blocked && (
162 <p className="rounded-lg border border-warn/40 bg-warn/5 px-3.5 py-2.5 text-sm text-fg-soft">
163 Deployments are on for {project.name}. Turn them off in{" "}
164 <Link to={`${base}/settings/deployments`} className="text-fg underline underline-offset-4">
165 Deployments settings
166 </Link>{" "}
167 first, then choose this here.
168 </p>
169 )}
170 </section>
171
172 <section className="scroll-mt-20 space-y-4" id="links">
173 <h2 className="flex items-center gap-2 text-sm font-medium">
174 <Link2 size={15} className="text-accent" />
175 Links
176 </h2>
177 <p className="max-w-2xl text-xs text-muted">
178 Shown on its overview, its card, and its workspace's Projects. Each is an http or https address;
179 https:// is added when you leave it out.
180 </p>
181 <LinkFields links={project.links} />
182 </section>
183
184 <div className="flex items-center gap-3">
185 <SubmitButton name="intent" value="save" pending="Saving…" disabled={blocked}>
186 Save
187 </SubmitButton>
188 {actionData && "saved" in actionData && <span className="text-sm text-success">Saved.</span>}
189 <span className="ml-auto text-xs text-faint">
190 Created{" "}
191 {mine ? (
192 "by you "
193 ) : creator ? (
194 <>
195 by{" "}
196 <Link to={`/${creator}`} className="text-muted hover:text-fg">
197 {creator}
198 </Link>{" "}
199 </>
200 ) : null}
201 <TimeAgo at={project.createdAt} />
202 </span>
203 </div>
204 <ErrorText>{actionData && "error" in actionData ? actionData.error : null}</ErrorText>
205 </Form>
206 </div>
207 );
208}