Files
rm-circle-team-router/public/app.js
T
martbost 29ec8e82a1 Mini App: tapping your OWN invite link previews the prospect view
Linked members were always routed to their dashboard, so a sponsor could
never see their angle page through their own t.me link. Now ref == own id ->
/join/<id>?src=miniapp-preview (+angle); a teammate's link still opens the
dashboard. links command notes the preview trick.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-23 07:16:00 -05:00

58 lines
2.7 KiB
JavaScript

// Telegram Mini App entry: verify initData server-side, then land the linked
// member on THEIR dashboard with a minted session — zero login. Unlinked
// users get the one-time wallet-link instructions instead.
(function () {
'use strict';
var tg = window.Telegram && window.Telegram.WebApp;
function show(id) {
['st-loading', 'st-unlinked', 'st-error', 'st-notg'].forEach(function (x) {
var el = document.getElementById(x); if (el) el.style.display = x === id ? '' : 'none';
});
}
function on(id, fn) { var el = document.getElementById(id); if (el) el.addEventListener('click', fn); }
on('btn-open-site', function () {
var url = location.origin + '/my';
if (tg && tg.openLink) tg.openLink(url); else location.href = url;
});
on('btn-join', function () { location.href = '/join-now?src=miniapp'; });
on('btn-browse', function () { location.href = '/start'; });
on('btn-retry', function () { location.reload(); });
if (!tg || !tg.initData) { show('st-notg'); return; }
// Flag the webview session so tg-app.js activates on every page after this one.
try { sessionStorage.setItem('rmcTg', '1'); } catch (e) {}
tg.ready();
try { tg.expand(); } catch (e) {}
try { tg.setHeaderColor('#071421'); tg.setBackgroundColor('#071421'); } catch (e) {}
// Invite attribution: t.me/<bot>/<short>?startapp=<sponsorId>[_<angle>]
// routes a prospect to the sponsor's personalized squeeze page. The webview
// session keeps ?src=miniapp so the join records where it came from.
var ref = null, angle = null;
try {
var sp = (tg.initDataUnsafe && tg.initDataUnsafe.start_param) || '';
var m = /^(\d{1,15})(?:[_-]([a-z]{2,12}))?$/.exec(sp);
if (m) { ref = m[1]; angle = m[2] || null; }
} catch (e) {}
fetch('/api/public/tg-webapp-auth', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ initData: tg.initData })
}).then(function (r) { return r.json(); }).then(function (d) {
if (d && d.ok && d.linked) {
// A member tapping their OWN invite link wants to preview what their
// prospects see — show the squeeze page (tagged so stats stay clean).
// Tapping a teammate's link still lands on their own dashboard.
if (ref && String(d.id) === ref) { location.replace('/join/' + ref + '?src=miniapp-preview' + (angle ? '&v=' + angle : '')); return; }
location.replace('/my/' + d.id); return;
}
if (d && d.ok) {
if (ref) { location.replace('/join/' + ref + '?src=miniapp' + (angle ? '&v=' + angle : '')); return; }
show('st-unlinked'); return;
}
var el = document.getElementById('err-detail');
if (el && d && d.error) el.textContent = d.error;
show('st-error');
}).catch(function () { show('st-error'); });
})();