add notification for new post in thread

This commit is contained in:
2025-05-28 04:01:51 +03:00
parent 8ea9afd39d
commit 1a96612544
10 changed files with 195 additions and 12 deletions

View File

@ -35,4 +35,49 @@
form.action = `/post/${postId}/delete`
})
}
let newPostSubscription = null;
function hideNotification() {
const notification = document.getElementById('new-post-notification');
notification.classList.add('hidden');
}
function showNewPostNotification(url) {
const notification = document.getElementById("new-post-notification");
notification.classList.remove("hidden");
document.getElementById("dismiss-new-post-button").onclick = () => {
hideNotification();
reconnectSSE();
}
document.getElementById("go-to-new-post-button").href = url;
document.getElementById("unsub-new-post-button").onclick = () => {
hideNotification()
}
}
function reconnectSSE() {
if (newPostSubscription) newPostSubscription.close();
const threadEndpoint = document.getElementById("thread-subscribe-endpoint").value;
newPostSubscription = new EventSource(threadEndpoint);
newPostSubscription.onerror = (e) => {
console.error(e);
};
newPostSubscription.addEventListener("new_post_url", (e) => {
showNewPostNotification(e.data);
newPostSubscription.close();
})
}
window.addEventListener('beforeunload', () => {
if(newPostSubscription)
{
newPostSubscription.close();
}
});
reconnectSSE();
}