1 import noop from '@proton/utils/noop';
3 import type { ElectronNotification } from '../desktop/desktopTypes';
4 import { invokeInboxDesktopIPC } from '../desktop/ipcHelpers';
12 let notifications: Notification[] = [];
14 export const hasNotificationSupport = (): boolean => 'Notification' in window;
15 export const hasPermission = (): boolean => Notification.permission === Status.GRANTED;
16 export const hasDenied = (): boolean => Notification.permission === Status.DENIED;
18 const addNotification = (notification: Notification) => {
19 notifications.push(notification);
22 const removeNotification = (notification: Notification) => {
23 notifications = notifications.filter((n) => n !== notification);
26 const setupNotificationHandlers = (notification: Notification, onClick?: () => void) => {
27 notification.onclose = () => removeNotification(notification);
28 notification.onclick = () => {
34 export const getStatus = (): Status => {
35 if (!hasNotificationSupport()) {
36 return Status.DEFAULT;
39 if (hasPermission()) {
40 return Status.GRANTED;
47 return Status.DEFAULT;
50 export const isEnabled = (): boolean => {
51 if (hasNotificationSupport()) {
52 return hasPermission();
57 export const clear = () => {
58 notifications.forEach((notification) => notification.close());
62 export const request = async (onGranted: () => void = noop, onDenied: () => void = noop) => {
63 if (!hasNotificationSupport() || hasDenied()) {
68 if (hasPermission()) {
73 const permission = await Notification.requestPermission();
75 if (permission === Status.GRANTED) {
82 const createWebNotification = (title: string, options?: NotificationOptions, onClick?: () => void) => {
87 const notification = new Notification(title, options);
89 addNotification(notification);
91 setupNotificationHandlers(notification, onClick);
96 export const createElectronNotification = (payload: ElectronNotification) => {
97 return invokeInboxDesktopIPC({ type: 'showNotification', payload });
100 interface NotificationParams extends NotificationOptions {
101 onClick?: () => void;
104 export const create = async (title = '', params: NotificationParams = {}) => {
107 return; // Exit if permission wasn't granted
110 return createWebNotification(title, params, params.onClick);