logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

desktop_notification_utils.js (1261B)


  1. import {
  2. showDesktopNotification as swDesktopNotification,
  3. closeDesktopNotification as swCloseDesktopNotification,
  4. isSWSupported
  5. } from '../sw/sw.js'
  6. const state = { failCreateNotif: false }
  7. export const showDesktopNotification = (rootState, desktopNotificationOpts) => {
  8. if (!('Notification' in window && window.Notification.permission === 'granted')) return
  9. if (rootState.notifications.desktopNotificationSilence) { return }
  10. if (isSWSupported()) {
  11. swDesktopNotification(desktopNotificationOpts)
  12. } else if (!state.failCreateNotif) {
  13. try {
  14. const desktopNotification = new window.Notification(desktopNotificationOpts.title, desktopNotificationOpts)
  15. setTimeout(desktopNotification.close.bind(desktopNotification), 5000)
  16. } catch {
  17. state.failCreateNotif = true
  18. }
  19. }
  20. }
  21. export const closeDesktopNotification = (rootState, { id }) => {
  22. if (!('Notification' in window && window.Notification.permission === 'granted')) return
  23. if (isSWSupported()) {
  24. swCloseDesktopNotification({ id })
  25. }
  26. }
  27. export const closeAllDesktopNotifications = (rootState) => {
  28. if (!('Notification' in window && window.Notification.permission === 'granted')) return
  29. if (isSWSupported()) {
  30. swCloseDesktopNotification({})
  31. }
  32. }