@nibgate/sdk 0.2.18 → 0.2.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nibgate/sdk",
3
- "version": "0.2.18",
3
+ "version": "0.2.20",
4
4
  "description": "Framework-agnostic browser and server package for creator-owned gated content, unlock events, and receipts.",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -89,7 +89,7 @@ export function renderDefaultUnlockUI(container, resource, options = {}) {
89
89
  <button type="button" data-nibgate-unlock disabled class="nui-btn nui-btn-primary" style="width:100%;padding:24px 32px;font-size:24px;position:relative;z-index:4;background:${theme.accent};transition:transform .1s,opacity .15s;display:flex">${lockSVG}Hold to pay ${esc(resource.price)} USDC</button></div>
90
90
  <div class="nui-stat" style="text-align:center;margin-top:16px" data-nibgate-status></div>
91
91
  </div>
92
- <div data-nibgate-premium hidden style="margin-top:32px;border-top:1px solid ${theme.border};padding-top:32px"></div>
92
+ <div data-nibgate-premium hidden style="margin-top:32px;border-top:1px solid ${theme.border};padding-top:32px">${options.premiumContentHTML || ''}</div>
93
93
  `;
94
94
 
95
95
  (typeof container === 'string' ? document.querySelector(container) : container)?.appendChild(card);
@@ -142,6 +142,19 @@ export function renderDefaultUnlockUI(container, resource, options = {}) {
142
142
  status: '[data-nibgate-status]',
143
143
  unlockedTarget: '[data-nibgate-premium]',
144
144
  onStatus: (msg) => status(st, msg),
145
+ onUnlock: options.premiumContentUrl ? async () => {
146
+ const premiumEl = card.querySelector('[data-nibgate-premium]');
147
+ if (!premiumEl) return;
148
+ premiumEl.innerHTML = 'Loading content...';
149
+ try {
150
+ const res = await fetch(options.premiumContentUrl);
151
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
152
+ const html = await res.text();
153
+ premiumEl.innerHTML = html;
154
+ } catch {
155
+ premiumEl.innerHTML = 'Could not load premium content. Refresh and try again.';
156
+ }
157
+ } : options.onUnlock,
145
158
  });
146
159
 
147
160
  function shortAddress(a) { return a ? a.slice(0, 6) + '...' + a.slice(-4) : ''; }
@@ -228,7 +241,71 @@ export function renderDefaultUnlockUI(container, resource, options = {}) {
228
241
  const cleanup = () => { document.removeEventListener('mouseup', cancelHold); document.removeEventListener('touchend', cancelHold); };
229
242
  card.addEventListener('remove', cleanup);
230
243
 
231
- return { ...ctrl, element: card, destroy: () => { cleanup(); card.remove(); } };
244
+ // ── Gateway balance + deposit icon ─────────────────────────────────────
245
+ let gwOverlayEl = null;
246
+ let balTimer = null;
247
+ let balEl = null;
248
+
249
+ function depositIconHTML() {
250
+ return '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:inline;vertical-align:baseline"><path d="M12 17V3"/><path d="m6 11 6 6 6-6"/><path d="M19 21H5"/></svg>';
251
+ }
252
+
253
+ async function showGatewayWallet() {
254
+ if (gwOverlayEl) return;
255
+ gwOverlayEl = el('div', { style: 'position:fixed;inset:0;z-index:9999;background:rgba(0,0,0,0.5);display:flex;align-items:center;justify-content:center;padding:20px;animation:nfade .15s ease-out' });
256
+ const modal = el('div', { style: 'background:' + theme.bg + ';border-radius:16px;max-width:540px;width:100%;max-height:90vh;overflow:auto;position:relative;box-shadow:0 8px 32px rgba(0,0,0,0.12);animation:nscale .15s ease-out' });
257
+ const close = el('button', { style: 'position:absolute;top:12px;right:16px;z-index:20;background:none;border:none;font-size:28px;cursor:pointer;color:' + theme.muted + ';font-family:inherit;line-height:1' }, '\u00d7');
258
+ close.addEventListener('click', closeGatewayWallet);
259
+ modal.appendChild(close);
260
+ gwOverlayEl.appendChild(modal);
261
+ gwOverlayEl.addEventListener('click', (e) => { if (e.target === gwOverlayEl) closeGatewayWallet(); });
262
+ document.body.appendChild(gwOverlayEl);
263
+ document.addEventListener('keydown', onGwKey);
264
+
265
+ const gw = await import('./default-ui.js');
266
+ gw.renderDefaultGatewayWalletUI(modal, options.gatewayOptions || {});
267
+ }
268
+
269
+ function closeGatewayWallet() {
270
+ if (!gwOverlayEl) return;
271
+ gwOverlayEl.remove();
272
+ gwOverlayEl = null;
273
+ document.removeEventListener('keydown', onGwKey);
274
+ }
275
+
276
+ function onGwKey(e) { if (e.key === 'Escape') closeGatewayWallet(); }
277
+
278
+ function ensureBalEl() {
279
+ if (balEl && balEl.isConnected) return balEl;
280
+ balEl = el('span', { 'data-nibgate-bal': '', style: 'margin-left:6px;cursor:pointer;white-space:nowrap' }, depositIconHTML() + ' <span data-nibgate-bal-text></span>');
281
+ balEl.addEventListener('click', showGatewayWallet);
282
+ if (label.parentNode) label.parentNode.insertBefore(balEl, label.nextSibling);
283
+ return balEl;
284
+ }
285
+
286
+ async function refreshBalance() {
287
+ if (stateRef?.destroyed) return;
288
+ const addr = ctrl.getWalletAddress();
289
+ if (!addr) return;
290
+ const txt = ensureBalEl().querySelector('[data-nibgate-bal-text]');
291
+ if (options.gatewayBalanceUrl) {
292
+ try {
293
+ const res = await fetch(options.gatewayBalanceUrl + '?address=' + encodeURIComponent(addr));
294
+ const data = await res.json();
295
+ if (txt) txt.textContent = data?.balance || data?.availableBalance || '\u2026';
296
+ return;
297
+ } catch {}
298
+ }
299
+ if (txt) txt.textContent = '\u2026';
300
+ }
301
+
302
+ if (window.ethereum) {
303
+ balTimer = setInterval(refreshBalance, 3000);
304
+ setTimeout(refreshBalance, 1000);
305
+ window.ethereum.on('accountsChanged', refreshBalance);
306
+ }
307
+
308
+ return { ...ctrl, element: card, destroy: () => { cleanup(); card.remove(); if (balTimer) clearInterval(balTimer); closeGatewayWallet(); } };
232
309
  }
233
310
 
234
311
  export function renderDefaultRatingUI(container, resource, options = {}) {