const FIREBASE_CONFIG = { apiKey: "AIzaSyDuSjEGEKx5FnWYnQq8f_owbpYRBRrl5x0", authDomain: "esef-514bf.firebaseapp.com", projectId: "esef-514bf", storageBucket: "esef-514bf.firebasestorage.app", messagingSenderId: "353969651290", appId: "1:353969651290:web:41e3aaf8e54c743702a263" }; // ================================================================ const MAX_CHARS = 300; const COLLECTION = "comments"; document.addEventListener('DOMContentLoaded', () => { const configured = FIREBASE_CONFIG.apiKey !== "YOUR_API_KEY"; const form = document.getElementById('comment-form'); const nameInput = document.getElementById('comment-name'); const msgInput = document.getElementById('comment-message'); const submitBtn = document.getElementById('comment-submit'); const charCountEl = document.getElementById('char-count'); const listEl = document.getElementById('comments-list'); const titleEl = document.getElementById('comments-title'); const colorInput = document.getElementById('comment-avatar-color'); const warning = document.getElementById('firebase-setup-warning'); if (!form || !listEl) return; // ── Avatar colour picker ───────────────────────────────────── document.querySelectorAll('.av-opt').forEach(btn => { btn.addEventListener('click', () => { document.querySelectorAll('.av-opt').forEach(b => b.classList.remove('selected')); btn.classList.add('selected'); colorInput.value = btn.dataset.color; }); }); // ── Char counter ───────────────────────────────────────────── charCountEl.textContent = MAX_CHARS; msgInput.addEventListener('input', () => { const left = MAX_CHARS - msgInput.value.length; charCountEl.textContent = left; charCountEl.style.color = left < 40 ? '#e05555' : '#364858'; }); // ── No Firebase → show demo + setup warning ────────────────── if (!configured) { if (warning) warning.style.display = 'block'; showDemo(listEl); form.addEventListener('submit', e => { e.preventDefault(); alert('Firebase is not connected yet!\n\nOpen js/comments.js and follow the setup instructions at the top of the file.'); }); return; } // ── Firebase init ──────────────────────────────────────────── firebase.initializeApp(FIREBASE_CONFIG); const db = firebase.firestore(); // ── Real-time feed ─────────────────────────────────────────── db.collection(COLLECTION) .orderBy('timestamp', 'desc') .onSnapshot(snap => { listEl.innerHTML = ''; if (titleEl) { titleEl.textContent = snap.size > 0 ? `(${snap.size})` : ''; } if (snap.empty) { listEl.innerHTML = '
No messages yet — be the first!
'; return; } snap.forEach(doc => listEl.appendChild(buildBubble(doc.data()))); }, err => { console.error(err); listEl.innerHTML = 'Could not load comments. Check your Firebase config.
'; }); // ── Submit ─────────────────────────────────────────────────── form.addEventListener('submit', async e => { e.preventDefault(); const name = clean(nameInput.value.trim()).slice(0, 32) || 'Anonymous'; const message = clean(msgInput.value.trim()).slice(0, MAX_CHARS); const color = colorInput.value || '#5b6fde'; if (!message) { msgInput.focus(); return; } submitBtn.disabled = true; submitBtn.textContent = 'Posting...'; try { await db.collection(COLLECTION).add({ name, message, color, timestamp: firebase.firestore.FieldValue.serverTimestamp() }); nameInput.value = ''; msgInput.value = ''; charCountEl.textContent = MAX_CHARS; charCountEl.style.color = '#364858'; } catch (err) { console.error(err); alert('Failed to post. Try again!'); } finally { submitBtn.disabled = false; submitBtn.textContent = 'POST'; } }); }); // ── Build a single comment bubble (Steam layout) ───────────────── function buildBubble(data) { const div = document.createElement('div'); div.className = 'comment-bubble'; const ts = data.timestamp ? data.timestamp.toDate() : new Date(); const timeStr = ts.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); const color = data.color || '#64748b'; const initial = (data.name || '?').charAt(0).toUpperCase(); div.innerHTML = `${esc(data.message)}