Fix TUI screen wrapping, channel selection/creation, and online members presence

This commit is contained in:
tyuXX 2026-10-11 17:21:54 +00:00
parent cbc24e7672
commit 25696db42c
12 changed files with 582 additions and 109 deletions

View file

@ -33,6 +33,11 @@
sendBtn: document.getElementById('sendBtn'),
fileInput: document.getElementById('fileInput'),
welcomeMotd: document.getElementById('welcomeMotd'),
toggleMembersBtn: document.getElementById('toggleMembersBtn'),
chatMembersSidebar: document.getElementById('chatMembersSidebar'),
chatOnlineUsersList: document.getElementById('chatOnlineUsersList'),
chatOnlineCountBadge: document.getElementById('chatOnlineCountBadge'),
chatHeaderOnlineCount: document.getElementById('chatHeaderOnlineCount'),
// Channels View
channelList: document.getElementById('channelList'),
@ -70,10 +75,20 @@
dropOverlay: document.getElementById('dropOverlay')
};
function initElements() {
for (const key in el) {
if (!el[key] && typeof key === 'string') {
const found = document.getElementById(key);
if (found) el[key] = found;
}
}
}
// ==========================================
// Initialization
// ==========================================
async function init() {
initElements();
setupEventListeners();
await fetchServerInfo();
@ -323,6 +338,19 @@
el.tabViews.forEach(view => {
view.classList.toggle('active', view.id === `view-${tabName}`);
});
if (tabName === 'users') {
renderUsers();
if (state.socket && state.socket.readyState === WebSocket.OPEN) {
sendEnvelope('USER_LIST_SYNC', {});
}
} else if (tabName === 'channels') {
renderChannels();
} else if (tabName === 'files') {
renderFiles();
} else if (tabName === 'chat') {
renderMessages();
}
}
function updateActiveChannelHeader() {
@ -331,7 +359,7 @@
const chId = active.id || active.Id;
const chName = active.name || active.Name || 'general';
const chTopic = active.topic || active.Topic || '';
if (el.channelSelectDropdown) {
if (el.channelSelectDropdown && el.channelSelectDropdown.value !== chId) {
el.channelSelectDropdown.value = chId;
}
el.activeChannelTopic.textContent = chTopic ? ` - ${chTopic}` : '';
@ -340,50 +368,66 @@
}
function renderChannels() {
el.channelList.innerHTML = '';
if (el.channelSelectDropdown) {
el.channelSelectDropdown.innerHTML = '';
if (el.channelList) {
el.channelList.innerHTML = '';
state.channels.forEach(ch => {
const chId = ch.id || ch.Id;
const chName = ch.name || ch.Name;
const chTopic = ch.topic || ch.Topic || '';
const isActive = chId === state.activeChannelId;
// Channels View list item
const item = document.createElement('div');
item.className = 'channel-item' + (isActive ? ' active' : '');
item.innerHTML = `
<div class="channel-meta">
<span class="channel-title">#${escapeHtml(chName)}</span>
<span class="channel-desc">${escapeHtml(chTopic)}</span>
</div>
${isActive ? '<span class="channel-badge">ACTIVE</span>' : ''}
`;
item.addEventListener('click', () => {
joinChannel(chId);
switchTab('chat');
});
el.channelList.appendChild(item);
});
}
state.channels.forEach(ch => {
const chId = ch.id || ch.Id;
const chName = ch.name || ch.Name;
const chTopic = ch.topic || ch.Topic || '';
const isActive = chId === state.activeChannelId;
// In-chat dropdown option: only rebuild if list changed
if (el.channelSelectDropdown) {
const currentOpts = Array.from(el.channelSelectDropdown.options).map(o => o.value);
const newIds = state.channels.map(c => c.id || c.Id);
const needsRebuild = currentOpts.length !== newIds.length || !newIds.every((id, idx) => id === currentOpts[idx]);
// Channels View list item
const item = document.createElement('div');
item.className = 'channel-item' + (isActive ? ' active' : '');
item.innerHTML = `
<div class="channel-meta">
<span class="channel-title">#${escapeHtml(chName)}</span>
<span class="channel-desc">${escapeHtml(chTopic)}</span>
</div>
${isActive ? '<span class="channel-badge">ACTIVE</span>' : ''}
`;
item.addEventListener('click', () => {
joinChannel(chId);
switchTab('chat');
});
el.channelList.appendChild(item);
// In-chat dropdown option
if (el.channelSelectDropdown) {
const opt = document.createElement('option');
opt.value = chId;
opt.textContent = `#${chName}`;
opt.selected = isActive;
el.channelSelectDropdown.appendChild(opt);
if (needsRebuild) {
el.channelSelectDropdown.innerHTML = '';
state.channels.forEach(ch => {
const chId = ch.id || ch.Id;
const chName = ch.name || ch.Name;
const opt = document.createElement('option');
opt.value = chId;
opt.textContent = `#${chName}`;
if (chId === state.activeChannelId) opt.selected = true;
el.channelSelectDropdown.appendChild(opt);
});
}
});
if (state.activeChannelId) {
el.channelSelectDropdown.value = state.activeChannelId;
}
}
// Show create button for Admin, Owner, or if allowed for members
const isPrivileged = state.user && (state.user.role === 'Owner' || state.user.role === 'Admin');
const allowMemberChannels = state.serverInfo && state.serverInfo.allowMemberChannelCreation;
el.openCreateChannelBtn.style.display = (isPrivileged || allowMemberChannels) ? 'inline-flex' : 'none';
if (el.openCreateChannelBtn) {
el.openCreateChannelBtn.style.display = (isPrivileged || allowMemberChannels) ? 'inline-flex' : 'none';
}
}
function joinChannel(channelId) {
if (!channelId) return;
state.activeChannelId = channelId;
sendEnvelope('JOIN_CHANNEL', { channelId });
renderChannels();
@ -499,27 +543,104 @@
el.chatMessages.scrollTop = el.chatMessages.scrollHeight;
}
function renderUsers() {
el.userList.innerHTML = '';
const onlineCount = state.users.filter(u => u.isOnline ?? u.IsOnline).length;
el.userCountBadge.textContent = `${onlineCount} Online / ${state.users.length} Total`;
function checkIsOnline(u) {
if (!u) return false;
const val = u.isOnline !== undefined ? u.isOnline : u.IsOnline;
return val === true || val === 'true' || val === 1 || val === '1';
}
state.users.forEach(u => {
const item = document.createElement('div');
item.className = 'user-item';
const username = u.username || u.Username || 'User';
const role = u.role || u.Role || 'Member';
const roleClass = role.toLowerCase();
const isOnline = u.isOnline ?? u.IsOnline ?? false;
item.innerHTML = `
<div class="user-meta">
<span class="user-dot ${isOnline ? 'online' : ''}"></span>
<span style="font-weight:600;">@${escapeHtml(username)}</span>
</div>
<span class="role-badge role-${roleClass}">${escapeHtml(role)}</span>
`;
el.userList.appendChild(item);
});
function renderUsers() {
initElements();
const onlineUsers = state.users.filter(checkIsOnline);
const onlineCount = onlineUsers.length;
const totalCount = state.users.length;
// 1. Update in-chat members sidebar and header badge
if (el.chatOnlineCountBadge) {
el.chatOnlineCountBadge.textContent = onlineCount;
}
if (el.chatHeaderOnlineCount) {
el.chatHeaderOnlineCount.textContent = onlineCount;
}
if (el.chatOnlineUsersList) {
el.chatOnlineUsersList.innerHTML = '';
if (onlineUsers.length === 0) {
el.chatOnlineUsersList.innerHTML = '<div style="color:var(--text-muted); font-size:0.8rem; padding:12px 6px; text-align:center;">No members currently online</div>';
} else {
const roleOrder = { owner: 1, admin: 2, member: 3 };
const sortedOnline = [...onlineUsers].sort((a, b) => {
const aRole = (a.role || a.Role || 'member').toLowerCase();
const bRole = (b.role || b.Role || 'member').toLowerCase();
const orderDiff = (roleOrder[aRole] || 99) - (roleOrder[bRole] || 99);
if (orderDiff !== 0) return orderDiff;
const aName = (a.username || a.Username || '').toLowerCase();
const bName = (b.username || b.Username || '').toLowerCase();
return aName.localeCompare(bName);
});
sortedOnline.forEach(u => {
const item = document.createElement('div');
item.className = 'chat-member-item';
const username = u.username || u.Username || 'User';
const role = u.role || u.Role || 'Member';
const roleClass = role.toLowerCase();
item.innerHTML = `
<div class="chat-member-meta">
<span class="user-dot online"></span>
<span class="chat-member-name" title="@${escapeHtml(username)}">@${escapeHtml(username)}</span>
</div>
<span class="role-badge role-${roleClass}" style="font-size:0.65rem; padding:1px 6px;">${escapeHtml(role)}</span>
`;
el.chatOnlineUsersList.appendChild(item);
});
}
}
// 2. Update dedicated Community Members tab (#view-users)
if (el.userCountBadge) {
el.userCountBadge.textContent = `${onlineCount} Online / ${totalCount} Total`;
}
if (el.userList) {
el.userList.innerHTML = '';
if (state.users.length === 0) {
el.userList.innerHTML = '<div style="color:var(--text-muted); text-align:center; padding:20px;">No registered members found.</div>';
return;
}
const roleOrder = { owner: 1, admin: 2, member: 3 };
const sortedAll = [...state.users].sort((a, b) => {
const aOnline = checkIsOnline(a) ? 1 : 0;
const bOnline = checkIsOnline(b) ? 1 : 0;
if (bOnline !== aOnline) return bOnline - aOnline;
const aRole = (a.role || a.Role || 'member').toLowerCase();
const bRole = (b.role || b.Role || 'member').toLowerCase();
const orderDiff = (roleOrder[aRole] || 99) - (roleOrder[bRole] || 99);
if (orderDiff !== 0) return orderDiff;
const aName = (a.username || a.Username || '').toLowerCase();
const bName = (b.username || b.Username || '').toLowerCase();
return aName.localeCompare(bName);
});
sortedAll.forEach(u => {
const item = document.createElement('div');
item.className = 'user-item';
const username = u.username || u.Username || 'User';
const role = u.role || u.Role || 'Member';
const roleClass = role.toLowerCase();
const isOnline = checkIsOnline(u);
item.innerHTML = `
<div class="user-meta">
<span class="user-dot ${isOnline ? 'online' : ''}"></span>
<span style="font-weight:600;">@${escapeHtml(username)}</span>
</div>
<span class="role-badge role-${roleClass}">${escapeHtml(role)}</span>
`;
el.userList.appendChild(item);
});
}
}
function renderFiles() {
@ -616,9 +737,17 @@
const args = parts.slice(1).join(' ').trim();
if (cmd === 'join') {
const ch = state.channels.find(c => c.name.toLowerCase() === args.replace('#', '').toLowerCase());
if (ch) joinChannel(ch.id);
else showToast(`Channel ${args} not found`);
const target = args.replace('#', '').toLowerCase();
const ch = state.channels.find(c => {
const n = (c.name || c.Name || '').toLowerCase();
const id = (c.id || c.Id || '').toLowerCase();
return n === target || id === target;
});
if (ch) {
joinChannel(ch.id || ch.Id);
} else {
showToast(`Channel #${args} not found`);
}
return;
}
@ -641,9 +770,10 @@
return;
}
sendEnvelope('CREATE_CHANNEL', { name, topic: topic || '' });
el.createChannelCard.style.display = 'none';
el.newChannelName.value = '';
el.newChannelTopic.value = '';
if (el.createChannelCard) el.createChannelCard.style.display = 'none';
if (el.newChannelName) el.newChannelName.value = '';
if (el.newChannelTopic) el.newChannelTopic.value = '';
switchTab('chat');
}
async function uploadFile(file) {
@ -745,18 +875,49 @@
});
// Channels Create Form
el.openCreateChannelBtn.addEventListener('click', () => {
el.createChannelCard.style.display = 'block';
el.newChannelName.focus();
});
if (el.openCreateChannelBtn) {
el.openCreateChannelBtn.addEventListener('click', () => {
if (el.createChannelCard) el.createChannelCard.style.display = 'block';
if (el.newChannelName) el.newChannelName.focus();
});
}
el.cancelCreateChannelBtn.addEventListener('click', () => {
el.createChannelCard.style.display = 'none';
});
if (el.cancelCreateChannelBtn) {
el.cancelCreateChannelBtn.addEventListener('click', () => {
if (el.createChannelCard) el.createChannelCard.style.display = 'none';
});
}
el.submitCreateChannelBtn.addEventListener('click', () => {
createChannel(el.newChannelName.value.trim(), el.newChannelTopic.value.trim());
});
if (el.submitCreateChannelBtn) {
el.submitCreateChannelBtn.addEventListener('click', () => {
createChannel(el.newChannelName.value.trim(), el.newChannelTopic.value.trim());
});
}
if (el.newChannelName) {
el.newChannelName.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
createChannel(el.newChannelName.value.trim(), el.newChannelTopic.value.trim());
}
});
}
if (el.newChannelTopic) {
el.newChannelTopic.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
createChannel(el.newChannelName.value.trim(), el.newChannelTopic.value.trim());
}
});
}
// Toggle In-Chat Online Members Sidebar
if (el.toggleMembersBtn && el.chatMembersSidebar) {
el.toggleMembersBtn.addEventListener('click', () => {
el.chatMembersSidebar.classList.toggle('closed');
});
}
// In-chat Channel Selector Dropdown
if (el.channelSelectDropdown) {