import { useEffect, useState } from 'react'; import { ApiError, loadInvite, register } from '../lib/api'; import type { InviteInfo } from '../lib/api'; interface Props { token: string; } const fmtDate = (ms: number | null) => ms ? new Date(ms).toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' }) : ''; const STATUS_COPY: Record = { accepted: { title: 'This invite has already been used', body: 'Each invite link works once. Sign in if the account is yours, or ask an admin for a fresh link.', }, expired: { title: 'This invite has expired', body: 'Invite links are time-limited. Ask an admin to send you a new one.', }, revoked: { title: 'This invite was turned off', body: 'An admin revoked this link. Ask them for a new invite if you still need access.', }, }; /** Public signup screen reached from an admin's single-use invite link. */ export default function JoinPage({ token }: Props) { const [invite, setInvite] = useState(null); const [missing, setMissing] = useState(false); const [loadError, setLoadError] = useState(null); const [email, setEmail] = useState(''); const [name, setName] = useState(''); const [password, setPassword] = useState(''); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); useEffect(() => { let alive = true; loadInvite(token) .then((next) => { if (!alive) return; setInvite(next); setEmail(next.email); }) .catch((err) => { if (!alive) return; if (err instanceof ApiError && err.status === 404) setMissing(true); else setLoadError(err instanceof Error ? err.message : 'Could not check this invite'); }); return () => { alive = false; }; }, [token]); const submit = async (e: React.FormEvent) => { e.preventDefault(); if (!invite || invite.status !== 'pending' || busy) return; if (password.length < 8) { setError('Use at least 8 characters for the password'); return; } setBusy(true); setError(null); try { await register({ email: email.trim(), password, name: name.trim(), inviteToken: token, }); window.location.assign('/'); } catch (err) { setError(err instanceof Error ? err.message : 'Could not create the account'); setBusy(false); } }; const state = missing ? 'missing' : invite?.status; const lockedEmail = Boolean(invite?.email); const copy = state === 'missing' ? { title: 'This invite link is not valid', body: 'Check the link you were sent, or ask an admin for a new invite.', } : state && STATUS_COPY[state] ? STATUS_COPY[state] : null; return (
LUMINARY
{!invite && !missing && !loadError &&

Checking your invite…

} {loadError && ( <> Invite

Can’t reach Luminary

{loadError}

Go to Luminary )} {copy && ( <> Invite

{copy.title}

{copy.body}

Go to sign in )} {invite?.status === 'pending' && ( <> You’re invited

Join the studio

{lockedEmail ? `Create your account for ${invite.email}. This link works once.` : 'Create your account to start editing, proofing and sharing photos. This link works once.'}

{error &&
{error}
}

You’ll join as a {invite.role === 'admin' ? 'administrator' : 'member'} {invite.expiresAt ? ` · link expires ${fmtDate(invite.expiresAt)}` : ''}

Already have an account? Sign in

)}
); }