import { useEffect, useRef, useState } from "react"; import type { FormEvent } from "react"; import { API_V1, CREDENTIALS } from "../apiContract"; import { Brand } from "../shell/Brand"; import "./signup.css"; type Invite = { company: string; phone: string; expiresAt: string }; type Phase = "loading" | "ready" | "expired" | "used" | "success" | "error"; type JoinError = { error?: { code?: string; message?: string } }; // Keep consumed links recognizable in this tab without persisting invitation tokens. const consumed = new Set(); const EXPIRED = "This link has expired. Text Loopy again."; export default function JoinPage({ token }: { token: string }) { const [phase, setPhase] = useState("loading"); const [invite, setInvite] = useState(null); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [message, setMessage] = useState(""); const [weak, setWeak] = useState(false); const [busy, setBusy] = useState(false); const [attempt, setAttempt] = useState(0); const submitting = useRef(false); const endpoint = `${API_V1}/join/${encodeURIComponent(token)}`; useEffect(() => { if (consumed.has(token)) { setPhase("used"); return; } const controller = new AbortController(); setPhase("loading"); setMessage(""); void (async () => { try { const response = await fetch(endpoint, { credentials: CREDENTIALS, signal: controller.signal }); const body = await response.json() as Invite & JoinError; if (controller.signal.aborted) return; if (response.status === 404) { setPhase("expired"); return; } if (!response.ok) { setMessage(body.error?.message || "This link could not be loaded. Try again."); setPhase("error"); return; } if (typeof body.company !== "string" || typeof body.phone !== "string" || typeof body.expiresAt !== "string") throw new Error("Unreadable invitation"); setInvite({ company: body.company, phone: body.phone, expiresAt: body.expiresAt }); setPhase("ready"); } catch { if (controller.signal.aborted) return; setMessage("This link could not be loaded. Try again."); setPhase("error"); } })(); return () => controller.abort(); }, [token, endpoint, attempt]); async function submit(event: FormEvent) { event.preventDefault(); if (submitting.current || phase !== "ready") return; submitting.current = true; setBusy(true); setMessage(""); setWeak(false); try { const response = await fetch(endpoint, { method: "POST", credentials: CREDENTIALS, headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: name.trim(), email: email.trim(), password }), }); const body = await response.json() as JoinError & { ok?: boolean }; if (response.status === 404) { setPhase("expired"); return; } if (body.error?.code === "join_used") { setPhase("used"); return; } if (!response.ok) { setWeak(body.error?.code === "weak_password"); setMessage(body.error?.message || "Your account could not be created. Try again."); return; } if (body.ok !== true) throw new Error("Unreadable signup result"); consumed.add(token); setPassword(""); setPhase("success"); } catch { setMessage("The server could not be reached. Try again."); } finally { submitting.current = false; setBusy(false); } } return (

Join{invite ? ` ${invite.company}` : ""}

{phase === "loading" ? (
) : phase === "ready" && invite ? ( <>

{invite.phone}

setName(event.target.value)} disabled={busy} /> setEmail(event.target.value)} disabled={busy} /> setPassword(event.target.value)} disabled={busy} aria-invalid={weak} aria-describedby="join-message" />
) : phase === "expired" ? (

{EXPIRED}

) : phase === "used" ? ( <>

This link has already been used. Sign in to continue.

Sign in ) : phase === "success" ? ( <>

You are in. Sign in to finish setup.

Sign in ) : ( <>

{message}

)}
); }