/** * The Rooms tab (DESIGN.md §12.7): live rooms now (watching, heat, a live * line of chat), Moments (today's hero, live detections, your clips), Need * eyes near you, and crews: yours, and ones to discover. */ import { icon } from './icons.js'; import { $, esc, ago, haptic, toast, esriThumb, reducedMotion } from './dom.js'; import { BAND_COLOR, bandOf } from './bands.js'; import { viewerCount, sampleLines, chatGenre } from './chatSim.js'; import { richText } from './emotes.js'; import { simTier, hashStr } from './signal.js'; import { crewAvatar, fmtM } from './crews.js'; const fmtV = (v) => (v >= 1000 ? `${(v / 1000).toFixed(1)}k` : String(v)); export function createRoomsTab(el, core) { let tickers = []; let tickTimer = 0; let live = []; let moments = []; el.classList.add('rooms-tab'); core.ctx.bindScroll?.(el); el.addEventListener( 'scroll', () => el.classList.toggle('is-scrolled', el.scrollTop > 44), { passive: true }, ); function heatBars(s) { let x = hashStr(s.id); const bars = []; for (let i = 0; i < 12; i += 1) { x = (Math.imul(x, 1103515245) + 12345) >>> 0; bars.push(0.25 + ((x >>> 8) % 100) / 133 + i / 30); } return ``; } function liveRow(s, i) { const band = bandOf(s); const v = viewerCount(s) + Math.max(0, core.rt.presence(String(s.id)).length - 1); const thumb = core.globe?.cachedThumbnail?.(s) || s.thumbUrl || s.thumbnail || ''; return ``; } function momentHero(m) { const ms = Math.max( 1, Math.round(((+m.time?.end || 0) - (+m.time?.start || 0)) / 60e3), ); return `
${icon('sparkles')}Today’s Moment${m.illustrative ? 'Illustrative' : 'Detected · ADS-B'}

${esc(m.title)}

${esc(m.place || '')} · ${ms} min · ${(m.tracks || []).length || 3} aircraft · ${(m.captures || []).length} sealed captures
`; } async function render() { const sits = (await core.situations()) || []; moments = await core.moments(); const scored = sits .filter((s) => Number.isFinite(s.lat)) .map((s) => ({ s, v: viewerCount(s) * (1 + (s.severity || 0) * 0.2) })) .sort((a, b) => b.v - a.v); // Diversity: one per chat genre. const seen = new Set(); live = []; for (const x of scored) { const g = chatGenre(x.s); if (seen.has(g)) continue; seen.add(g); live.push(x.s); if (live.length >= 6) break; } const detections = sits .filter((s) => s.momentId || /circling/.test(s.kind || '')) .slice(0, 4); const loc = core.myLoc(); const needs = core.needs .near(loc, 60) .filter((r) => r.status === 'open') .slice(0, 3); const crews = core.crews.list(); const mine = crews.filter((c) => c.joined); const other = crews.filter((c) => !c.joined); const hero = moments[0]; el.innerHTML = `

Rooms

Live is fast. The Record is true. Signal rewards being right, early, with receipts.

Live now ${live.reduce((a, s) => a + viewerCount(s), 0).toLocaleString()} watching
${live.map(liveRow).join('') || '
Nothing unusual right now. That’s a valid state.
'}
Moments 4D replays with chat and the record
${hero ? momentHero(hero) : ''} ${ detections.length || moments.length > 1 ? `
${[ ...moments .slice(1) .map( (m) => ``, ), ...detections.map( (s) => ``, ), ].join('')}
` : '' }
${icon('clock')}Your clipsSelect a range on the time machine and “Clip this Moment”. It lands here with its receipts.
Need eyes near you ${loc?.guessed ? 'approximate · share location for distances' : 'one quiet ask each'}
${needs.length ? needs.map((n) => core.needs.cardHtml(n)).join('') : '
No open requests near you. Calm is a feature.
'}
Your crews ${mine.length ? `${mine.length} joined` : 'communities, not followers'}
${mine.map((c) => ``).join('') || '
Join a crew to see its feed here.
'}
${ other.length ? `
Discover crews
${other.map((c) => `
${crewAvatar(c)}${esc(c.name)}${esc(c.tagline)}
`).join('')}
` : '' }

Simulated chat and example crews are labelled. Your posts, votes, captures and Signal are real and stay on this device.

`; // Tickers: a real-feeling line of each room's chat. tickers = live.map((s) => ({ s, lines: sampleLines(s, 8), i: 0 })); tickAll(true); clearInterval(tickTimer); tickTimer = setInterval(() => tickAll(false), 2600); } function tickAll(first) { if (!el.isConnected || (el.offsetParent === null && !first)) return; tickers.forEach((t, k) => { const node = $(`[data-tick="${k}"]`, el); if (!node || !t.lines.length) return; // Stagger so the list shimmers rather than blinks. if (!first && Math.random() < 0.45) return; const m = t.lines[t.i % t.lines.length]; t.i += 1; const tier = m.tier || simTier(m.handle); node.innerHTML = `${esc(m.handle.slice(1))} ${richText(m.text).html}`; }); } el.addEventListener('click', async (e) => { const room = e.target.closest('[data-room]'); if (room) { const sits = await core.situations(); const s = sits.find((x) => String(x.id) === room.dataset.room); if (s) core.openRoom(s, { lane: 'live' }); return; } const mo = e.target.closest('[data-mo]'); if (mo) { haptic('light'); const m = moments.find((x) => x.id === mo.dataset.id) || (await core.moment(mo.dataset.id)); if (mo.dataset.mo === 'play') { if (core.ctx.onPlayMoment) core.ctx.onPlayMoment(m); else core.openRoom(m, { lane: 'live' }); } else core.openRoom(m, { lane: mo.dataset.mo === 'record' ? 'record' : 'live', }); return; } const nd = e.target.closest('[data-need-act]'); if (nd) { core.needs.act(nd.dataset.needAct, nd.dataset.need); return; } const jn = e.target.closest('[data-join]'); if (jn) { core.crews.toggle(jn.dataset.join); haptic('success'); toast('Joined', { icon: icon('users') }); render(); return; } const cr = e.target.closest('[data-crew]'); if (cr) core.crews.open(cr.dataset.crew); }); const offN = core.needs.on('change', () => el.isConnected && render()); const offC = core.crews.on('change', () => el.isConnected && render()); render(); return { refresh: render, destroy() { clearInterval(tickTimer); offN?.(); offC?.(); el.innerHTML = ''; }, }; }