iterate.com / ui

git clone https://git.iterate.com/repos/ui

ui / src / components / connect-mind.tsxmain at d68d7e1

// connect-mind.tsx — POINT AN APP AT A MIND: one field, the mind's address. Every app
// serves it at `/connect` (iterate/app-server `CONNECT_PAGE_PATH`). The form posts to the app's own
// `/.auth/connect`, a same-origin POST, which is the only way a browser is bound to a mind. The
// gate finds the mind's sign-in provider and answers where to sign in, or why it cannot.
import { useEffect, useId, useState } from "react";
import { z } from "zod";
import { IterateLogo } from "#/components/iterate-logo.tsx";
import { Button } from "#/components/ui/button.tsx";
import { Field, FieldDescription, FieldError, FieldLabel } from "#/components/ui/field.tsx";
import { Input } from "#/components/ui/input.tsx";

const GateAnswer = z.union([z.object({ location: z.string() }), z.object({ error: z.string() })]);

export function ConnectMind({
  app,
  mind = "",
  next,
}: {
  /** the app's name, above the form */
  app: string;
  /** the address to fill in: the gate sends a browser here for a mind it is not connected to */
  mind?: string;
  /** where the browser lands once signed in; by default the mind's own page */
  next?: string;
}) {
  const fieldId = useId();
  const [address, setAddress] = useState(mind);
  const [error, setError] = useState<string | null>(null);
  const [connecting, setConnecting] = useState(false);

  // back from the issuer with the browser's back button, a page out of the back/forward cache is
  // still connecting
  useEffect(() => {
    const again = () => setConnecting(false);
    window.addEventListener("pageshow", again);
    return () => window.removeEventListener("pageshow", again);
  }, []);

  const connect = async () => {
    setConnecting(true);
    setError(null);
    const form = new FormData();
    form.set("mind", address);
    if (next) form.set("next", next);
    try {
      // react-doctor-disable-next-line react-doctor/no-fetch-response-used-without-status-check -- checked on the next line: a 400 is the gate's refusal, and its body is the reason
      const response = await fetch("/.auth/connect", { method: "POST", body: form });
      if (!response.ok && response.status !== 400) throw new Error(`gate ${response.status}`);
      const answer = GateAnswer.safeParse(await response.json());
      if (!answer.success) throw new Error("not the gate's answer");
      if ("location" in answer.data) {
        window.location.assign(answer.data.location);
        return;
      }
      setError(answer.data.error);
    } catch {
      setError("This app could not connect just now. Try again.");
    }
    setConnecting(false);
  };

  return (
    <main className="flex min-h-svh flex-col items-center justify-center p-6">
      <form
        className="flex w-full max-w-sm flex-col gap-6"
        onSubmit={(event) => {
          event.preventDefault();
          void connect();
        }}
      >
        <div className="flex items-center gap-3">
          <span className="flex size-10 items-center justify-center rounded-md bg-black">
            <IterateLogo alt="" className="size-7" />
          </span>
          <div className="flex flex-col">
            <h1 className="text-lg font-semibold">Connect a mind</h1>
            <span className="text-sm text-muted-foreground">{app}</span>
          </div>
        </div>
        <Field data-invalid={error ? true : undefined}>
          <FieldLabel htmlFor={fieldId}>Mind address</FieldLabel>
          <Input
            id={fieldId}
            name="mind"
            className="h-11 sm:h-9"
            placeholder="iterate.example.com"
            value={address}
            onChange={(event) => setAddress(event.target.value)}
            aria-invalid={error ? true : undefined}
            autoCapitalize="none"
            autoCorrect="off"
            autoComplete="url"
            spellCheck={false}
            inputMode="url"
            enterKeyHint="go"
            required
          />
          <FieldDescription>
            Type the mind&apos;s address, like iterate.example.com. The domain alone works too.
          </FieldDescription>
          {error ? <FieldError>{error}</FieldError> : null}
        </Field>
        <Button type="submit" className="h-11 sm:h-9" disabled={connecting || !address.trim()}>
          {connecting ? "Connecting…" : "Connect"}
        </Button>
      </form>
    </main>
  );
}