import { AgentAvatar, Avatar, Button } from "@refs/design-system"; import { data, Form, redirect, useNavigation } from "react-router"; import type { Route } from "./+types/oauth-authorize"; import { refsErrorCode } from "@refs/backend/contract"; import { AuthPage } from "../components/shell"; import { requestContext } from "../context"; import { formText } from "../lib/session"; export const meta: Route.MetaFunction = () => [{ title: "Connect an agent ยท refs" }]; export function headers({ loaderHeaders, actionHeaders }: Route.HeadersArgs) { return actionHeaders.has("set-cookie") ? actionHeaders : loaderHeaders; } export async function loader({ request, context }: Route.LoaderArgs) { const url = new URL(request.url); let step; try { step = await context.get(requestContext).backend.agentConsent(request).begin(); } catch (error) { if (refsErrorCode(error) === "sign_in_required") throw redirect(`/login?next=${encodeURIComponent(url.pathname + url.search)}`); throw error; } if (!step.ok) return data({ view: null, error: step.problem.message }, { status: 400 }); if ("redirectTo" in step.value) throw redirect(step.value.redirectTo, { headers: step.value.headers }); return data({ view: step.value.view, error: null }, { headers: step.value.headers }); } export async function action({ request, context }: Route.ActionArgs) { const form = await request.formData(); const consent = context.get(requestContext).backend.agentConsent(request); const handle = formText(form, "handle"); const step = formText(form, "intent") === "allow" ? await consent.approve(handle) : await consent.deny(handle); if (!step.ok) return { error: step.problem.message }; throw redirect(step.value.redirectTo, { headers: step.value.headers }); } export default function Authorize({ loaderData, actionData }: Route.ComponentProps) { const navigation = useNavigation(); const intent = navigation.state === "submitting" ? navigation.formData?.get("intent") : null; const view = loaderData.view; const error = actionData?.error ?? loaderData.error ?? undefined; if (!view) return {null}; return ( {view.client} wants to act as{" "} {view.person.name} on refs. It will be able to Come online on repositories you can push to Clone and read those repositories Take a branch and push to it You'll be sent back to {view.redirectHost} {view.loopback ? ", an app on this computer" : ""}. You can disconnect it any time under Access tokens. Allow Deny ); }
{view.client} wants to act as{" "} {view.person.name} on refs.
You'll be sent back to {view.redirectHost} {view.loopback ? ", an app on this computer" : ""}. You can disconnect it any time under Access tokens.