This commit is contained in:
Radon 2025-01-21 12:25:04 -06:00
parent d33bd49c38
commit 4aa4702d27
3 changed files with 69 additions and 43 deletions

View File

@ -50,9 +50,9 @@ async function deleteMessage(messageId) {
} }
async function updateMessagesInPlace() { async function updateMessagesInPlace() {
const currenteScrollLocation = getScrollLocation(); lastMessageCount = await getMessageCount();
await loadMessages(true); const currentScrollLocation = getScrollLocation();
setScrollLocation(currenteScrollLocation); loadMessages(true, currentScrollLocation);
} }
function getScrollLocation() { function getScrollLocation() {
@ -130,46 +130,56 @@ async function loadUsers() {
} }
} }
let lastMessageCount = 0; async function getMessageCount() {
async function loadMessages(forceUpdate = false) {
try { try {
let messagesDiv = document.getElementById("messages"); const response = await fetch("/messages/length");
const response = await fetch("/messages"); const text = await response.json();
const text = await response.text(); return text.length;
const tempDiv = document.createElement("div"); } catch (error) {
tempDiv.innerHTML = text; console.error("Error getting message count:", error);
}
}
// getting an error right here that messagesDiv is null let lastMessageCount = 0;
// but only on the first load, so maybe we can just hold off? async function loadMessages(forceUpdate = false, scrollLocation) {
while (messagesDiv === null) { try {
await new Promise((resolve) => const newMessageCount = await getMessageCount();
setTimeout(resolve, 100)
);
messagesDiv = document.getElementById("messages");
}
if (messagesDiv.scrollTop != bottom) {
// show a button to scroll to the bottom
const scrollToBottomButton = document.getElementById(
"scroll",
);
scrollToBottomButton.style.display = "block";
scrollToBottomButton.onclick = scrollToBottom;
} else {
// hide the button
const scrollToBottomButton = document.getElementById(
"scroll",
);
scrollToBottomButton.style.display = "none";
}
const messages = tempDiv.getElementsByTagName("p");
const newMessageCount = messages.length;
const update = newMessageCount != lastMessageCount || const update = newMessageCount != lastMessageCount ||
lastMessageCount === 0 || forceUpdate; lastMessageCount === 0 || forceUpdate;
let messagesDiv = document.getElementById("messages");
while (messagesDiv === null) {
await new Promise((resolve) =>
setTimeout(resolve, 100)
);
messagesDiv = document.getElementById(
"messages",
);
}
if (messagesDiv.scrollTop != bottom) {
// show a button to scroll to the bottom
const scrollToBottomButton = document
.getElementById(
"scroll",
);
scrollToBottomButton.style.display = "block";
scrollToBottomButton.onclick = scrollToBottom;
} else {
// hide the button
const scrollToBottomButton = document
.getElementById(
"scroll",
);
scrollToBottomButton.style.display = "none";
}
if (update) { if (update) {
const response = await fetch("/messages");
const text = await response.text();
const tempDiv = document.createElement("div");
tempDiv.innerHTML = text;
const messages = tempDiv.getElementsByTagName("p");
messagesDiv.innerHTML = ""; messagesDiv.innerHTML = "";
Array.from(messages).forEach((msg) => { Array.from(messages).forEach((msg) => {
const messageDiv = document.createElement( const messageDiv = document.createElement(
@ -214,7 +224,11 @@ async function loadMessages(forceUpdate = false) {
messagesDiv.appendChild(messageDiv); messagesDiv.appendChild(messageDiv);
}); });
scrollToBottom(); if (scrollLocation !== undefined) {
setScrollLocation(scrollLocation);
} else {
scrollToBottom();
}
lastMessageCount = newMessageCount; lastMessageCount = newMessageCount;
} }
} catch (error) { } catch (error) {
@ -270,8 +284,6 @@ function getYouTubeID(url) {
return (match && match[7].length == 11) ? match[7] : false; return (match && match[7].length == 11) ? match[7] : false;
} }
var bottom = 0;
function scrollToBottom() { function scrollToBottom() {
const messagesDiv = document.getElementById("messages"); const messagesDiv = document.getElementById("messages");
messagesDiv.scrollTop = messagesDiv.scrollHeight; messagesDiv.scrollTop = messagesDiv.scrollHeight;
@ -465,6 +477,7 @@ document.addEventListener("keyup", function (event) {
} }
}); });
let bottom = 0;
async function initialize() { async function initialize() {
usersPanel = document.getElementById("users-panel"); usersPanel = document.getElementById("users-panel");
if (usersPanel) { if (usersPanel) {
@ -475,14 +488,13 @@ async function initialize() {
settingsPanel.style.display = "none"; settingsPanel.style.display = "none";
} }
initializeTheme(); initializeTheme();
checkUsername(); await checkUsername();
updateCurrentUser(); await updateCurrentUser();
timeZoneCheck(); await timeZoneCheck();
setInterval(loadMessages, 1000); setInterval(loadMessages, 1000);
setInterval(loadUsers, 1000); setInterval(loadUsers, 1000);
setInterval(pingCheck, 3000); setInterval(pingCheck, 3000);
await loadMessages(true); await loadMessages(true);
scrollToBottom();
} }
initialize(); initialize();

BIN
main Normal file

Binary file not shown.

14
main.go
View File

@ -656,6 +656,19 @@ func (s *Server) handleCss(w http.ResponseWriter, r *http.Request) {
w.Write(file) w.Write(file)
} }
func (s *Server) handleMessagesLength(w http.ResponseWriter, r *http.Request) {
// should return the number of messages in the database
if r.Method != http.MethodGet {
http.Error(w, `{"error": "Method not allowed"}`, http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
messages := s.Database.MessagesGet()
json.NewEncoder(w).Encode(map[string]int{
"length": len(messages),
})
}
func (s *Server) Run() { func (s *Server) Run() {
s.Database.DbCreateTableMessages() s.Database.DbCreateTableMessages()
s.Database.DbCreateTableUsers() s.Database.DbCreateTableUsers()
@ -664,6 +677,7 @@ func (s *Server) Run() {
handler.HandleFunc("/ping", s.handlePing) handler.HandleFunc("/ping", s.handlePing)
handler.HandleFunc("/username", s.handleUsername) handler.HandleFunc("/username", s.handleUsername)
handler.HandleFunc("/messages", s.handleMessages) handler.HandleFunc("/messages", s.handleMessages)
handler.HandleFunc("/messages/length", s.handleMessagesLength)
handler.HandleFunc("/username/status", s.handleUsernameStatus) handler.HandleFunc("/username/status", s.handleUsernameStatus)
handler.HandleFunc("/users", s.handleUsers) handler.HandleFunc("/users", s.handleUsers)
handler.HandleFunc("/", s.handleRoot) handler.HandleFunc("/", s.handleRoot)