/* ═══════════════════════════════════════════════════════════════ WELCOME: /account/welcome?token=…, an invite's link (076) Who the invite is for, then a password to set, or Google under the invited email. Either takes up the invite: the link stops working and the account is signed in (routes/account.js, routes/auth.js). ═══════════════════════════════════════════════════════════════ */ import { useEffect, useState } from 'react' import { Link, useNavigate, useSearchParams } from 'react-router-dom' import PageShell from '../components/PageShell.tsx' import { useAuth } from '../lib/auth.tsx' import { ApiError, get, post } from '../lib/api.ts' import { BRAND } from '../lib/color.ts' import { GoogleButton, fieldClass, primaryButton } from './SignIn.tsx' const MIN_PASSWORD = 12 type Invite = { email: string; name: string | null; google: boolean } export default function AccountWelcome() { const [searchParams] = useSearchParams() const token = searchParams.get('token') ?? '' const navigate = useNavigate() const { signedIn } = useAuth() const [invite, setInvite] = useState(null) const [problem, setProblem] = useState(null) const [password, setPassword] = useState('') const [again, setAgain] = useState('') const [error, setError] = useState(null) const [busy, setBusy] = useState(false) useEffect(() => { if (!token) { setProblem('This page needs the link from your invite email.') return } get(`/account/invite?token=${encodeURIComponent(token)}`, { ttl: 0 }) .then((data) => setInvite(data.invite)) .catch((err) => setProblem(err instanceof ApiError ? err.message : "Couldn't reach the server. Try again in a moment.")) }, [token]) async function submit(event: React.FormEvent) { event.preventDefault() if (busy) return if (password.length < MIN_PASSWORD) return setError(`Use at least ${MIN_PASSWORD} characters.`) if (password !== again) return setError("The two passwords don't match.") setBusy(true) setError(null) try { const data = await post('/account/accept', { token, password }) signedIn(data.user) navigate('/account', { replace: true }) } catch (err) { setError(err instanceof ApiError ? err.message : "Couldn't reach the server. Try again in a moment.") setBusy(false) } } return ( {problem ? (

{problem}

Already set up? Sign in. Otherwise ask whoever invited you for a new link.

) : !invite ? (
) : (

Your account is {invite.email}.

{invite.google && ( <>

Sign in to Google as {invite.email}.

or set a password
)} setPassword(e.target.value)} aria-describedby="welcome-password-help" className={`mt-2 ${fieldClass}`} />

At least {MIN_PASSWORD} characters.

setAgain(e.target.value)} className={`mt-2 ${fieldClass}`} /> {error && (

{error}

)}
)}
), }, ]} /> ) }