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

186 lines8,591 bytesCodeBlame
1import { Form, Link } 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 { ErrorText, Field, Input, SubmitButton } 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 base = `/${params.owner}/${params.repo}`;
100 return (
101 <div className="max-w-4xl">
102 <RepoSettingsHeading base={base} />
103 <div className="min-h-6">
104 {actionData && "notice" in actionData && <p className="text-sm text-accent">{actionData.notice}</p>}
105 <ErrorText>{actionData && "error" in actionData ? actionData.error : null}</ErrorText>
106 </div>
107
108 <Detected kind={settings.detected ?? null} />
109
110 {!settings.enabled ? (
111 <section className="rounded-xl border border-line bg-surface p-5">
112 <h2 className="font-medium">Deployments are off</h2>
113 <p className="mt-1 text-sm text-muted">
114 Deployments are opt-in for each project, and nothing builds or runs until you turn them on. Then g1t builds
115 production at{" "}
116 <span className="font-mono text-fg">{settings.productionUrl.replace("https://", "")}</span> and a preview for
117 every pull request. The workspace needs the g1t plan, under Billing.
118 </p>
119 {notDeploying ? (
120 <p className="mt-4 text-sm text-muted">
121 This project is set as one that doesn't deploy, a library or a tool. To deploy it, choose Deploys or Detect
122 automatically under{" "}
123 <Link to={`${base}/settings#deploys`} className="text-fg underline underline-offset-4">
124 General
125 </Link>{" "}
126 first.
127 </p>
128 ) : (
129 <Form method="post" className="mt-4">
130 <SubmitButton variant="accent" name="intent" value="enable" pending="Turning on…">
131 Turn on deployments
132 </SubmitButton>
133 </Form>
134 )}
135 </section>
136 ) : (
137 <>
138 <Form method="post" className="space-y-5">
139 <div className="grid gap-3 md:grid-cols-2">
140 <Check name="production" on={settings.production} title="Production">
141 Deploy the default branch on every push.
142 </Check>
143 <Check name="previews" on={settings.previews} title="Previews">
144 A preview for every branch with an open pull request, linked on it.
145 </Check>
146 </div>
147 <div className="grid gap-4 md:grid-cols-3">
148 <Field label="Build command" hint="Instead of the project's own build script.">
149 <Input name="buildCommand" defaultValue={settings.buildCommand ?? ""} placeholder="npm run build" />
150 </Field>
151 <Field label="Output directory" hint="For a static site; found by itself when empty.">
152 <Input name="outputDir" defaultValue={settings.outputDir ?? ""} placeholder="dist" />
153 </Field>
154 <Field label="Idle days" hint="A preview no one visits for this long comes down.">
155 <Input name="idleDays" type="number" min={1} max={90} defaultValue={settings.idleDays} />
156 </Field>
157 </div>
158 <p className="text-sm text-muted">
159 Builds and running apps read the{" "}
160 <Link to={`${base}/settings/secrets`} className="text-fg hover:underline">
161 secrets and variables
162 </Link>{" "}
163 available to Deployments: each row for Production or Preview, or for all environments.
164 </p>
165 <SubmitButton name="intent" value="save" pending="Saving…">
166 Save
167 </SubmitButton>
168 </Form>
169
170 <section className="mt-10 rounded-xl border border-danger/30 p-5">
171 <h2 className="text-sm font-medium">Turn off deployments</h2>
172 <p className="mt-1 text-sm text-muted">
173 Takes production and every preview down now, and stops building. Nothing of this project's keeps
174 running or costing anything. The workspace's plan stays on; turn it off under Billing.
175 </p>
176 <Form method="post" className="mt-3">
177 <SubmitButton variant="quiet" name="intent" value="disable" pending="Turning off…">
178 Turn off deployments
179 </SubmitButton>
180 </Form>
181 </section>
182 </>
183 )}
184 </div>
185 );
186}