106 lines4,866 bytesCodeBlame
1import { Box, Code2, GitBranch } from "lucide-react";
2import { Form, Link, useNavigation } from "react-router";
3
4import type { Route } from "./+types/settings";
5import { page } from "../../lib/meta";
6import { RepoSettingsHeading } from "../../components/repo-settings-heading";
7import { Button, ErrorText, Field, Input, TimeAgo } from "../../components/ui";
8import { projects } from "../../lib/services.server";
9import { assertSameOrigin, getViewer, requireUser, unwrap } from "../../lib/session.server";
10import { requireCapability, requireInsider } from "../../lib/access.server";
11
12export function meta({ params, ...args }: Route.MetaArgs) {
13 return page(args, { title: `Settings · ${params.owner}/${params.repo} · g1t` });
14}
15
16export async function loader({ params, context }: Route.LoaderArgs) {
17 const viewer = getViewer(context);
18 // Maintain and up; to anyone without a role here the page does not exist.
19 await requireInsider(context, params, "manage_settings");
20 return { project: unwrap(await projects.get(params.owner, params.repo, viewer)) };
21}
22
23export async function action({ request, params, context }: Route.ActionArgs) {
24 assertSameOrigin(request);
25 const user = requireUser(context, request);
26 await requireCapability(context, params, "manage_settings");
27 const form = await request.formData();
28 const saved = await projects.update(user, params.owner, params.repo, {
29 name: String(form.get("name") ?? ""),
30 description: String(form.get("description") ?? ""),
31 rootDir: String(form.get("rootDir") ?? ""),
32 });
33 return saved.ok ? { saved: true as const } : { error: saved.error.message };
34}
35
36export default function ProjectSettings({ loaderData, actionData, params }: Route.ComponentProps) {
37 const { project } = loaderData;
38 const saving = useNavigation().state === "submitting";
39 const base = `/${params.owner}/${params.repo}`;
40 const source = project.source.kind === "hosted" ? project.source : null;
41 return (
42 <div className="max-w-4xl">
43 <RepoSettingsHeading base={base} />
44 <Form method="post" className="space-y-8">
45 <section className="space-y-4">
46 <h2 className="flex items-center gap-2 text-sm font-medium">
47 <Box size={15} className="text-accent" />
48 Project
49 </h2>
50 <div className="grid gap-4 md:grid-cols-2">
51 <Field label="Name" hint={`Its address stays g1t.sh/${project.workspace}/${project.slug}.`}>
52 <Input name="name" defaultValue={project.name} required maxLength={100} />
53 </Field>
54 <Field label="Description">
55 <Input name="description" defaultValue={project.description ?? ""} maxLength={200} placeholder="What it is, in a line" />
56 </Field>
57 </div>
58 </section>
59
60 <section className="space-y-4">
61 <h2 className="flex items-center gap-2 text-sm font-medium">
62 <Code2 size={15} className="text-accent" />
63 Source
64 </h2>
65 {source && (
66 <div className="rounded-xl border border-line bg-surface p-4 text-sm">
67 <div className="flex flex-wrap items-center justify-between gap-3">
68 <div>
69 <Link to={`${base}/code`} className="font-mono hover:text-accent">
70 {source.repo.namespace}/{source.repo.name}
71 </Link>
72 <p className="mt-0.5 text-xs text-muted">
73 Hosted on g1t · <GitBranch size={11} className="inline" /> {source.defaultBranch}
74 </p>
75 </div>
76 <Link to={`${base}/settings/repository`} className="text-xs text-muted hover:text-fg">
77 Repository settings
78 </Link>
79 </div>
80 <p className="mt-3 text-xs text-faint">
81 Mirroring a repository from GitHub, GitLab or Bitbucket as a project's source is coming next.
82 </p>
83 </div>
84 )}
85 <Field
86 label="Root directory"
87 hint="Where in the repository this project lives, such as apps/web. Builds run there. Empty for the whole repository."
88 >
89 <Input name="rootDir" defaultValue={source?.rootDir ?? ""} placeholder="apps/web" className="font-mono" />
90 </Field>
91 </section>
92
93 <div className="flex items-center gap-3">
94 <Button type="submit" disabled={saving}>
95 Save
96 </Button>
97 {actionData && "saved" in actionData && <span className="text-sm text-accent">Saved.</span>}
98 <span className="ml-auto text-xs text-faint">
99 Created by {project.createdBy} <TimeAgo at={project.createdAt} />
100 </span>
101 </div>
102 <ErrorText>{actionData && "error" in actionData ? actionData.error : null}</ErrorText>
103 </Form>
104 </div>
105 );
106}