diff src/chat.js @ 16:82b55186a4a0

fix delete_chat
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Nov 2024 21:22:06 -0700
parents 8b8905f63d80
children 7230c821c368
line wrap: on
line diff
--- a/src/chat.js	Sun Nov 03 17:36:49 2024 -0700
+++ b/src/chat.js	Sun Nov 03 21:22:06 2024 -0700
@@ -3,11 +3,13 @@
 let currentChatId = null;
 let eventSource;
 
+function evalEvent(event) {
+	eval(event.data);
+}
+
 function setUserEventSource(userId) {
 	let userEventSource = new EventSource(`${location.origin}/user/${userId}`);
-	userEventSource.onmessage = function(event) {
-		eval(event.data);
-	};
+	userEventSource.onmessage = evalEvent;
 }
 
 function selectChat(div) {
@@ -22,9 +24,7 @@
 
 	if(eventSource)  eventSource.close();
 	eventSource = new EventSource(`${location.origin}/chat/${chatId}`);
-	eventSource.onmessage = function(event) {
-		eval(event.data);
-	};
+	eventSource.onmessage = evalEvent;
 }
 
 function gotChat(html) {
@@ -90,7 +90,12 @@
 	document.querySelector('div[chats]').innerHTML = html;
 	if( currentChatId ) {
 		let current = document.querySelector(`div[chat="${currentChatId}"]`);
-		current.setAttribute('selected','');
-		current.scrollIntoViewIfNeeded(false);
+		if( current ) {
+			current.setAttribute('selected','');
+			current.scrollIntoViewIfNeeded(false);
+		} else {
+			currentChatId = null;
+			document.querySelector('div[posts]').innerHTML = '';
+		}
 	}
 }