Guest invite: hide the form once the 30-day invite is used; guest errors render in the guest card

When the member has no invite left (one per rolling 30 days), the invite
form is REMOVED and a dated line takes its place: 'You have already
invited a guest this month. Your next invite is available <date>' — the
date computed by the portal from the last invite (next_eligible_at).

All guest-invite feedback (errors, refusals, success) now renders in a
dedicated #guest-flash inside the guest access card — never the #flash
element in the API-keys card, which is a different feature (the bug the
operator reported: over-limit refusals surfaced in the API key area).
Hiding the form is UX only; the portal refuses direct over-limit calls
server-side regardless.
This commit is contained in:
inference-bot committed 2026-11-08 20:53:51 +00:00
1 parent e5162afa05
commit 1e6e9e481c
1 file changed
+35 -12
+35 -12
View File
@@ -848,6 +848,7 @@ PAGE_HTML = """<!DOCTYPE html>
<div class="card" id="guests-card" style="display:none"> <div class="card" id="guests-card" style="display:none">
<h2>Guest access</h2> <h2>Guest access</h2>
<p class="guest-hint" id="guest-hint">Give a friend $2 of AI credits for the co-op's chat and API — no membership needed. They get 60 days to try it. One guest per member every 30 days.</p> <p class="guest-hint" id="guest-hint">Give a friend $2 of AI credits for the co-op's chat and API — no membership needed. They get 60 days to try it. One guest per member every 30 days.</p>
<div id="guest-invite-wrap">
<h3 class="guest-invite-title">Invite a guest</h3> <h3 class="guest-invite-title">Invite a guest</h3>
<form class="new-key" id="guest-invite-form" onsubmit="sendGuestInvite(event)"> <form class="new-key" id="guest-invite-form" onsubmit="sendGuestInvite(event)">
<label class="sr-only" for="guest-email">Their email</label> <label class="sr-only" for="guest-email">Their email</label>
@@ -856,6 +857,10 @@ PAGE_HTML = """<!DOCTYPE html>
<input id="guest-name" type="text" class="name-input" placeholder="Their name (optional)" maxlength="80" autocomplete="off" /> <input id="guest-name" type="text" class="name-input" placeholder="Their name (optional)" maxlength="80" autocomplete="off" />
<button class="primary" id="guest-btn" type="submit">Send invite</button> <button class="primary" id="guest-btn" type="submit">Send invite</button>
</form> </form>
</div>
<!-- Guest feedback renders HERE, inside the guest card — never in the
API-key flash above, which is a different feature. -->
<div class="flash" id="guest-flash"></div>
<div class="hint" id="guest-allowance"></div> <div class="hint" id="guest-allowance"></div>
<div class="guest-trail" id="guest-trail"></div> <div class="guest-trail" id="guest-trail"></div>
</div> </div>
@@ -878,6 +883,16 @@ function flash(msg, ok) {
setTimeout(() => { f.className = 'flash'; }, 6000); setTimeout(() => { f.className = 'flash'; }, 6000);
} }
// Guest-invite feedback stays INSIDE the guest access card. The #flash element
// lives in the API-keys card — a different feature — and a guest error landing
// there reads as an API-key problem (this replaces exactly that bug).
function guestFlash(msg, ok) {
const f = $('guest-flash');
f.textContent = msg;
f.className = 'flash show ' + (ok ? 'ok' : 'err');
setTimeout(() => { f.className = 'flash'; }, 6000);
}
function fmtMoney(n) { const v = Math.abs(Number(n)) < 0.005 ? 0 : (Number(n) || 0); return '$' + v.toFixed(2); } function fmtMoney(n) { const v = Math.abs(Number(n)) < 0.005 ? 0 : (Number(n) || 0); return '$' + v.toFixed(2); }
// --- Guest self-service cancel (reveal → confirm → ended state) ----------- // --- Guest self-service cancel (reveal → confirm → ended state) -----------
@@ -998,13 +1013,19 @@ function renderGuests(d) {
$('guest-btn').textContent = 'Send invite'; $('guest-btn').textContent = 'Send invite';
$('guest-hint').textContent = "Give a friend $2 of AI credits for the co-op's chat and API — no membership needed. They get 60 days to try it. One guest per member every 30 days."; $('guest-hint').textContent = "Give a friend $2 of AI credits for the co-op's chat and API — no membership needed. They get 60 days to try it. One guest per member every 30 days.";
if (d.eligible) { if (d.eligible) {
// An invite is available: show the form.
$('guest-invite-wrap').style.display = '';
$('guest-allowance').textContent = 'You can send one guest invitation now.'; $('guest-allowance').textContent = 'You can send one guest invitation now.';
} else if (d.next_eligible_at) {
$('guest-allowance').textContent =
'One guest every ' + (d.per_inviter_days || 30) + ' days — your next invitation opens ' + fmtDay(d.next_eligible_at) + '.';
} else { } else {
$('guest-allowance').textContent = // The 30-day invite is used: the form GOES — there is nothing left to
'One guest every ' + (d.per_inviter_days || 30) + ' days.'; // submit — and the next-open date (computed by the portal from the last
// invite) takes its place. This is UX only; the portal refuses direct
// over-limit calls server-side regardless of what the page shows.
$('guest-invite-wrap').style.display = 'none';
const next = d.next_eligible_at ? fmtDay(d.next_eligible_at) : '';
$('guest-allowance').textContent = next
? 'You have already invited a guest this month. Your next invite is available ' + next + '.'
: 'You have already invited a guest this month.';
} }
const trail = d.trail || []; const trail = d.trail || [];
if (!trail.length) { if (!trail.length) {
@@ -1027,16 +1048,18 @@ function renderGuests(d) {
// The standard guest-invite form: the guest's email (required) plus an // The standard guest-invite form: the guest's email (required) plus an
// OPTIONAL display name. The inviter is the logged-in member — automatic, no // OPTIONAL display name. The inviter is the logged-in member — automatic, no
// field; the portal applies the member-side protections (active member, one // field; the portal applies the member-side protections SERVER-SIDE (active
// per 30 days, recipient checks) and a valid invitation provisions // member, one per 30 days, recipient checks) and a valid invitation
// immediately. An empty name is never derived from the email. // provisions immediately — hiding this form is UX, not the gate. An empty
// name is never derived from the email. Every message from this flow renders
// in the guest card (guestFlash), never the API-key flash.
async function sendGuestInvite(ev) { async function sendGuestInvite(ev) {
if (ev) ev.preventDefault(); if (ev) ev.preventDefault();
const email = ($('guest-email').value || '').trim(); const email = ($('guest-email').value || '').trim();
const name = ($('guest-name').value || '').trim(); const name = ($('guest-name').value || '').trim();
const btn = $('guest-btn'); const btn = $('guest-btn');
const label = btn.textContent; const label = btn.textContent;
if (!email || email.indexOf('@') < 1) { flash('Enter their email address', false); return; } if (!email || email.indexOf('@') < 1) { guestFlash('Enter their email address', false); return; }
btn.disabled = true; btn.disabled = true;
btn.textContent = 'Sending…'; btn.textContent = 'Sending…';
try { try {
@@ -1046,15 +1069,15 @@ async function sendGuestInvite(ev) {
}); });
const d = await r.json().catch(() => ({})); const d = await r.json().catch(() => ({}));
if (!r.ok || d.error) { if (!r.ok || d.error) {
flash(d.detail || d.note || d.error || 'Could not send the invitation', false); guestFlash(d.detail || d.note || d.error || 'Could not send the invitation', false);
return; return;
} }
$('guest-email').value = ''; $('guest-email').value = '';
$('guest-name').value = ''; $('guest-name').value = '';
flash(d.note || 'Invitation sent — your friend can set up their account now.', true); guestFlash(d.note || 'Invitation sent — your friend can set up their account now.', true);
await loadGuests(); await loadGuests();
} catch (e) { } catch (e) {
flash('Could not send the invitation: ' + e.message, false); guestFlash('Could not send the invitation: ' + e.message, false);
} finally { } finally {
btn.disabled = false; btn.disabled = false;
btn.textContent = label; btn.textContent = label;