aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/notifications.js
blob: 021f05cc3be3b79cb9cd2cd66e455e541fd01619 (plain) (blame)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
import { ipcRenderer } from 'electron';
import uuidV1 from 'uuid/v1';

const debug = require('debug')('Ferdi:Notifications');

class Notification {
  static permission = 'granted';

  constructor(title = '', options = {}) {
    debug('New notification', title, options);
    this.title = title;
    this.options = options;
    this.notificationId = uuidV1();

    ipcRenderer.sendToHost('notification', this.onNotify({
      title: this.title,
      options: this.options,
      notificationId: this.notificationId,
    }));

    ipcRenderer.once(`notification-onclick:${this.notificationId}`, () => {
      if (typeof this.onclick === 'function') {
        this.onclick();
      }
    });
  }

  static requestPermission(cb = null) {
    if (!cb) {
      return new Promise((resolve) => {
        resolve(Notification.permission);
      });
    }

    if (typeof (cb) === 'function') {
      return cb(Notification.permission);
    }

    return Notification.permission;
  }

  onNotify(data) {
    return data;
  }

  onClick() {}

  close() {}
}

window.Notification = Notification;