openvibers/openvibe.live Commits

openvibe.live/#vibes opens the Vibes window once (every site's account menu links its Vibes chip here, openvibe-shared 4.3) (#174)

OP OpenVibers committed 27 days ago · 2ae9e75a84 · 2 files, +45 −0

Changes

ddiff --git a/public/js/vibes.js b/public/js/vibes.js
iindex 5ab54993..795585d9 100644
---- a/public/js/vibes.js
++++ b/public/js/vibes.js
@@@ -446,3 +446,15 @@ async function doCashout() {
closeModal();
} catch (e) { toast(e.message || 'Cashout failed', 'error'); }
}
++
++// Every other OpenVibe site's account menu links its Vibes chip here (openvibe-shared 4.3: openvibe.live/#vibes): open
++// the Vibes window once, then drop the hash so a reload or a shared link does not open it again.
++(function openVibesFromLink() {
++ if (location.hash !== '#vibes') return;
++ const open = () => {
++ try { history.replaceState(history.state, '', location.pathname + location.search); } catch { /* keep the hash */ }
++ showModal('buy-funds');
++ };
++ if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', open, { once: true });
++ else setTimeout(open, 0);
++})();
ddiff --git a/test/vibes-hash.test.js b/test/vibes-hash.test.js
nnew file mode 100644
iindex 00000000..08e42559
---- /dev/null
++++ b/test/vibes-hash.test.js
@@@ -0,0 +1,33 @@
++/**
++ * openvibe-shared 4.3's account menu shows a person's Vibes on every OpenVibe site and links them to openvibe.live/#vibes.
++ * Live opens its Vibes window for that link, once: the hash goes, so a reload does not open it again.
++ *
++ * node test/vibes-hash.test.js
++ */
++'use strict';
++const assert = require('assert');
++const fs = require('fs');
++const path = require('path');
++
++const src = fs.readFileSync(path.join(__dirname, '..', 'public', 'js', 'vibes.js'), 'utf8');
++const block = src.slice(src.indexOf('(function openVibesFromLink()'));
++
++function run(hash) {
++ const opened = [];
++ const replaced = [];
++ const env = {
++ location: { hash, pathname: '/@alex', search: '?x=1' },
++ history: { state: null, replaceState: (s, t, url) => replaced.push(url) },
++ document: { readyState: 'complete', addEventListener() {} },
++ setTimeout: (fn) => fn(),
++ showModal: (id) => opened.push(id),
++ };
++ // eslint-disable-next-line no-new-func
++ new Function(...Object.keys(env), block)(...Object.values(env));
++ return { opened, replaced };
++}
++
++assert.deepStrictEqual(run('#vibes'), { opened: ['buy-funds'], replaced: ['/@alex?x=1'] }, 'the link opens the Vibes window and drops the hash');
++assert.deepStrictEqual(run(''), { opened: [], replaced: [] }, 'nothing otherwise');
++assert.deepStrictEqual(run('#chat'), { opened: [], replaced: [] });
++console.log('vibes hash: all checks passed');