Update static/js/04-main.js
This commit is contained in:
parent
5e080893e2
commit
2cb9d0157c
@ -1,6 +1,5 @@
|
||||
// static/js/04-main.js
|
||||
"use strict";
|
||||
|
||||
window.Glow = window.Glow || {};
|
||||
|
||||
Glow.loadRooms = async () => {
|
||||
@ -27,7 +26,6 @@ Glow.loadRooms = async () => {
|
||||
null;
|
||||
|
||||
Glow.state.activeRoom = selected;
|
||||
|
||||
Glow.updateViewUI();
|
||||
Glow.updateChatControls();
|
||||
};
|
||||
@ -36,7 +34,6 @@ Glow.updateViewUI = () => {
|
||||
const view = Glow.state.activeView || "feed";
|
||||
const room = Glow.state.activeRoom;
|
||||
const dm = Glow.state.activeDm;
|
||||
|
||||
const feedActive = view === "feed";
|
||||
const dmListActive = view === "dms";
|
||||
const chatActive = view === "room" || view === "channel" || view === "dm";
|
||||
@ -53,14 +50,12 @@ Glow.updateViewUI = () => {
|
||||
Glow.els.pagesBox?.classList.toggle("hidden", !pagesActive);
|
||||
Glow.els.pageDetailBox?.classList.toggle("hidden", !pageActive);
|
||||
Glow.els.profileBox?.classList.toggle("hidden", !(profileActive || settingsActive));
|
||||
|
||||
Glow.els.composerWrap.classList.toggle("hidden", !chatActive);
|
||||
Glow.els.threadComposerWrap?.classList.toggle("hidden", !threadActive);
|
||||
|
||||
if (!chatActive) {
|
||||
Glow.state.typingUsers = [];
|
||||
Glow.state.dmTypingUsers = [];
|
||||
|
||||
if (Glow.els.typingBar) {
|
||||
Glow.els.typingBar.textContent = "";
|
||||
Glow.els.typingBar.classList.add("hidden");
|
||||
@ -90,7 +85,6 @@ Glow.updateViewUI = () => {
|
||||
Glow.els.headerSubtitle.textContent = Glow.state.currentUser
|
||||
? "Recent activity across your accessible rooms"
|
||||
: "Sign in or create an account to personalize your feed";
|
||||
|
||||
Glow.els.roomSelect.disabled = !Glow.state.rooms.length;
|
||||
Glow.els.presenceLabel?.classList.add("hidden");
|
||||
return;
|
||||
@ -108,9 +102,7 @@ Glow.updateViewUI = () => {
|
||||
if (threadActive) {
|
||||
Glow.els.headerTitle.textContent = "Thread";
|
||||
Glow.els.headerSubtitle.textContent =
|
||||
dm
|
||||
? "Direct message replies"
|
||||
: room?.name || "Conversation replies";
|
||||
dm ? "Direct message replies" : room?.name || "Conversation replies";
|
||||
Glow.updateThreadControls?.();
|
||||
return;
|
||||
}
|
||||
@ -151,7 +143,6 @@ Glow.updateViewUI = () => {
|
||||
(dm.members || []).find(
|
||||
member => Number(member.id) !== Number(Glow.state.currentUser?.id)
|
||||
) || dm.members?.[0];
|
||||
|
||||
Glow.els.headerTitle.textContent =
|
||||
peer?.display_name || peer?.username || "Direct message";
|
||||
Glow.els.headerSubtitle.textContent =
|
||||
@ -166,7 +157,6 @@ Glow.updateViewUI = () => {
|
||||
Glow.els.headerSubtitle.textContent =
|
||||
room.description ||
|
||||
(room.visibility === "private" ? "private room" : "public room");
|
||||
|
||||
Glow.els.roomSelect.value = room.slug;
|
||||
} else {
|
||||
Glow.els.headerTitle.textContent = "No rooms";
|
||||
@ -219,7 +209,6 @@ Glow.checkInvite = async () => {
|
||||
body: JSON.stringify({})
|
||||
}
|
||||
);
|
||||
|
||||
Glow.showToast(`Joined private room: ${data.room.name}`);
|
||||
window.history.replaceState({}, "", window.location.pathname);
|
||||
} catch (error) {
|
||||
@ -247,11 +236,11 @@ Glow.checkInvite = async () => {
|
||||
Glow.checkAuth = async () => {
|
||||
try {
|
||||
const data = await Glow.apiRequest("/api/me");
|
||||
|
||||
Glow.state.currentUser = data.authenticated ? data.user : null;
|
||||
Glow.state.csrfToken = data.csrf_token || "";
|
||||
|
||||
Glow.updateAuthUI();
|
||||
|
||||
await Glow.checkInvite();
|
||||
await Glow.loadRooms();
|
||||
await Glow.loadDms?.().catch(() => {});
|
||||
@ -260,6 +249,8 @@ Glow.checkAuth = async () => {
|
||||
Glow.updateViewUI();
|
||||
await Glow.loadFeed();
|
||||
await Glow.loadMySettings?.().catch(() => {});
|
||||
|
||||
Glow.calls?.scheduleInvitePolling?.();
|
||||
} catch (error) {
|
||||
Glow.showToast(error.message || "Could not connect.", "error");
|
||||
} finally {
|
||||
@ -269,7 +260,6 @@ Glow.checkAuth = async () => {
|
||||
|
||||
Glow.authenticate = async endpoint => {
|
||||
Glow.clearAuthNotice();
|
||||
|
||||
const username = Glow.els.usernameInput.value.trim().toLowerCase();
|
||||
const password = Glow.els.passwordInput.value;
|
||||
|
||||
@ -293,6 +283,7 @@ Glow.authenticate = async endpoint => {
|
||||
Glow.els.passwordInput.value = "";
|
||||
|
||||
Glow.updateAuthUI();
|
||||
|
||||
await Glow.checkInvite();
|
||||
await Glow.loadRooms();
|
||||
await Glow.loadDms?.().catch(() => {});
|
||||
@ -301,6 +292,7 @@ Glow.authenticate = async endpoint => {
|
||||
Glow.updateViewUI();
|
||||
await Glow.loadFeed();
|
||||
await Glow.loadMySettings?.().catch(() => {});
|
||||
Glow.calls?.scheduleInvitePolling?.();
|
||||
|
||||
Glow.showToast(
|
||||
endpoint.endsWith("register") ? "Account created." : "Signed in."
|
||||
@ -321,9 +313,7 @@ Glow.authenticate = async endpoint => {
|
||||
Glow.logout = async () => {
|
||||
try {
|
||||
await Glow.apiRequest("/api/logout", { method: "POST" });
|
||||
} catch (_) {
|
||||
// ignore
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
Glow.state.currentUser = null;
|
||||
Glow.state.csrfToken = "";
|
||||
@ -350,6 +340,8 @@ Glow.logout = async () => {
|
||||
Glow.closeMediaModal?.();
|
||||
Glow.endLocalCallUi?.();
|
||||
|
||||
clearTimeout(Glow.state.calls?.invitePollTimer);
|
||||
|
||||
Glow.updateAuthUI();
|
||||
|
||||
Glow.els.authOverlay.classList.remove("hidden");
|
||||
@ -369,7 +361,6 @@ Glow.toggleNotifications = async () => {
|
||||
|
||||
if (!Glow.state.notificationsEnabled) {
|
||||
const permission = await Notification.requestPermission();
|
||||
|
||||
if (permission !== "granted") {
|
||||
Glow.showToast("Notification permission was not granted.", "error");
|
||||
return;
|
||||
@ -377,12 +368,10 @@ Glow.toggleNotifications = async () => {
|
||||
}
|
||||
|
||||
Glow.state.notificationsEnabled = !Glow.state.notificationsEnabled;
|
||||
|
||||
localStorage.setItem(
|
||||
"glow_notifications_enabled",
|
||||
String(Glow.state.notificationsEnabled)
|
||||
);
|
||||
|
||||
Glow.updateAdminPreferenceButtons?.();
|
||||
Glow.showToast(
|
||||
Glow.state.notificationsEnabled
|
||||
@ -393,20 +382,16 @@ Glow.toggleNotifications = async () => {
|
||||
|
||||
Glow.toggleTypingIndicators = () => {
|
||||
Glow.state.typingIndicatorsEnabled = !Glow.state.typingIndicatorsEnabled;
|
||||
|
||||
localStorage.setItem(
|
||||
"glow_typing_enabled",
|
||||
String(Glow.state.typingIndicatorsEnabled)
|
||||
);
|
||||
|
||||
if (!Glow.state.typingIndicatorsEnabled) {
|
||||
Glow.state.typingUsers = [];
|
||||
Glow.state.dmTypingUsers = [];
|
||||
Glow.renderTyping();
|
||||
}
|
||||
|
||||
Glow.updateAdminPreferenceButtons?.();
|
||||
|
||||
Glow.showToast(
|
||||
Glow.state.typingIndicatorsEnabled
|
||||
? "Typing indicators enabled."
|
||||
@ -431,9 +416,7 @@ Glow.pingPresence = async () => {
|
||||
|
||||
try {
|
||||
await Glow.apiPingPresence(payload);
|
||||
} catch (_) {
|
||||
// non-blocking
|
||||
}
|
||||
} catch (_) {}
|
||||
};
|
||||
|
||||
Glow.refreshPresence = async () => {
|
||||
@ -441,8 +424,7 @@ Glow.refreshPresence = async () => {
|
||||
if (Glow.state.activeDm?.id) {
|
||||
const data = await Glow.apiGetDmPresence(Glow.state.activeDm.id);
|
||||
Glow.state.presence.dmOnlineCount = Number(data.online_count) || 0;
|
||||
Glow.els.presenceLabel.textContent =
|
||||
`${Glow.state.presence.dmOnlineCount} online`;
|
||||
Glow.els.presenceLabel.textContent = `${Glow.state.presence.dmOnlineCount} online`;
|
||||
Glow.els.presenceLabel.classList.remove("hidden");
|
||||
return;
|
||||
}
|
||||
@ -453,8 +435,7 @@ Glow.refreshPresence = async () => {
|
||||
Glow.state.activeChannel.slug
|
||||
);
|
||||
Glow.state.presence.channelOnlineCount = Number(data.online_count) || 0;
|
||||
Glow.els.presenceLabel.textContent =
|
||||
`${Glow.state.presence.channelOnlineCount} online`;
|
||||
Glow.els.presenceLabel.textContent = `${Glow.state.presence.channelOnlineCount} online`;
|
||||
Glow.els.presenceLabel.classList.remove("hidden");
|
||||
return;
|
||||
}
|
||||
@ -462,8 +443,7 @@ Glow.refreshPresence = async () => {
|
||||
if (Glow.state.activeRoom?.slug) {
|
||||
const data = await Glow.apiGetRoomPresence(Glow.state.activeRoom.slug);
|
||||
Glow.state.presence.roomOnlineCount = Number(data.online_count) || 0;
|
||||
Glow.els.presenceLabel.textContent =
|
||||
`${Glow.state.presence.roomOnlineCount} online`;
|
||||
Glow.els.presenceLabel.textContent = `${Glow.state.presence.roomOnlineCount} online`;
|
||||
Glow.els.presenceLabel.classList.remove("hidden");
|
||||
return;
|
||||
}
|
||||
@ -476,10 +456,7 @@ Glow.refreshPresence = async () => {
|
||||
|
||||
Glow.schedulePoll = () => {
|
||||
clearTimeout(Glow.state.pollTimer);
|
||||
|
||||
if (Glow.els.blockedOverlay.classList.contains("hidden") === false) {
|
||||
return;
|
||||
}
|
||||
if (Glow.els.blockedOverlay.classList.contains("hidden") === false) return;
|
||||
|
||||
Glow.state.pollTimer = setTimeout(async () => {
|
||||
if (Glow.state.activeView === "feed") {
|
||||
@ -500,26 +477,19 @@ Glow.schedulePoll = () => {
|
||||
|
||||
await Glow.pingPresence?.();
|
||||
await Glow.refreshPresence?.();
|
||||
|
||||
Glow.schedulePoll();
|
||||
}, document.hidden ? 12000 : 5000);
|
||||
};
|
||||
|
||||
Glow.els.chatForm.addEventListener("submit", Glow.sendMessage);
|
||||
|
||||
Glow.els.threadForm?.addEventListener("submit", event =>
|
||||
Glow.sendThreadReply?.(event)
|
||||
);
|
||||
Glow.els.threadForm?.addEventListener("submit", event => Glow.sendThreadReply?.(event));
|
||||
|
||||
Glow.els.messageInput.addEventListener("input", () => {
|
||||
Glow.autoResizeComposer();
|
||||
Glow.updateMessageCount();
|
||||
|
||||
clearTimeout(Glow.state.typingTimer);
|
||||
|
||||
if (Glow.els.messageInput.value.trim()) {
|
||||
Glow.sendTyping(true);
|
||||
|
||||
Glow.state.typingTimer = setTimeout(() => {
|
||||
Glow.sendTyping(false);
|
||||
}, 2500);
|
||||
@ -549,12 +519,10 @@ Glow.els.threadInput?.addEventListener("keydown", event => {
|
||||
|
||||
Glow.els.attachmentInput.addEventListener("change", () => {
|
||||
const files = Glow.normalizeSelectedFiles(Glow.els.attachmentInput.files);
|
||||
|
||||
if (!files.length) {
|
||||
Glow.setSelectedAttachment([]);
|
||||
return;
|
||||
}
|
||||
|
||||
const invalid = files.find(file => file.size > Glow.MAX_ATTACHMENT_BYTES);
|
||||
if (invalid) {
|
||||
Glow.showToast("Attachments must be 5 MB or smaller.", "error");
|
||||
@ -562,18 +530,15 @@ Glow.els.attachmentInput.addEventListener("change", () => {
|
||||
Glow.setSelectedAttachment([]);
|
||||
return;
|
||||
}
|
||||
|
||||
Glow.setSelectedAttachment(files);
|
||||
});
|
||||
|
||||
Glow.els.threadAttachmentInput?.addEventListener("change", () => {
|
||||
const files = Glow.normalizeSelectedFiles(Glow.els.threadAttachmentInput.files);
|
||||
|
||||
if (!files.length) {
|
||||
Glow.setSelectedThreadAttachment?.([]);
|
||||
return;
|
||||
}
|
||||
|
||||
const invalid = files.find(file => file.size > Glow.MAX_ATTACHMENT_BYTES);
|
||||
if (invalid) {
|
||||
Glow.showToast("Attachments must be 5 MB or smaller.", "error");
|
||||
@ -581,7 +546,6 @@ Glow.els.threadAttachmentInput?.addEventListener("change", () => {
|
||||
Glow.setSelectedThreadAttachment?.([]);
|
||||
return;
|
||||
}
|
||||
|
||||
Glow.setSelectedThreadAttachment?.(files);
|
||||
});
|
||||
|
||||
@ -633,18 +597,16 @@ Glow.els.roomSelect.addEventListener("change", async () => {
|
||||
const selected = Glow.state.rooms.find(
|
||||
room => room.slug === Glow.els.roomSelect.value
|
||||
);
|
||||
|
||||
if (!selected) return;
|
||||
|
||||
Glow.state.activeDm = null;
|
||||
Glow.state.activeRoom = selected;
|
||||
Glow.state.activeView = "room";
|
||||
|
||||
Glow.state.typingUsers = [];
|
||||
Glow.state.dmTypingUsers = [];
|
||||
Glow.renderTyping();
|
||||
|
||||
Glow.updateViewUI();
|
||||
|
||||
await Glow.loadMessages();
|
||||
Glow.pollTyping();
|
||||
Glow.pingPresence?.();
|
||||
@ -654,7 +616,6 @@ Glow.els.roomSelect.addEventListener("change", async () => {
|
||||
|
||||
Glow.els.teamSelect?.addEventListener("change", () => {
|
||||
const teams = Glow.state.teams || [];
|
||||
|
||||
Glow.state.activeTeam =
|
||||
teams.find(team => team.slug === Glow.els.teamSelect.value) || null;
|
||||
});
|
||||
@ -776,6 +737,7 @@ document.addEventListener("keydown", event => {
|
||||
|
||||
document.addEventListener("visibilitychange", () => {
|
||||
Glow.schedulePoll();
|
||||
Glow.calls?.scheduleInvitePolling?.();
|
||||
|
||||
if (!document.hidden) {
|
||||
Glow.resetUnreadMessages();
|
||||
@ -824,6 +786,7 @@ window.addEventListener("online", () => {
|
||||
|
||||
Glow.pingPresence?.();
|
||||
Glow.refreshPresence?.();
|
||||
Glow.calls?.scheduleInvitePolling?.();
|
||||
});
|
||||
|
||||
window.addEventListener("offline", () => {
|
||||
@ -856,4 +819,4 @@ Glow.autoResizeComposer();
|
||||
Glow.updateMessageCount();
|
||||
Glow.autoResizeThreadComposer?.();
|
||||
Glow.updateThreadMessageCount?.();
|
||||
Glow.checkAuth();
|
||||
Glow.checkAuth();
|
||||
Loading…
x
Reference in New Issue
Block a user