improvements
All checks were successful
/ upload (release) Successful in 19s

This commit is contained in:
pavel 2026-02-13 20:10:52 +01:00
commit a4bb3cfcca
7 changed files with 556 additions and 364 deletions

View file

@ -23,6 +23,7 @@ const state = {
voicePresencePollId: null,
chatWs: null,
lastMessageId: null,
onlineUsers: new Set(),
};
const el = {
@ -141,6 +142,14 @@ function formatDate(isoString) {
}
let audioCtx = null;
// Global interaction listener to unlock AudioContext (autoplay policy)
window.addEventListener('click', () => {
if (audioCtx && audioCtx.state === 'suspended') {
audioCtx.resume();
}
}, { once: true });
function playSound(type) {
try {
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
@ -274,9 +283,16 @@ function renderChannels() {
function renderDMs() {
el.dmList.innerHTML = "";
for (const dm of state.dmConversations) {
const isOnline = state.onlineUsers.has(dm.user_id);
const row = document.createElement("button");
row.className = `channel-row ${state.selectedDmUserId === dm.user_id ? "active" : ""}`;
row.innerHTML = `<i data-lucide="message-circle"></i> <span>${escapeHtml(dm.display_name)}</span>`;
row.innerHTML = `
<div class="avatar-wrapper">
<i data-lucide="message-circle"></i>
<div class="status-dot ${isOnline ? "online" : ""}"></div>
</div>
<span>${escapeHtml(dm.display_name)}</span>
`;
row.onclick = async () => {
state.selectedDmUserId = dm.user_id;
state.selectedDmDisplayName = dm.display_name;
@ -329,11 +345,7 @@ function renderMessages(messages) {
}
if (messages.length > 0) {
const latest = messages[0];
if (state.lastMessageId && latest.id !== state.lastMessageId && latest.author_user_id !== state.me?.id) {
playSound(state.selectedDmUserId ? 'dm' : 'message');
}
state.lastMessageId = latest.id;
state.lastMessageId = messages[0].id;
}
el.messageList.scrollTop = el.messageList.scrollHeight;
@ -342,10 +354,14 @@ function renderMessages(messages) {
function renderMembers() {
el.memberList.innerHTML = "";
for (const m of state.members) {
const isOnline = state.onlineUsers.has(m.id);
const row = document.createElement("div");
row.className = "member-row";
row.innerHTML = `
<div class="member-avatar">${shortName(m.display_name)}</div>
<div class="member-avatar">
${shortName(m.display_name)}
<div class="status-dot ${isOnline ? "online" : ""}"></div>
</div>
<div class="member-name">${escapeHtml(m.display_name)}</div>
`;
row.style.cursor = "pointer";
@ -476,21 +492,31 @@ function initChatWs() {
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
const authorId = msg.message?.author_user_id;
const isFromMe = authorId && state.me && authorId === state.me.id;
if (msg.type === "message_created") {
if (!isFromMe) playSound('message');
if (state.selectedTextChannelId === msg.channel_id) {
// Optimization: we could just push the message, but refresh for now
api(`/channels/${state.selectedTextChannelId}/messages?limit=100`).then(renderMessages);
} else {
// Even if not active, play sound
playSound('message');
}
} else if (msg.type === "dm_created") {
if (!isFromMe) playSound('dm');
if (state.selectedDmUserId === msg.other_user_id) {
api(`/dms/${state.selectedDmUserId}/messages?limit=100`).then(renderMessages);
} else {
playSound('dm');
loadDMConversations().then(renderDMs);
} else {
loadDMConversations().then(renderDMs);
}
} else if (msg.type === "user_presence") {
if (msg.online) {
state.onlineUsers.add(msg.user_id);
} else {
state.onlineUsers.delete(msg.user_id);
}
renderMembers();
renderDMs();
}
};
@ -918,6 +944,11 @@ async function init() {
await loadGuilds();
await loadDMConversations();
try {
const online = await api("/presence");
state.onlineUsers = new Set(online);
} catch (err) { console.warn("presence sync failed", err); }
if (state.guilds.length > 0) {
const lastGuildId = localStorage.getItem(LAST_GUILD_STORAGE_KEY);
const guild = state.guilds.find((g) => g.id === lastGuildId) || state.guilds[0];