e66a38dd74
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
91 lines
9.5 KiB
JavaScript
91 lines
9.5 KiB
JavaScript
// LinkSpin Control Room shell (2026-09-29): the frame every workspace screen shares.
|
|
// Rail + topbar, session check, in-shell sign-in (email code, same endpoints as the dashboard),
|
|
// fetch helper, toast, in-page dialogs (never native prompt/confirm). Pages: put content in
|
|
// <div id="page">, then CR.mount({active,title,sub,actions}) and CR.requireSession(cb).
|
|
window.CR = (function () {
|
|
const $ = id => document.getElementById(id);
|
|
const esc = s => String(s == null ? '' : s).replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
|
|
const fmt = n => Number(n || 0).toLocaleString();
|
|
const NAV = [
|
|
['links', 'Links', '/links', '<path d="M10 14a4 4 0 0 0 5.7 0l3-3a4 4 0 0 0-5.7-5.7l-1 1"/><path d="M14 10a4 4 0 0 0-5.7 0l-3 3a4 4 0 0 0 5.7 5.7l1-1"/>'],
|
|
['pages', 'Pages', '/pages', '<rect x="4" y="4" width="16" height="16" rx="2"/><path d="M8 9h8M8 13h6"/>'],
|
|
['zones', 'Zones', '/zones', '<rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 10h18"/>'],
|
|
['reports', 'Reports', '/reports', '<path d="M4 19V9M10 19V5M16 19v-8M22 19H2"/>'],
|
|
['team', 'Team', '/team', '<circle cx="9" cy="8" r="3"/><circle cx="17" cy="9" r="2.5"/><path d="M3 19c0-3 3-5 6-5s6 2 6 5M15 15c3 0 6 1.5 6 4"/>'],
|
|
['wallet', 'Wallet', '/wallet', '<rect x="3" y="6" width="18" height="12" rx="2"/><path d="M16 12h3"/>'],
|
|
];
|
|
// screens not built yet fall back to the classic dashboard pane so nothing is a dead end
|
|
const FALLBACK = { pages: '/my#promo', zones: '/my#campaigns', team: '/my#line', wallet: '/my#wallet' };
|
|
const BUILT = new Set(['links', 'pages', 'zones', 'reports', 'team', 'wallet']);
|
|
let ME = null, toastT;
|
|
function toast(msg, bad) { let t = $('crToast'); if (!t) { t = document.createElement('div'); t.id = 'crToast'; t.className = 'toast'; t.setAttribute('role', 'status'); document.body.appendChild(t); } t.textContent = msg; t.style.background = bad ? '#FF7A7A' : ''; t.classList.add('on'); clearTimeout(toastT); toastT = setTimeout(() => t.classList.remove('on'), 2000); }
|
|
async function api(path, body) { const r = await (await fetch(path, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body || {}) })).json(); if (r.error) throw new Error(r.error); return r; }
|
|
async function get(path) { const r = await (await fetch(path, { headers: { Accept: 'application/json' } })).json(); if (r.error) throw new Error(r.error); return r; }
|
|
function copy(text) { try { navigator.clipboard.writeText(text).then(() => toast('Copied'), () => toast('Select and copy: ' + text)); } catch (e) { toast('Select and copy: ' + text); } }
|
|
function dialog(opts) {
|
|
return new Promise(resolve => {
|
|
let host = $('crModalHost'); if (!host) { host = document.createElement('div'); host.id = 'crModalHost'; document.body.appendChild(host); }
|
|
host.innerHTML = '';
|
|
const back = document.createElement('div'); back.className = 'modal-back';
|
|
back.innerHTML = '<div class="modal" role="dialog" aria-modal="true"><h2>' + esc(opts.title) + '</h2>' + (opts.text ? '<div class="ink2">' + esc(opts.text) + '</div>' : '') + (opts.html || '')
|
|
+ (opts.fields || []).map(f => '<label class="lbl">' + esc(f.label) + (f.type === 'textarea' ? '<textarea class="field' + (f.mono ? ' mono' : '') + '" id="dlg-' + f.k + '" rows="' + (f.rows || 3) + '" placeholder="' + esc(f.placeholder || '') + '">' + esc(f.value || '') + '</textarea>' : '<input class="field' + (f.mono ? ' mono' : '') + '" id="dlg-' + f.k + '" type="' + (f.type || 'text') + '" value="' + esc(f.value || '') + '" placeholder="' + esc(f.placeholder || '') + '"' + (f.max ? ' maxlength="' + f.max + '"' : '') + '>') + '</label>').join('')
|
|
+ '<div class="acts"><button class="btn" type="button" id="dlgNo">' + esc(opts.no || 'Cancel') + '</button><button class="btn pri' + (opts.danger ? ' danger' : '') + '" type="button" id="dlgOk">' + esc(opts.ok || 'OK') + '</button></div></div>';
|
|
host.appendChild(back);
|
|
const done = v => { host.innerHTML = ''; resolve(v); };
|
|
back.querySelector('#dlgNo').addEventListener('click', () => done(null));
|
|
back.addEventListener('click', e => { if (e.target === back) done(null); });
|
|
back.querySelector('#dlgOk').addEventListener('click', () => { const out = {}; (opts.fields || []).forEach(f => { out[f.k] = back.querySelector('#dlg-' + f.k).value; }); done(opts.fields ? out : true); });
|
|
const first = back.querySelector('input,textarea'); if (first) { first.focus(); if (first.select) first.select(); }
|
|
back.addEventListener('keydown', e => { if (e.key === 'Enter' && e.target.tagName !== 'TEXTAREA') back.querySelector('#dlgOk').click(); if (e.key === 'Escape') done(null); });
|
|
});
|
|
}
|
|
function mount(o) {
|
|
const page = $('page'); if (!page) return;
|
|
const app = document.createElement('div'); app.className = 'app';
|
|
app.innerHTML = '<nav class="rail" aria-label="LinkSpin"><a class="mark" href="/" aria-label="LinkSpin home" title="LinkSpin"></a>'
|
|
+ NAV.map(([k, label, href, svg]) => '<a href="' + (BUILT.has(k) ? href : FALLBACK[k]) + '"' + (k === o.active ? ' class="on" aria-current="page"' : '') + ' title="' + label + '" aria-label="' + label + '"><svg viewBox="0 0 24 24">' + svg + '</svg></a>').join('')
|
|
+ '<span class="sp"></span><a href="#" id="crUserBtn" title="Account" aria-label="Account"><svg viewBox="0 0 24 24"><circle cx="12" cy="8" r="4"/><path d="M4 21c0-4 4-6 8-6s8 2 8 6"/></svg></a></nav>'
|
|
+ '<div class="main"><div class="topbar"><h1>' + esc(o.title || '') + '</h1><span class="muted mono" id="crSub">' + esc(o.sub || '') + '</span><span class="grow"></span><span id="crActions">' + (o.actions || '') + '</span></div><div id="crBody"></div></div>';
|
|
page.parentNode.insertBefore(app, page); app.querySelector('#crBody').appendChild(page);
|
|
$('crUserBtn').addEventListener('click', async e => { e.preventDefault(); if (!ME || !ME.signedIn) return signInFlow(() => location.reload()); const v = await dialog({ title: ME.username ? '@' + ME.username : (ME.email || 'Account'), text: 'Signed in on LinkSpin. Your classic dashboard is still at /my.', ok: 'Sign out', no: 'Close', danger: true }); if (v) { try { await api('/api/auth/logout'); } catch (er) {} location.reload(); } });
|
|
}
|
|
const sub = t => { const s = $('crSub'); if (s) s.textContent = t; };
|
|
async function me(force) { if (ME && !force) return ME; try { ME = await (await fetch('/api/me', { headers: { Accept: 'application/json' } })).json(); } catch (e) { ME = { signedIn: false }; } return ME; }
|
|
// sign-in inside the shell: email -> six-digit code, on the same endpoints and guards as the dashboard.
|
|
const FTS = Date.now();
|
|
async function signInFlow(onReady) {
|
|
let host = $('crModalHost'); if (!host) { host = document.createElement('div'); host.id = 'crModalHost'; document.body.appendChild(host); }
|
|
host.innerHTML = '<div class="modal-back"><div class="modal" role="dialog" aria-modal="true"><h2>Sign in</h2><div class="ink2">Email first. We send a six-digit code; no password to remember. New here? The same box creates your account.</div>'
|
|
+ '<label class="lbl">Email<input class="field" id="siEmail" type="email" autocomplete="email" placeholder="you@example.com"></label>'
|
|
+ '<div id="siCodeRow" hidden><label class="lbl">Code from the email<input class="field mono" id="siCode" inputmode="numeric" maxlength="6" placeholder="123456"></label></div>'
|
|
+ '<div class="muted" id="siMsg"></div><div class="acts"><a class="btn" href="/my">Classic dashboard</a><button class="btn pri" type="button" id="siGo">Send code</button></div></div></div>';
|
|
const email = $('siEmail'), code = $('siCode'), go = $('siGo'), msg = $('siMsg'); let stage = 'email';
|
|
email.focus();
|
|
const say = (t, bad) => { msg.textContent = t; msg.className = bad ? 'bad' : 'muted'; };
|
|
async function step() {
|
|
try {
|
|
if (stage === 'email') {
|
|
const e = email.value.trim(); if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(e)) return say('That email address does not look right.', true);
|
|
go.disabled = true; say('Sending…');
|
|
// the sign-up guard refuses a form younger than a few seconds (bot defence); wait it out instead of bouncing the person
|
|
const wait = FTS + 4500 - Date.now(); if (wait > 0) await new Promise(r => setTimeout(r, wait));
|
|
const r = await api('/api/auth/email/start', { email: e, fts: FTS, hp_field_x9: '', pick: null });
|
|
go.disabled = false;
|
|
if (r.challenge) { say('One more check is needed for this address. Use the classic dashboard sign-in this once.', true); return; }
|
|
$('siCodeRow').hidden = false; code.focus(); stage = 'code'; go.textContent = 'Sign in';
|
|
if (r.devCode) code.value = r.devCode;
|
|
say(r.sent ? 'Code sent. Check your inbox, and spam the first time.' : 'Code filled in (local dev).');
|
|
} else {
|
|
go.disabled = true; say('Checking…');
|
|
await api('/api/auth/email/verify', { email: email.value.trim(), code: code.value.trim() });
|
|
host.innerHTML = ''; ME = null; await me(true); toast('You are in'); onReady && onReady(ME);
|
|
}
|
|
} catch (e) { go.disabled = false; say(e.message, true); }
|
|
}
|
|
go.addEventListener('click', step);
|
|
host.querySelector('.modal').addEventListener('keydown', e => { if (e.key === 'Enter') step(); });
|
|
}
|
|
async function requireSession(onReady) { const m = await me(); if (m && m.signedIn) return onReady(m); signInFlow(onReady); }
|
|
return { esc, fmt, api, get, copy, dialog, toast, mount, sub, me, requireSession, signInFlow };
|
|
})();
|