| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| <title>BiteChat π¬</title> |
| <style> |
| |
| *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } |
| :root { |
| --bg: #0f1117; |
| --surface: #1a1d27; |
| --surface2: #22263a; |
| --accent: #ff6b35; |
| --accent2: #ffac35; |
| --text: #e8eaf0; |
| --muted: #7b7f96; |
| --border: #2d3148; |
| --success: #4ade80; |
| --danger: #f87171; |
| --radius: 12px; |
| --radius-sm: 6px; |
| --shadow: 0 4px 24px rgba(0,0,0,.4); |
| } |
| body { |
| font-family: 'Segoe UI', system-ui, sans-serif; |
| background: var(--bg); |
| color: var(--text); |
| height: 100dvh; |
| display: flex; |
| flex-direction: column; |
| overflow: hidden; |
| } |
| |
| #join-screen { |
| display: flex; |
| flex-direction: column; |
| align-items: center; |
| justify-content: center; |
| height: 100dvh; |
| gap: 20px; |
| padding: 24px; |
| } |
| .logo { |
| font-size: 3rem; |
| font-weight: 800; |
| background: linear-gradient(135deg, var(--accent), var(--accent2)); |
| -webkit-background-clip: text; |
| -webkit-text-fill-color: transparent; |
| background-clip: text; |
| letter-spacing: -1px; |
| } |
| .tagline { color: var(--muted); font-size: .95rem; margin-top: -12px; } |
| .join-card { |
| background: var(--surface); |
| border: 1px solid var(--border); |
| border-radius: var(--radius); |
| padding: 32px; |
| width: 100%; |
| max-width: 400px; |
| display: flex; |
| flex-direction: column; |
| gap: 14px; |
| box-shadow: var(--shadow); |
| } |
| label { font-size: .85rem; color: var(--muted); margin-bottom: 4px; display: block; } |
| input, select { |
| width: 100%; |
| padding: 10px 14px; |
| background: var(--surface2); |
| border: 1px solid var(--border); |
| border-radius: var(--radius-sm); |
| color: var(--text); |
| font-size: .95rem; |
| outline: none; |
| transition: border-color .2s; |
| } |
| input:focus, select:focus { border-color: var(--accent); } |
| .btn { |
| padding: 11px 20px; |
| border-radius: var(--radius-sm); |
| border: none; |
| cursor: pointer; |
| font-weight: 600; |
| font-size: .95rem; |
| transition: opacity .15s, transform .1s; |
| } |
| .btn:active { transform: scale(.97); } |
| .btn-primary { |
| background: linear-gradient(135deg, var(--accent), var(--accent2)); |
| color: #fff; |
| } |
| .btn-primary:hover { opacity: .9; } |
| .btn-ghost { |
| background: transparent; |
| border: 1px solid var(--border); |
| color: var(--muted); |
| } |
| .btn-ghost:hover { border-color: var(--accent); color: var(--accent); } |
| .error-msg { color: var(--danger); font-size: .85rem; display: none; } |
| |
| |
| #app { |
| display: none; |
| flex: 1; |
| overflow: hidden; |
| flex-direction: column; |
| } |
| #app.visible { display: flex; } |
| |
| |
| header { |
| display: flex; |
| align-items: center; |
| gap: 12px; |
| padding: 12px 20px; |
| background: var(--surface); |
| border-bottom: 1px solid var(--border); |
| flex-shrink: 0; |
| } |
| .header-logo { font-weight: 800; font-size: 1.2rem; |
| background: linear-gradient(135deg, var(--accent), var(--accent2)); |
| -webkit-background-clip: text; -webkit-text-fill-color: transparent; |
| background-clip: text; |
| } |
| .room-badge { |
| background: var(--surface2); |
| border: 1px solid var(--border); |
| border-radius: 99px; |
| padding: 3px 12px; |
| font-size: .82rem; |
| color: var(--muted); |
| } |
| .status-dot { |
| width: 8px; height: 8px; |
| border-radius: 50%; |
| background: var(--success); |
| flex-shrink: 0; |
| animation: pulse 2s infinite; |
| } |
| @keyframes pulse { |
| 0%,100% { opacity: 1; } |
| 50% { opacity: .4; } |
| } |
| .user-count { font-size: .82rem; color: var(--muted); margin-left: auto; } |
| .btn-leave { |
| background: transparent; |
| border: 1px solid var(--border); |
| border-radius: var(--radius-sm); |
| color: var(--muted); |
| padding: 5px 12px; |
| cursor: pointer; |
| font-size: .8rem; |
| transition: all .15s; |
| } |
| .btn-leave:hover { border-color: var(--danger); color: var(--danger); } |
| |
| .chat-body { |
| display: flex; |
| flex: 1; |
| overflow: hidden; |
| } |
| |
| |
| #sidebar { |
| width: 220px; |
| flex-shrink: 0; |
| background: var(--surface); |
| border-right: 1px solid var(--border); |
| display: flex; |
| flex-direction: column; |
| overflow: hidden; |
| } |
| .sidebar-section { |
| padding: 14px 16px 8px; |
| font-size: .72rem; |
| font-weight: 700; |
| letter-spacing: .08em; |
| color: var(--muted); |
| text-transform: uppercase; |
| } |
| #user-list, #room-list { overflow-y: auto; flex: 1; } |
| .user-item, .room-item { |
| padding: 7px 16px; |
| font-size: .88rem; |
| display: flex; |
| align-items: center; |
| gap: 8px; |
| cursor: pointer; |
| border-radius: var(--radius-sm); |
| margin: 1px 8px; |
| transition: background .15s; |
| } |
| .room-item:hover { background: var(--surface2); } |
| .room-item.active { background: var(--surface2); color: var(--accent); } |
| .user-avatar { |
| width: 24px; height: 24px; |
| border-radius: 50%; |
| background: linear-gradient(135deg, var(--accent), var(--accent2)); |
| display: flex; align-items: center; justify-content: center; |
| font-size: .65rem; font-weight: 700; color: #fff; flex-shrink: 0; |
| } |
| .new-room-row { |
| padding: 8px 12px; |
| display: flex; |
| gap: 6px; |
| } |
| .new-room-row input { |
| flex: 1; font-size: .82rem; padding: 6px 10px; |
| } |
| .new-room-row button { |
| padding: 6px 10px; font-size: .82rem; |
| background: var(--accent); border: none; border-radius: var(--radius-sm); |
| color: #fff; cursor: pointer; font-weight: 600; |
| } |
| |
| .chat-main { |
| display: flex; |
| flex-direction: column; |
| flex: 1; |
| overflow: hidden; |
| } |
| #messages { |
| flex: 1; |
| overflow-y: auto; |
| padding: 20px 24px 8px; |
| display: flex; |
| flex-direction: column; |
| gap: 4px; |
| scroll-behavior: smooth; |
| } |
| #messages::-webkit-scrollbar { width: 5px; } |
| #messages::-webkit-scrollbar-track { background: transparent; } |
| #messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; } |
| |
| .msg { display: flex; flex-direction: column; max-width: 80%; } |
| .msg.mine { align-self: flex-end; align-items: flex-end; } |
| .msg.other { align-self: flex-start; align-items: flex-start; } |
| .msg-meta { |
| font-size: .72rem; |
| color: var(--muted); |
| margin-bottom: 3px; |
| display: flex; |
| gap: 6px; |
| align-items: center; |
| } |
| .msg-meta .name { font-weight: 600; color: var(--text); } |
| .bubble { |
| padding: 9px 14px; |
| border-radius: 14px; |
| font-size: .92rem; |
| line-height: 1.5; |
| word-break: break-word; |
| } |
| .mine .bubble { |
| background: linear-gradient(135deg, var(--accent), var(--accent2)); |
| color: #fff; |
| border-bottom-right-radius: 4px; |
| } |
| .other .bubble { |
| background: var(--surface2); |
| border: 1px solid var(--border); |
| border-bottom-left-radius: 4px; |
| } |
| .system-msg { |
| align-self: center; |
| font-size: .78rem; |
| color: var(--muted); |
| padding: 4px 12px; |
| background: var(--surface2); |
| border-radius: 99px; |
| margin: 6px 0; |
| } |
| |
| |
| .input-bar { |
| padding: 14px 20px; |
| background: var(--surface); |
| border-top: 1px solid var(--border); |
| display: flex; |
| gap: 10px; |
| align-items: flex-end; |
| } |
| #msg-input { |
| flex: 1; |
| background: var(--surface2); |
| border: 1px solid var(--border); |
| border-radius: var(--radius); |
| color: var(--text); |
| font-size: .95rem; |
| padding: 10px 16px; |
| resize: none; |
| max-height: 120px; |
| outline: none; |
| transition: border-color .2s; |
| font-family: inherit; |
| line-height: 1.5; |
| } |
| #msg-input:focus { border-color: var(--accent); } |
| #msg-input::placeholder { color: var(--muted); } |
| #send-btn { |
| background: linear-gradient(135deg, var(--accent), var(--accent2)); |
| border: none; |
| border-radius: var(--radius); |
| width: 44px; height: 44px; |
| cursor: pointer; |
| display: flex; align-items: center; justify-content: center; |
| flex-shrink: 0; |
| transition: opacity .15s, transform .1s; |
| } |
| #send-btn:hover { opacity: .9; } |
| #send-btn:active { transform: scale(.93); } |
| #send-btn svg { width: 20px; height: 20px; fill: #fff; } |
| |
| |
| @media (max-width: 600px) { |
| #sidebar { display: none; } |
| .msg { max-width: 90%; } |
| } |
| </style> |
| </head> |
| <body> |
|
|
| |
| <div id="join-screen"> |
| <div class="logo">BiteChat π¬</div> |
| <div class="tagline">Chat with Hugging Face users in real-time</div> |
| <div class="join-card"> |
| <div> |
| <label for="username-input">Your name</label> |
| <input id="username-input" type="text" placeholder="e.g. llama_fan42" maxlength="32" autocomplete="off" /> |
| </div> |
| <div> |
| <label for="room-input">Room</label> |
| <input id="room-input" type="text" placeholder="general" value="general" maxlength="40" autocomplete="off" /> |
| </div> |
| <button class="btn btn-primary" id="join-btn">Join Chat β</button> |
| <span class="error-msg" id="join-error"></span> |
| </div> |
| </div> |
|
|
| |
| <div id="app"> |
| <header> |
| <span class="header-logo">BiteChat</span> |
| <span class="room-badge" id="header-room">#general</span> |
| <span class="status-dot"></span> |
| <span class="user-count" id="user-count">1 online</span> |
| <button class="btn-leave" id="leave-btn">Leave</button> |
| </header> |
|
|
| <div class="chat-body"> |
| |
| <div id="sidebar"> |
| <div class="sidebar-section">Rooms</div> |
| <div id="room-list"></div> |
| <div class="new-room-row"> |
| <input id="new-room-input" placeholder="new-room" maxlength="40" /> |
| <button id="new-room-btn">+</button> |
| </div> |
| <div class="sidebar-section">Online</div> |
| <div id="user-list"></div> |
| </div> |
|
|
| |
| <div class="chat-main"> |
| <div id="messages"></div> |
| <div class="input-bar"> |
| <textarea id="msg-input" placeholder="Say somethingβ¦" rows="1"></textarea> |
| <button id="send-btn" title="Send"> |
| <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> |
| <path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"/> |
| </svg> |
| </button> |
| </div> |
| </div> |
| </div> |
| </div> |
|
|
| <script> |
| const $ = id => document.getElementById(id); |
| |
| let ws = null; |
| let myUsername = ''; |
| let currentRoom = ''; |
| const WS_BASE = location.protocol === 'https:' ? 'wss' : 'ws'; |
| |
| |
| $('join-btn').onclick = () => connect($('username-input').value.trim(), $('room-input').value.trim() || 'general'); |
| $('username-input').onkeydown = e => e.key === 'Enter' && $('join-btn').click(); |
| $('room-input').onkeydown = e => e.key === 'Enter' && $('join-btn').click(); |
| |
| function connect(username, room) { |
| if (!username) return showError('Pick a username first.'); |
| if (ws) ws.close(); |
| |
| ws = new WebSocket(`${WS_BASE}://${location.host}/ws/${encodeURIComponent(room)}`); |
| |
| ws.onopen = () => { |
| ws.send(JSON.stringify({ username })); |
| myUsername = username; |
| currentRoom = room; |
| showApp(room); |
| }; |
| |
| ws.onmessage = e => handleMessage(JSON.parse(e.data)); |
| |
| ws.onclose = () => { |
| appendSystem('π Disconnected. Refresh to reconnect.'); |
| }; |
| |
| ws.onerror = () => showError('Could not connect. Try again.'); |
| } |
| |
| function showError(msg) { |
| const el = $('join-error'); |
| el.textContent = msg; |
| el.style.display = 'block'; |
| } |
| |
| function showApp(room) { |
| $('join-screen').style.display = 'none'; |
| const app = $('app'); |
| app.classList.add('visible'); |
| $('header-room').textContent = `#${room}`; |
| $('messages').innerHTML = ''; |
| refreshRoomList(); |
| } |
| |
| function handleMessage(msg) { |
| switch (msg.kind) { |
| case 'history': |
| msg.messages.forEach(renderMsg); |
| scrollBottom(); |
| break; |
| case 'chat': |
| renderMsg(msg); |
| scrollBottom(); |
| break; |
| case 'system': |
| appendSystem(msg.text); |
| if (msg.users) updateUserList(msg.users); |
| scrollBottom(); |
| break; |
| case 'error': |
| appendSystem('β οΈ ' + msg.text); |
| break; |
| case 'pong': |
| break; |
| } |
| } |
| |
| function renderMsg(msg) { |
| if (msg.kind !== 'chat') return; |
| const isMine = msg.username === myUsername; |
| const wrap = document.createElement('div'); |
| wrap.className = `msg ${isMine ? 'mine' : 'other'}`; |
| |
| const meta = document.createElement('div'); |
| meta.className = 'msg-meta'; |
| if (!isMine) { |
| const name = document.createElement('span'); |
| name.className = 'name'; |
| name.textContent = msg.username; |
| meta.appendChild(name); |
| } |
| const ts = document.createElement('span'); |
| ts.textContent = formatTime(msg.ts); |
| meta.appendChild(ts); |
| |
| const bubble = document.createElement('div'); |
| bubble.className = 'bubble'; |
| bubble.textContent = msg.text; |
| |
| wrap.appendChild(meta); |
| wrap.appendChild(bubble); |
| $('messages').appendChild(wrap); |
| } |
| |
| function appendSystem(text) { |
| const el = document.createElement('div'); |
| el.className = 'system-msg'; |
| el.textContent = text; |
| $('messages').appendChild(el); |
| } |
| |
| function formatTime(ts) { |
| const d = new Date(ts); |
| return d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); |
| } |
| |
| function scrollBottom() { |
| const box = $('messages'); |
| box.scrollTop = box.scrollHeight; |
| } |
| |
| function sendMessage() { |
| const input = $('msg-input'); |
| const text = input.value.trim(); |
| if (!text || !ws || ws.readyState !== WebSocket.OPEN) return; |
| ws.send(JSON.stringify({ kind: 'chat', text })); |
| input.value = ''; |
| input.style.height = 'auto'; |
| } |
| |
| $('send-btn').onclick = sendMessage; |
| $('msg-input').onkeydown = e => { |
| if (e.key === 'Enter' && !e.shiftKey) { |
| e.preventDefault(); |
| sendMessage(); |
| } |
| }; |
| $('msg-input').oninput = function() { |
| this.style.height = 'auto'; |
| this.style.height = Math.min(this.scrollHeight, 120) + 'px'; |
| }; |
| |
| |
| function updateUserList(users) { |
| const list = $('user-list'); |
| list.innerHTML = ''; |
| $('user-count').textContent = `${users.length} online`; |
| users.forEach(u => { |
| const el = document.createElement('div'); |
| el.className = 'user-item'; |
| const av = document.createElement('div'); |
| av.className = 'user-avatar'; |
| av.textContent = u[0].toUpperCase(); |
| const name = document.createElement('span'); |
| name.textContent = u; |
| if (u === myUsername) name.style.color = 'var(--accent)'; |
| el.appendChild(av); |
| el.appendChild(name); |
| list.appendChild(el); |
| }); |
| } |
| |
| async function refreshRoomList() { |
| try { |
| const res = await fetch('/rooms'); |
| const rooms = await res.json(); |
| const list = $('room-list'); |
| list.innerHTML = ''; |
| rooms.forEach(r => { |
| const el = document.createElement('div'); |
| el.className = `room-item${r.name === currentRoom ? ' active' : ''}`; |
| el.textContent = `#${r.name}`; |
| el.onclick = () => switchRoom(r.name); |
| list.appendChild(el); |
| }); |
| } catch {} |
| } |
| |
| function switchRoom(room) { |
| if (room === currentRoom) return; |
| $('messages').innerHTML = ''; |
| if (ws) ws.close(); |
| connect(myUsername, room); |
| } |
| |
| $('new-room-btn').onclick = () => { |
| const name = $('new-room-input').value.trim().toLowerCase().replace(/[^a-z0-9-]/g, '-'); |
| if (!name) return; |
| $('new-room-input').value = ''; |
| switchRoom(name); |
| }; |
| $('new-room-input').onkeydown = e => e.key === 'Enter' && $('new-room-btn').click(); |
| |
| $('leave-btn').onclick = () => { |
| if (ws) ws.close(); |
| $('app').classList.remove('visible'); |
| $('join-screen').style.display = 'flex'; |
| }; |
| |
| |
| setInterval(refreshRoomList, 15000); |
| |
| |
| setInterval(() => { |
| if (ws && ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify({ kind: 'ping' })); |
| }, 25000); |
| </script> |
| </body> |
| </html> |