Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

50 linesCodeBlameRaw
1import { data } from "react-router";
2
3import type { Route } from "./+types/guardrails";
4import { GuardrailsForm } from "../../components/guardrails";
5import { usePending } from "../../components/ui";
6import { SANDBOX_RUN_KINDS, settingsFromForm } from "../../lib/guardrails";
7import { page } from "../../lib/meta";
8import { guardrails } from "../../lib/services.server";
9import { assertSameOrigin, getViewer, requireUser, roleIn, unwrap } from "../../lib/session.server";
10
11export function meta({ params, ...args }: Route.MetaArgs) {
12 return page(args, { title: `Guardrails · ${params.owner} · g1t` });
13}
14
15export async function loader({ params, context }: Route.LoaderArgs) {
16 const viewer = getViewer(context);
17 const role = roleIn(viewer, params.owner);
18 if (!role) throw data(null, { status: 404 });
19 const view = await guardrails.getGuardrails(viewer, params.owner);
20 return { view: unwrap(view), owner: role === "owner" };
21}
22
23export async function action({ request, params, context }: Route.ActionArgs) {
24 assertSameOrigin(request);
25 const user = requireUser(context, request);
26 const form = await request.formData();
27 const current = await guardrails.getGuardrails(user, params.owner);
28 if (!current.ok) return { saved: false, error: current.error.message };
29 const settings = settingsFromForm(form, {
30 registries: current.value.registries.map((registry) => registry.id),
31 kinds: SANDBOX_RUN_KINDS,
32 });
33 const saved = await guardrails.updateGuardrails(user, params.owner, null, settings);
34 return saved.ok ? { saved: true, error: null } : { saved: false, error: saved.error.message };
35}
36
37export default function WorkspaceGuardrails({ loaderData, actionData }: Route.ComponentProps) {
38 // Until the saved settings are back on the page, not just until the post is answered.
39 const saving = usePending();
40 return (
41 <GuardrailsForm
42 view={loaderData.view}
43 level="workspace"
44 editable={loaderData.owner}
45 saving={saving}
46 saved={actionData?.saved ?? false}
47 error={actionData?.error}
48 />
49 );
50}