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

187 lines8,613 bytesCodeBlame
1import { Form, Link, useNavigation } from "react-router";
2
3import type { DetectedKind } from "@g1t/contracts";
4
5import type { Route } from "./+types/settings-deployments";
6import { page } from "../../lib/meta";
7import { RepoSettingsHeading } from "../../components/repo-settings-heading";
8import { Button, ErrorText, Field, Input } from "../../components/ui";
9import { SwitchCard } from "../../components/ui/switch";
10import { deployments, projects } from "../../lib/services.server";
11import { assertSameOrigin, getViewer, requireUser, unwrap } from "../../lib/session.server";
12import { requireCapability, requireInsider } from "../../lib/access.server";
13
14export function meta({ params, ...args }: Route.MetaArgs) {
15 return page(args, { title: `Deployment settings · ${params.owner}/${params.repo} · g1t` });
16}
17
18export async function loader({ params, context }: Route.LoaderArgs) {
19 const viewer = getViewer(context);
20 // Admins; to anyone without a role here the page does not exist.
21 await requireInsider(context, params, "manage_integrations");
22 const [settings, project] = await Promise.all([
23 deployments.settings({ workspace: params.owner, slug: params.repo }, viewer),
24 projects.get(params.owner, params.repo, viewer).catch(() => null),
25 ]);
26 // Set not to deploy in General settings: turning them on is refused until that changes.
27 const notDeploying = project?.ok ? project.value.deploys === "no" : false;
28 return { settings: unwrap(settings), notDeploying };
29}
30
31export async function action({ request, params, context }: Route.ActionArgs) {
32 assertSameOrigin(request);
33 const user = requireUser(context, request);
34 await requireCapability(context, params, "manage_integrations");
35 const form = await request.formData();
36 const ref = { workspace: params.owner, slug: params.repo };
37 const intent = form.get("intent");
38 if (intent === "enable" || intent === "disable") {
39 const saved = await deployments.updateSettings(user, ref, { enabled: intent === "enable" });
40 return saved.ok
41 ? { notice: intent === "enable" ? "Deployments are on. Production is building." : "Deployments are off, and every app is down." }
42 : { error: saved.error.message };
43 }
44 const on = (name: string) => form.get(name) === "on";
45 const saved = await deployments.updateSettings(user, ref, {
46 previews: on("previews"),
47 production: on("production"),
48 buildCommand: String(form.get("buildCommand") ?? ""),
49 outputDir: String(form.get("outputDir") ?? ""),
50 idleDays: Number(form.get("idleDays")),
51 });
52 return saved.ok ? { notice: "Saved." } : { error: saved.error.message };
53}
54
55function Check({ name, on, title, children }: { name: string; on: boolean; title: string; children: string }) {
56 return (
57 <SwitchCard name={name} defaultChecked={on} title={title}>
58 {children}
59 </SwitchCard>
60 );
61}
62
63/** What each kind of project is called, and what running it costs, in a line. */
64const DETECTED: Record<DetectedKind, { label: string; cost: string }> = {
65 static: {
66 label: "Static site",
67 cost: "Serving its files runs no code, so it adds no requests or CPU time. Builds are metered.",
68 },
69 html: {
70 label: "Plain HTML",
71 cost: "Its files are served as they are and run no code, so serving adds no requests or CPU time. Builds are metered.",
72 },
73 workers: {
74 label: "Workers project",
75 cost: "Its code runs on every request: requests and CPU time are metered, and so are builds.",
76 },
77};
78
79/** What g1t found the project to be at its last build, and what that costs. */
80function Detected({ kind }: { kind: DetectedKind | null }) {
81 const found = kind ? DETECTED[kind] : null;
82 return (
83 <section className="mb-6 rounded-xl border border-line bg-surface p-5">
84 <h2 className="text-sm font-medium">What g1t detected</h2>
85 <p className="mt-1 text-sm">
86 {found ? found.label : <span className="text-muted">Detected at the first build</span>}
87 </p>
88 <p className="mt-1 text-xs text-muted">
89 {found
90 ? found.cost
91 : "A static site's files cost nothing to serve; a Workers project's requests and CPU time are metered. Builds are metered either way."}
92 </p>
93 </section>
94 );
95}
96
97export default function DeploymentSettings({ loaderData, actionData, params }: Route.ComponentProps) {
98 const { settings, notDeploying } = loaderData;
99 const busy = useNavigation().state === "submitting";
100 const base = `/${params.owner}/${params.repo}`;
101 return (
102 <div className="max-w-4xl">
103 <RepoSettingsHeading base={base} />
104 <div className="min-h-6">
105 {actionData && "notice" in actionData && <p className="text-sm text-accent">{actionData.notice}</p>}
106 <ErrorText>{actionData && "error" in actionData ? actionData.error : null}</ErrorText>
107 </div>
108
109 <Detected kind={settings.detected ?? null} />
110
111 {!settings.enabled ? (
112 <section className="rounded-xl border border-line bg-surface p-5">
113 <h2 className="font-medium">Deployments are off</h2>
114 <p className="mt-1 text-sm text-muted">
115 Deployments are opt-in for each project, and nothing builds or runs until you turn them on. Then g1t builds
116 production at{" "}
117 <span className="font-mono text-fg">{settings.productionUrl.replace("https://", "")}</span> and a preview for
118 every pull request. The workspace needs the g1t plan, under Billing.
119 </p>
120 {notDeploying ? (
121 <p className="mt-4 text-sm text-muted">
122 This project is set as one that doesn't deploy, a library or a tool. To deploy it, choose Deploys or Detect
123 automatically under{" "}
124 <Link to={`${base}/settings#deploys`} className="text-fg underline underline-offset-4">
125 General
126 </Link>{" "}
127 first.
128 </p>
129 ) : (
130 <Form method="post" className="mt-4">
131 <Button variant="accent" type="submit" name="intent" value="enable" disabled={busy}>
132 Turn on deployments
133 </Button>
134 </Form>
135 )}
136 </section>
137 ) : (
138 <>
139 <Form method="post" className="space-y-5">
140 <div className="grid gap-3 md:grid-cols-2">
141 <Check name="production" on={settings.production} title="Production">
142 Deploy the default branch on every push.
143 </Check>
144 <Check name="previews" on={settings.previews} title="Previews">
145 A preview for every branch with an open pull request, linked on it.
146 </Check>
147 </div>
148 <div className="grid gap-4 md:grid-cols-3">
149 <Field label="Build command" hint="Instead of the project's own build script.">
150 <Input name="buildCommand" defaultValue={settings.buildCommand ?? ""} placeholder="npm run build" />
151 </Field>
152 <Field label="Output directory" hint="For a static site; found by itself when empty.">
153 <Input name="outputDir" defaultValue={settings.outputDir ?? ""} placeholder="dist" />
154 </Field>
155 <Field label="Idle days" hint="A preview no one visits for this long comes down.">
156 <Input name="idleDays" type="number" min={1} max={90} defaultValue={settings.idleDays} />
157 </Field>
158 </div>
159 <p className="text-sm text-muted">
160 Builds and running apps read the{" "}
161 <Link to={`${base}/settings/secrets`} className="text-fg hover:underline">
162 secrets and variables
163 </Link>{" "}
164 available to Deployments: each row for Production or Preview, or for all environments.
165 </p>
166 <Button type="submit" disabled={busy}>
167 Save
168 </Button>
169 </Form>
170
171 <section className="mt-10 rounded-xl border border-danger/30 p-5">
172 <h2 className="text-sm font-medium">Turn off deployments</h2>
173 <p className="mt-1 text-sm text-muted">
174 Takes production and every preview down now, and stops building. Nothing of this project's keeps
175 running or costing anything. The workspace's plan stays on; turn it off under Billing.
176 </p>
177 <Form method="post" className="mt-3">
178 <Button variant="quiet" type="submit" name="intent" value="disable" disabled={busy}>
179 Turn off deployments
180 </Button>
181 </Form>
182 </section>
183 </>
184 )}
185 </div>
186 );
187}