/** * @file Browser notification utilities. * @copyright nhcarrigan * @license Naomi's Public License * @author Naomi Carrigan */ import { logError } from "./logError.js"; /** * Requests browser notification permission from the user. * @returns Whether permission was granted. */ const requestNotificationPermission = async(): Promise => { if (typeof Notification === "undefined") { return false; } if (Notification.permission === "granted") { return true; } if (Notification.permission === "denied") { return false; } const permission = await Notification.requestPermission(); return permission === "granted"; }; /** * Sends a browser notification if permission has been granted. * @param title - The notification title text. * @param body - The notification message displayed below the title. */ const sendNotification = (title: string, body: string): void => { if ( typeof Notification === "undefined" || Notification.permission !== "granted" ) { return; } try { // eslint-disable-next-line no-new -- Notification constructor has side effects new Notification(title, { body: body, icon: "/favicon.ico" }); } catch (error_: unknown) { logError("send_notification", error_); // Silently ignore — notifications may fail silently } }; export { requestNotificationPermission, sendNotification };