Repository navigation
Expand file tree
/
Copy pathsn-ui.js
More file actions
128 lines (125 loc) · 7.74 KB
/
Copy pathsn-ui.js
File metadata and controls
128 lines (125 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* SyncNet UI helpers shared by every page: the sync-state glyph, the top-bar wallet control, current-page marking,
* copy buttons and small formatting helpers. Plain script, no framework.
*
* SYNC STATE answers exactly one question — what is this project's control state? — with three values:
* UNCLAIMED two parallel lines (not converged)
* SYNCING two lines converging (only while a sync is actually in progress)
* SYNCED one resulting line
* The SHAPE carries the state; colour never does. Other dimensions (for sale, home live, network hub) are separate.
*/
(function () {
'use strict';
const GLYPH = {
unclaimed: '<path d="M1.5 3.2H16.5"/><path d="M1.5 8.8H16.5"/>',
syncing: '<path d="M1.5 3H5.5C8.5 3 9 5.4 11.4 5.6"/><path d="M1.5 9H5.5C8.5 9 9 6.6 11.4 6.4"/><path d="M13.6 6H16.5"/>',
synced: '<path class="is-strong" d="M1.5 6H16.5"/>',
};
const LABEL = { unclaimed: 'Unclaimed', syncing: 'Syncing', synced: 'Synced' };
function glyph(stateName, cls) {
const s = GLYPH[stateName] ? stateName : 'unclaimed';
return `<svg class="sn-glyph${cls ? ' ' + cls : ''}" viewBox="0 0 18 12" aria-hidden="true" focusable="false" data-glyph="${s}">${GLYPH[s]}</svg>`;
}
function stateHtml(stateName, text) {
const s = GLYPH[stateName] ? stateName : 'unclaimed';
return `<span class="sn-state" data-state="${s}">${glyph(s)}<span>${esc(text || LABEL[s])}</span></span>`;
}
/** The ceremonial transition when a project actually becomes SYNCED: two lines → converging → one line. */
function animateSync(el) {
if (!el) return Promise.resolve();
const reduce = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const set = (s) => { const svg = el.querySelector('.sn-glyph'); if (svg) { svg.innerHTML = GLYPH[s]; svg.dataset.glyph = s; } el.dataset.state = s; const t = el.querySelector('span'); if (t) t.textContent = LABEL[s]; };
if (reduce) { set('synced'); return Promise.resolve(); }
set('syncing');
return new Promise((r) => setTimeout(() => { set('synced'); r(); }, 650));
}
const esc = (v) => String(v == null ? '' : v).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
const short = (a) => (/^0x[0-9a-fA-F]{40}$/.test(String(a || '')) ? String(a).slice(0, 6) + '…' + String(a).slice(-4) : String(a || ''));
const isAddr = (a) => /^0x[0-9a-fA-F]{40}$/.test(String(a || '').trim());
/** Neutral logo fallback: the project's own initial, never the SyncNet mark. */
function logoHtml(uri, letter, cls) {
const Ipfs = window.SyncNetIpfs;
const img = uri && Ipfs ? Ipfs.imgHtml(uri, { letter, alt: '' }) : '';
return `<span class="sn-logo${cls ? ' ' + cls : ''}" aria-hidden="true">${img || esc(String(letter || '·').charAt(0).toUpperCase())}</span>`;
}
// ---- top bar
function markCurrent() {
const p = location.pathname;
const key = p === '/' || p === '/index.html' || p === '/for-sale' || p.startsWith('/project/') || p.startsWith('/token/') || p === '/network.html' ? 'explore'
: p === '/build.html' ? 'create' : p === '/you.html' || p === '/home-editor.html' ? 'you' : p === '/labs/early' || p.startsWith('/labs/early/') ? 'early' : '';
document.querySelectorAll('[data-nav]').forEach((a) => { if (a.dataset.nav === key) a.setAttribute('aria-current', 'page'); else a.removeAttribute('aria-current'); });
}
// ---- EARLY in the primary navigation (Explore · Create · EARLY · My Projects; mobile: Explore · Create · Early · You).
// Shown ONLY when this deployment has EARLY enabled (GET /api/config → early): a closed feature is never a nav dead end.
// Built here, once, for every page, so the page markup and the stylesheets stay exactly as they are; with EARLY off nothing changes.
const EARLY_TAB_ICON = '<svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="8"/><path d="M12 7.5V12l3 2"/></svg>';
function addEarlyNav() {
const create = document.querySelector('.sn-nav [data-nav="create"]'), tab = document.querySelector('.sn-tabbar [data-nav="create"]');
if (document.querySelector('[data-nav="early"]') || (!create && !tab)) return;
if (create) { const a = document.createElement('a'); a.href = '/labs/early'; a.dataset.nav = 'early'; a.textContent = 'Early'; create.after(a); }
if (tab) { const a = document.createElement('a'); a.href = '/labs/early'; a.dataset.nav = 'early'; a.innerHTML = EARLY_TAB_ICON + 'Early'; tab.after(a); tab.parentElement.style.gridTemplateColumns = 'repeat(4,1fr)'; }
markCurrent();
}
function removeEarlyNav() {
const bar = document.querySelector('.sn-tabbar');
document.querySelectorAll('[data-nav="early"]').forEach((a) => a.remove());
if (bar) { bar.style.gridTemplateColumns = ''; if (!bar.getAttribute('style')) bar.removeAttribute('style'); }
}
function wireEarlyNav() {
let cached = ''; try { cached = sessionStorage.getItem('syncnet_early_nav') || ''; } catch { /* storage may be blocked */ }
if (cached === '1') addEarlyNav(); // the last answer, so later pages do not flash
if (typeof fetch !== 'function') return;
fetch('/api/config', { cache: 'no-store' }).then((r) => (r.ok ? r.json() : null)).then((j) => {
const on = Boolean(j && j.early === true);
try { sessionStorage.setItem('syncnet_early_nav', on ? '1' : '0'); } catch { /* storage may be blocked */ }
if (on) addEarlyNav(); else removeEarlyNav();
}).catch(() => { /* config unavailable: the nav simply stays as it is */ });
}
function wireWallet() {
const W = window.SyncNetWallet;
const btn = document.querySelector('[data-wallet]');
const you = document.querySelector('.sn-nav .sn-you');
if (!W || !btn) return;
W.onChange((s) => {
if (s.connected) {
btn.innerHTML = `<span class="sn-mono">${esc(short(s.account))}</span>`;
btn.setAttribute('aria-label', 'Wallet ' + s.account + ' — open My Projects');
btn.dataset.connected = 'true';
if (you) you.hidden = false;
} else {
btn.textContent = 'Connect';
btn.removeAttribute('aria-label');
btn.dataset.connected = 'false';
if (you) you.hidden = true;
}
});
btn.addEventListener('click', async () => {
if (W.state().connected) { location.href = '/you.html'; return; }
try { await W.connect(); } catch (e) { alertInline(e.message || 'The wallet did not connect.'); }
});
}
function alertInline(msg) {
let el = document.getElementById('snTopNote');
if (!el) { el = document.createElement('div'); el.id = 'snTopNote'; el.setAttribute('role', 'status'); el.className = 'sn-wrap sn-small sn-dim'; el.style.padding = '10px 0'; document.querySelector('.sn-top')?.after(el); }
el.textContent = msg;
}
function wireCopy() {
document.addEventListener('click', async (e) => {
const b = e.target.closest('[data-copy-text]');
if (!b) return;
try { await navigator.clipboard.writeText(b.getAttribute('data-copy-text')); const t = b.textContent; b.textContent = 'Copied'; setTimeout(() => { b.textContent = t; }, 1200); } catch { /* clipboard may be unavailable */ }
});
}
function wireTopSearch() {
const f = document.querySelector('.sn-top-search');
if (!f) return;
f.addEventListener('submit', (e) => {
const q = f.querySelector('input').value.trim();
if (isAddr(q)) { e.preventDefault(); location.href = '/project/' + q.toLowerCase(); }
else if (!q) e.preventDefault();
});
}
function init() { markCurrent(); wireWallet(); wireCopy(); wireTopSearch(); wireEarlyNav(); }
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init); else init();
window.SyncNetUI = Object.freeze({ glyph, stateHtml, animateSync, esc, short, isAddr, logoHtml, LABEL });
})();