// My packets (the member dashboard): sign in (network account through the hub, or wallet), my packet (status, link check, sprout // meter, edit / remove), dormant old programs to confirm, the planting form with the AI writer, my saved IDs, Spotlights and earnings. (async function () { const $ = YPB.$, esc = YPB.esc; YPB.wire('other').catch(() => {}); const c = await YPB.getConfig().catch(() => ({})); const me = await YPB.getJSON('/api/me'); const qs = new URLSearchParams(location.search); if (!me.signedIn) { try { const ss = await YPB.getJSON('/api/sso/status'); if (ss && ss.check) { location.replace('/sso/start?mode=check&to=' + encodeURIComponent(location.pathname + location.search)); return; } } catch (e) {} $('signin').hidden = false; if (c.joinUrl) $('joinLink').href = c.joinUrl; $('walletBtn').onclick = async () => { try { YPB.status('Check your wallet for the sign-in message.', '', 'status'); await YPBWallet.signIn(); location.reload(); } catch (e) { YPB.status(String(e && e.message || e).slice(0, 300), 'err'); } }; return; } $('dash').hidden = false; $('logout').onclick = async () => { await YPB.post('/api/auth/logout'); location.reload(); }; $('logoutAll').onclick = async () => { $('logoutAll').disabled = true; await YPB.post('/api/auth/logout-everywhere'); location.reload(); }; const tabs = document.querySelectorAll('[data-tab]'); const show = t => { tabs.forEach(b => b.setAttribute('aria-pressed', String(b.dataset.tab === t))); document.querySelectorAll('[data-pane]').forEach(p => { p.hidden = p.dataset.pane !== t; }); }; tabs.forEach(b => { b.onclick = () => show(b.dataset.tab); }); show(['plant', 'saved', 'spot'].includes(qs.get('tab')) ? qs.get('tab') : 'packets'); (c.cats || []).forEach(k => { const o = document.createElement('option'); o.value = k.id; o.textContent = k.name; $('cat').appendChild(o); }); $('cat').value = '0'; const costPicked = () => document.querySelector('input[name=cost]:checked').value; document.querySelectorAll('input[name=cost]').forEach(r => { r.onchange = () => { $('costRow').hidden = costPicked() !== 'paid'; }; }); let d = null; const CHECK = { green: 'Link check green', yellow: 'Link check yellow: waiting for a person', red: 'Link check red: not shown' }; const stageTag = x => x.stage === 'review' ? 'In review' : x.stage === 'approved' ? 'Transplanted' : x.stage === 'rejected' ? 'Not added this time' : ''; function packetBox(x) { const pctv = Math.min(100, Math.round(100 * x.saves / x.need)); const notes = x.check && x.check.why && x.check.why.length ? '

Check notes: ' + esc(x.check.why.join('; ')) + '

' : ''; return '
' + '

' + (x.status === 'live' ? '' + esc(x.name) + '' : esc(x.name)) + '

PACKET No. ' + esc(x.no) + '
' + '

' + (CHECK[x.check && x.check.state] || '') + (x.status === 'held' ? 'Held' + (x.heldWhy ? ': ' + esc(x.heldWhy) : '') + '' : '') + stageTag(x) + '

' + notes + (x.decidedNote ? '

Note: ' + esc(x.decidedNote) + '

' : '') + '
Saved IDs' + YPB.n(x.saves) + ' / ' + YPB.n(x.need) + '
' + '

' + YPB.n(x.views) + ' verified views, ' + YPB.n(x.clicks) + ' verified clicks' + (x.spotlight ? ', Spotlight ' + YPB.n(x.spotlight.delivered) + ' / ' + YPB.n(x.spotlight.views) + ' views' : '') + '

' + '
' + (x.stage !== 'approved' ? '' : '') + (x.status === 'live' ? 'Spotlight it' : '') + '
'; } async function load() { d = await YPB.getJSON('/api/my'); if (d.error) return YPB.status(d.error, 'err'); $('who').innerHTML = 'Signed in as ' + (d.username ? '@' + esc(d.username) + '' : esc(YPB.short(d.address))) + (d.netVerified ? ', network member' : ', join the network free to plant and save IDs') + (d.test ? ' (staging test member)' : ''); $('myStats').innerHTML = [['Packets planted', d.packets.length], ['IDs saved', d.saved.length], ['Clicks to my links', d.saved.reduce((t, s) => t + (s.clicks || 0), 0)], ['Spotlights bought', d.spotlights.length]].map(s => '
' + YPB.n(s[1]) + '' + s[0] + '
').join(''); if (d.page) { $('pageBox').hidden = false; $('pageUrl').textContent = d.page; $('copyPage').onclick = e => YPB.copy(d.page, e.target); } $('myPackets').innerHTML = d.packets.length ? d.packets.map(packetBox).join('') : '

Nothing planted yet. Plant a program.

'; document.querySelectorAll('[data-go-tab]').forEach(a => { a.onclick = e => { e.preventDefault(); show(a.dataset.goTab); }; }); document.querySelectorAll('[data-edit]').forEach(b => { b.onclick = () => edit(d.packets.find(x => x.id === Number(b.dataset.edit))); }); document.querySelectorAll('[data-remove]').forEach(b => { b.onclick = async () => { b.disabled = true; const r = await YPB.post('/api/my/packet/remove', { id: Number(b.dataset.remove) }); if (r.error) { b.disabled = false; return YPB.status(r.error, 'err'); } load(); }; }); if (d.dormant && d.dormant.length) { $('dormant').hidden = false; $('dormant').innerHTML = 'Your programs from the old YourProgramBuilder
' + d.dormant.map(x => esc(x.name) + ' (' + esc(x.url) + ') ').join('
') + '

Confirming opens the planting form with your old link: put {id} where your ID goes, then plant it. It counts as your packet.

'; document.querySelectorAll('[data-dorm]').forEach(b => { b.onclick = () => { const x = d.dormant.find(y => y.id === Number(b.dataset.dorm)); reset(); $('dormantId').value = x.id; $('url').value = x.url; $('name').value = x.name; $('plantTitle').textContent = 'Confirm your old program'; show('plant'); }; }); } else $('dormant').hidden = true; $('savedRows').innerHTML = d.saved.length ? d.saved.map(s => '' + (s.shown ? '' + esc(s.name) + '' : esc(s.name) + ' (not shown now)') + '' + esc(s.refId) + '' + YPB.n(s.saves) + '' + YPB.n(s.clicks) + '' + esc(s.stage) + '').join('') : 'Open any packet in the Seed Bed and tap Save my ID.'; $('spotRows').innerHTML = d.spotlights.length ? d.spotlights.map(b => '' + esc(YPB.when(b.at)) + 'Packet ' + esc(b.packetId || '?') + '' + YPB.n(b.delivered) + ' / ' + YPB.n(b.views) + '' + esc(b.status) + '').join('') : 'No Spotlights bought yet.'; $('earnStats').innerHTML = '
' + YPB.polOf(d.earned.wei) + 'POL paid to you by Spotlights
'; $('earnRows').innerHTML = d.earned.items.length ? d.earned.items.map(i => '' + esc(YPB.when(i.at)) + 'Level ' + i.kind + '' + YPB.polOf(i.wei) + ' POL' + (i.failed ? ' (waiting to claim)' : '') + '' + (i.buyerName ? '@' + esc(i.buyerName) : '') + '').join('') : 'Nothing yet. Nothing is promised.'; } function reset() { $('plantForm').reset(); $('editId').value = ''; $('dormantId').value = ''; $('cancelEdit').hidden = true; $('costRow').hidden = true; $('plantTitle').textContent = 'Plant a program'; $('plantBtn').textContent = 'Plant it'; $('cat').value = '0'; } function edit(x) { reset(); $('editId').value = x.id; $('url').value = x.url; $('myId').value = x.ownerId || ''; $('name').value = x.name; $('desc').value = x.desc; document.querySelector('input[name=cost][value=' + (x.cost === 'paid' ? 'paid' : 'free') + ']').checked = true; $('costRow').hidden = x.cost !== 'paid'; $('costText').value = x.costText || ''; $('contract').value = x.contract || ''; $('cat').value = x.cat || '0'; $('plantTitle').textContent = 'Edit packet No. ' + x.no; $('plantBtn').textContent = 'Save the changes'; $('cancelEdit').hidden = false; show('plant'); } $('cancelEdit').onclick = () => { reset(); show('packets'); }; $('plantForm').onsubmit = async e => { e.preventDefault(); $('plantBtn').disabled = true; YPB.status('Checking your link...', ''); const body = { id: $('editId').value || undefined, dormant: $('dormantId').value || undefined, url: $('url').value.trim(), myId: $('myId').value.trim(), name: $('name').value, desc: $('desc').value, cost: costPicked(), costText: $('costText').value, contract: $('contract').value.trim(), cat: $('cat').value }; const r = await YPB.post('/api/my/plant', body); $('plantBtn').disabled = false; if (r.error) { YPB.status(r.error, 'err'); const f = r.field && $(r.field); if (f && f.focus) f.focus(); return; } const pk = r.packet; const no = String(pk.id).padStart(3, '0'); YPB.status(pk.check === 'green' ? 'Planted. Packet No. ' + no + ' is in the Seed Bed now.' : 'Planted. Packet No. ' + no + ' waits for a person to look at it first (' + pk.why.join('; ') + ').', pk.check === 'green' ? 'ok' : 'warn'); reset(); await load(); }; // the AI writer (through the hub): a name and a one-sentence description from the program's own page; picking one fills the form if (c.aiWriter) { $('aiBox').hidden = false; $('aiWrite').onclick = async () => { const box = $('aiList'), url = $('url').value.trim(); if (!/^https?:\/\//i.test(url)) { box.innerHTML = '

Put your referral link in the first box first.

'; return; } $('aiWrite').disabled = true; box.innerHTML = '

Reading the program page...

'; const r = await YPB.post('/api/my/adwriter', { url }); $('aiWrite').disabled = false; if (r.error) { box.innerHTML = '

' + esc(r.error) + '

'; return; } box.innerHTML = r.options.map((o, i) => '').join('') + '

' + (r.left != null ? r.left + ' writes left today. ' : '') + 'Pick one, then change any words you like.

'; box.querySelectorAll('[data-ai]').forEach(b => { b.onclick = () => { const o = r.options[Number(b.dataset.ai)]; $('name').value = String(o.headline || '').slice(0, 40); if (o.line) $('desc').value = String(o.line).slice(0, 200); }; }); }; } await load(); })();