summaryrefslogtreecommitdiff
path: root/lib/brainstorm-server/helper.js
diff options
context:
space:
mode:
authorJesse Vincent <jesse@fsck.com>2026-03-09 15:35:02 -0700
committerJesse Vincent <jesse@fsck.com>2026-03-09 15:35:02 -0700
commit133a0a80c65c9f71206e31a08511fab1869354f2 (patch)
treeb658a1b53fb78d962a3a78bd941e0dd14715853f /lib/brainstorm-server/helper.js
parentd2d6cf485286ca9af69b84557e4da4c1d83a58e0 (diff)
parent57b346ddbcd338334e7598089f93f7340ac42d58 (diff)
Merge dev-reorder10: Release v5.0.0v5.0.0
Diffstat (limited to 'lib/brainstorm-server/helper.js')
-rw-r--r--lib/brainstorm-server/helper.js88
1 files changed, 88 insertions, 0 deletions
diff --git a/lib/brainstorm-server/helper.js b/lib/brainstorm-server/helper.js
new file mode 100644
index 0000000..111f97f
--- /dev/null
+++ b/lib/brainstorm-server/helper.js
@@ -0,0 +1,88 @@
+(function() {
+ const WS_URL = 'ws://' + window.location.host;
+ let ws = null;
+ let eventQueue = [];
+
+ function connect() {
+ ws = new WebSocket(WS_URL);
+
+ ws.onopen = () => {
+ eventQueue.forEach(e => ws.send(JSON.stringify(e)));
+ eventQueue = [];
+ };
+
+ ws.onmessage = (msg) => {
+ const data = JSON.parse(msg.data);
+ if (data.type === 'reload') {
+ window.location.reload();
+ }
+ };
+
+ ws.onclose = () => {
+ setTimeout(connect, 1000);
+ };
+ }
+
+ function sendEvent(event) {
+ event.timestamp = Date.now();
+ if (ws && ws.readyState === WebSocket.OPEN) {
+ ws.send(JSON.stringify(event));
+ } else {
+ eventQueue.push(event);
+ }
+ }
+
+ // Capture clicks on choice elements
+ document.addEventListener('click', (e) => {
+ const target = e.target.closest('[data-choice]');
+ if (!target) return;
+
+ sendEvent({
+ type: 'click',
+ text: target.textContent.trim(),
+ choice: target.dataset.choice,
+ id: target.id || null
+ });
+
+ // Update indicator bar (defer so toggleSelect runs first)
+ setTimeout(() => {
+ const indicator = document.getElementById('indicator-text');
+ if (!indicator) return;
+ const container = target.closest('.options') || target.closest('.cards');
+ const selected = container ? container.querySelectorAll('.selected') : [];
+ if (selected.length === 0) {
+ indicator.textContent = 'Click an option above, then return to the terminal';
+ } else if (selected.length === 1) {
+ const label = selected[0].querySelector('h3, .content h3, .card-body h3')?.textContent?.trim() || selected[0].dataset.choice;
+ indicator.innerHTML = '<span class="selected-text">' + label + ' selected</span> — return to terminal to continue';
+ } else {
+ indicator.innerHTML = '<span class="selected-text">' + selected.length + ' selected</span> — return to terminal to continue';
+ }
+ }, 0);
+ });
+
+ // Frame UI: selection tracking
+ window.selectedChoice = null;
+
+ window.toggleSelect = function(el) {
+ const container = el.closest('.options') || el.closest('.cards');
+ const multi = container && container.dataset.multiselect !== undefined;
+ if (container && !multi) {
+ container.querySelectorAll('.option, .card').forEach(o => o.classList.remove('selected'));
+ }
+ if (multi) {
+ el.classList.toggle('selected');
+ } else {
+ el.classList.add('selected');
+ }
+ window.selectedChoice = el.dataset.choice;
+ };
+
+ // Expose API for explicit use
+ window.brainstorm = {
+ send: sendEvent,
+ choice: (value, metadata = {}) => sendEvent({ type: 'choice', value, ...metadata })
+ };
+
+ connect();
+})();