import React from 'react'; import { api } from './api.js'; export default function UsernameGate({ onPick, isMobile }) { const [value, setValue] = React.useState(''); const [error, setError] = React.useState(''); const [busy, setBusy] = React.useState(false); const [info, setInfo] = React.useState(null); // { exists, best } const validate = (v) => /^[A-Za-z0-9_\-]{2,16}$/.test(v); const submit = async (e) => { e?.preventDefault(); const name = value.trim(); if (!validate(name)) { setError('2–16 chars. Letters, numbers, _ or - only.'); return; } if (!isMobile) { try { document.documentElement.requestFullscreen?.(); } catch {} } setBusy(true); setError(''); try { const r = await fetch(api('/api/user/check'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: name }), }).then((r) => r.json()); if (!r.ok) throw new Error(r.error || 'check failed'); setInfo({ exists: r.exists, best: r.best }); onPick(name); } catch (err) { setError(err.message || 'Could not verify username. Try again.'); } finally { setBusy(false); } }; return (
▸ ARCADE / EGG-CATCHER

PICK YOUR NAME

Your best score will be saved to the global leaderboard. New name? You start at zero. Returning? We’ll load your best.

setValue(e.target.value)} placeholder="e.g. pixel_pirate" maxLength={16} autoFocus autoComplete="off" spellCheck="false" />
2–16 characters. Letters, numbers, _ or - only.
{error &&
✕ {error}
} {info?.exists && !error && (
★ Welcome back — current best {info.best}
)}
); }