// public/js/ui.js - all DOM updates. Show message text with textContent, never innerHTML. const el = (id) => document.getElementById(id); function clear(node) { while (node.firstChild) node.removeChild(node.firstChild); } function button(label, onClick, { active = false } = {}) { const b = document.createElement('button'); b.type = 'button'; b.textContent = label; if (active) b.classList.add('active'); b.addEventListener('click', onClick); return b; } const MSG_STYLE_KEY = 'oscar.msgStyle'; export function getMessageStyle() { const v = localStorage.getItem(MSG_STYLE_KEY); return v === 'classic' ? 'classic' : 'bubbles'; } const NAME_COLORS = [ '#6ee7ff', '#ff7ab6', '#7CFF6B', '#ffd166', '#c792ea', '#ff9f43', '#4dd0e1', '#f78fb3', '#a8e063', '#82aaff', '#f6c177', '#ff6b6b', '#5ad1a3', '#d4a5ff', '#f9f871', ]; function nameColor(name) { let hash = 0; const s = String(name || 'unknown').toLowerCase(); for (let i = 0; i < s.length; i++) hash = ((hash << 5) - hash) + s.charCodeAt(i); return NAME_COLORS[Math.abs(hash) % NAME_COLORS.length]; } export function setMessageStyle(style) { const v = style === 'classic' ? 'classic' : 'bubbles'; localStorage.setItem(MSG_STYLE_KEY, v); applyMessageStyle(); const btn = document.getElementById('styleToggleBtn'); if (btn) btn.textContent = v === 'classic' ? 'Classic' : 'Bubbles'; } export function applyMessageStyle() { const box = document.getElementById('messages'); if (!box) return; const style = getMessageStyle(); box.classList.toggle('style-bubbles', style === 'bubbles'); box.classList.toggle('style-classic', style === 'classic'); } export function showLogin() { el('loginView').classList.remove('hidden'); el('chatView').classList.add('hidden'); el('logoutBtn').classList.add('hidden'); el('tocStatus').classList.add('hidden'); const styleBtn = document.getElementById('styleToggleBtn'); if (styleBtn) styleBtn.classList.add('hidden'); el('me').textContent = ''; } export function showChat(screenName) { el('loginView').classList.add('hidden'); el('chatView').classList.remove('hidden'); el('logoutBtn').classList.remove('hidden'); el('tocStatus').classList.remove('hidden'); const styleBtn = document.getElementById('styleToggleBtn'); if (styleBtn) styleBtn.classList.remove('hidden'); el('me').textContent = `Signed in as ${screenName}`; } export function setLoginError(msg) { el('loginError').textContent = msg || ''; } export function setChatError(msg) { el('chatError').textContent = msg || ''; } export function showBroadcast(text) { const banner = el('broadcastBanner'); banner.textContent = `Broadcast: ${text}`; banner.classList.remove('hidden'); } export function renderConversations(state, onClose) { const list = el('conversations'); clear(list); for (const conv of Object.values(state.conversations)) { const li = document.createElement('li'); li.className = 'tab' + (state.active === conv.key ? ' active' : ''); const labelBtn = document.createElement('button'); labelBtn.type = 'button'; labelBtn.className = 'tab-label'; labelBtn.textContent = conv.type === 'chat' ? `# ${conv.name}` : conv.name; labelBtn.title = conv.type === 'chat' ? `Chat: ${conv.name}` : `IM: ${conv.name}`; labelBtn.addEventListener('click', () => state.selectConversation(conv.key)); const closeBtn = document.createElement('button'); closeBtn.type = 'button'; closeBtn.className = 'tab-close'; closeBtn.textContent = '×'; closeBtn.title = conv.type === 'chat' ? 'Leave/close chat' : 'Close conversation'; closeBtn.addEventListener('click', (e) => { e.stopPropagation(); if (typeof onClose === 'function') onClose(conv.key); }); li.append(labelBtn, closeBtn); list.appendChild(li); } } export function renderMessages(state) { const conv = state.conversations[state.active]; el('chatTitle').textContent = conv ? (conv.type === 'chat' ? `# ${conv.name}` : conv.name) : 'No conversation selected'; el('chatMeta').textContent = conv ? (conv.type === 'chat' ? 'Chat room' : 'Direct message') : ''; const box = el('messages'); applyMessageStyle(); clear(box); if (!conv) return; for (const msg of conv.messages) { const item = document.createElement('div'); item.className = 'msg' + (msg.mine ? ' me' : ''); const meta = document.createElement('div'); meta.className = 'meta'; const when = msg.time ? new Date(msg.time).toLocaleTimeString() : ''; const from = msg.from || 'unknown'; const style = getMessageStyle(); if (style === 'classic') { const timeSpan = document.createElement('span'); timeSpan.textContent = `[${when || ''}] `; const nameSpan = document.createElement('span'); nameSpan.textContent = `<${from}>`; nameSpan.style.color = nameColor(from); meta.append(timeSpan, nameSpan, document.createTextNode(' ')); } else { meta.textContent = `${from}${when ? ' · ' + when : ''}`; } const body = document.createElement('div'); body.className = 'body'; body.textContent = msg.text || ''; // XSS-safe item.append(meta, body); box.appendChild(item); } box.scrollTop = box.scrollHeight; const input = el('messageInput'); if (input) input.focus(); } export function renderUserResults(users, onOpen) { const list = el('userResults'); clear(list); for (const name of users) { const li = document.createElement('li'); li.appendChild(button(name, () => onOpen(name))); list.appendChild(li); } } function buddyStatus(b) { const raw = String(b?.state || b?.status || '').toLowerCase(); if (b?.online === true || b?.online === 'true' || b?.online === 1) return 'online'; if (['offline', 'disconnected'].includes(raw)) return 'offline'; if (['online', 'present', 'active', 'away', 'idle', 'dnd', 'busy', 'na'].includes(raw)) return 'online'; if (b?.online === false || b?.online === 'false' || b?.online === 0) return 'offline'; // Only online/offline supported; unknown defaults to offline. return 'offline'; } export function renderBuddies(buddies, onOpen) { const list = el('buddies'); clear(list); for (const b of buddies) { const li = document.createElement('li'); const name = typeof b === 'string' ? b : (b.aimId || b.displayId || b.screenName || b.name || JSON.stringify(b)); const status = typeof b === 'string' ? 'offline' : buddyStatus(b); const title = status === 'online' ? 'Online' : 'Offline'; const btn = document.createElement('button'); btn.type = 'button'; btn.className = 'buddy-item'; btn.title = title; btn.addEventListener('click', () => onOpen(name)); const dot = document.createElement('span'); dot.className = `status-dot status-${status}`; const label = document.createElement('span'); label.className = 'buddy-name'; label.textContent = name; btn.append(dot, label); li.appendChild(btn); list.appendChild(li); } } export function renderInvites(invites, onAccept, onIgnore) { const list = document.getElementById('invites'); const card = document.getElementById('invitesCard'); if (!list) return; clear(list); const items = invites || []; if (card) { card.classList.toggle('hidden', items.length === 0); card.classList.toggle('invite-attention', items.length > 0); } for (const inv of items) { const li = document.createElement('li'); li.className = 'invite'; const title = document.createElement('div'); title.textContent = `${inv.from} invited you to "${inv.roomName}"`; const msg = document.createElement('div'); msg.className = 'meta'; msg.textContent = inv.message || ''; const actions = document.createElement('div'); actions.className = 'invite-actions'; const join = document.createElement('button'); join.type = 'button'; join.textContent = 'Join'; join.addEventListener('click', () => onAccept && onAccept(inv.roomId)); const ignore = document.createElement('button'); ignore.type = 'button'; ignore.textContent = 'Ignore'; ignore.addEventListener('click', () => onIgnore && onIgnore(inv.roomId)); actions.append(join, ignore); li.append(title, msg, actions); list.appendChild(li); } } export function setTocStatus(connected, detail = '') { const wrap = document.getElementById('tocStatus'); if (!wrap) return; const dot = wrap.querySelector('.dot'); if (dot) { dot.classList.toggle('dot-on', !!connected); dot.classList.toggle('dot-off', !connected); } const labelEl = wrap.querySelector('.toc-label'); if (labelEl) labelEl.textContent = connected ? 'Connected' : 'Disconnected'; const label = connected ? 'TOC connected' : 'TOC disconnected'; wrap.title = detail ? `${label}: ${detail}` : label; wrap.classList.remove('hidden'); }