Files
coms/client/public/notifications.js
2026-03-11 01:09:12 -04:00

60 lines
1.5 KiB
JavaScript

// Simple notification helpers for the chat client.
const APP_NAME = "COMS";
function supportsNotifications() {
return typeof Notification !== "undefined";
}
function stripHtml(input) {
const div = document.createElement("div");
div.innerHTML = input;
return div.textContent || div.innerText || "";
}
/**
* Ask for permission if not already granted/denied.
*/
function primeNotifications() {
if (!supportsNotifications()) return;
if (Notification.permission === "default") {
Notification.requestPermission();
}
}
function showNotification(title, body) {
if (!supportsNotifications()) return;
const options = { body };
if (Notification.permission === "granted") {
new Notification(title, options);
} else if (Notification.permission === "default") {
Notification.requestPermission().then((perm) => {
if (perm === "granted") new Notification(title, options);
});
}
}
/**
* Notify the user of a system notice.
* @param {string} content - HTML or text content of the notice.
*/
function notifyNotice(content) {
const text = stripHtml(content);
if (!text) return;
showNotification(`${APP_NAME} notice`, text);
}
/**
* Notify the user that someone replied directly to their thread.
* @param {string} authorName
* @param {string} content
*/
function notifyDirectReply(authorName, content) {
const text = stripHtml(content);
if (!text) return;
const name = authorName || "Anon";
showNotification(`${name} replied to your thread`, text);
}
export { primeNotifications, notifyNotice, notifyDirectReply };