aboutsummaryrefslogtreecommitdiff
path: root/src/services/desktop_notification_utils/desktop_notification_utils.js
blob: bde9f18cbaefb12967a775624e776fdf4ea8e142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {
  showDesktopNotification as swDesktopNotification,
  closeDesktopNotification as swCloseDesktopNotification,
  isSWSupported
} from '../sw/sw.js'
const state = { failCreateNotif: false }

export const showDesktopNotification = (rootState, desktopNotificationOpts) => {
  if (!('Notification' in window && window.Notification.permission === 'granted')) return
  if (rootState.notifications.desktopNotificationSilence) { return }

  if (isSWSupported()) {
    swDesktopNotification(desktopNotificationOpts)
  } else if (!state.failCreateNotif) {
    try {
      const desktopNotification = new window.Notification(desktopNotificationOpts.title, desktopNotificationOpts)
      setTimeout(desktopNotification.close.bind(desktopNotification), 5000)
    } catch {
      state.failCreateNotif = true
    }
  }
}

export const closeDesktopNotification = (rootState, id) => {
  if (!('Notification' in window && window.Notification.permission === 'granted')) return

  if (isSWSupported()) {
    swCloseDesktopNotification({ id })
  }
}

export const closeAllDesktopNotifications = (rootState) => {
  if (!('Notification' in window && window.Notification.permission === 'granted')) return

  if (isSWSupported()) {
    swCloseDesktopNotification()
  }
}