aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/notifications.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/webview/notifications.ts')
-rw-r--r--src/webview/notifications.ts72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/webview/notifications.ts b/src/webview/notifications.ts
new file mode 100644
index 000000000..73124b9a9
--- /dev/null
+++ b/src/webview/notifications.ts
@@ -0,0 +1,72 @@
1import { ipcRenderer } from 'electron';
2
3import { v1 as uuidV1 } from 'uuid';
4
5const debug = require('debug')('Ferdi:Notifications');
6
7export class NotificationsHandler {
8 onNotify = (data: { title: string; options: any; notificationId: string }) =>
9 data;
10
11 displayNotification(title: string, options: any) {
12 return new Promise(resolve => {
13 debug('New notification', title, options);
14
15 const notificationId = uuidV1();
16
17 ipcRenderer.sendToHost(
18 'notification',
19 this.onNotify({
20 title,
21 options,
22 notificationId,
23 }),
24 );
25
26 ipcRenderer.once(`notification-onclick:${notificationId}`, () => {
27 resolve(true);
28 });
29 });
30 }
31}
32
33export const notificationsClassDefinition = `(() => {
34 class Notification {
35 static permission = 'granted';
36
37 constructor(title = '', options = {}) {
38 this.title = title;
39 this.options = options;
40 window.ferdi.displayNotification(title, options)
41 .then(() => {
42 if (typeof (this.onClick) === 'function') {
43 this.onClick();
44 }
45 });
46 }
47
48 static requestPermission(cb = null) {
49 if (!cb) {
50 return new Promise((resolve) => {
51 resolve(Notification.permission);
52 });
53 }
54
55 if (typeof (cb) === 'function') {
56 return cb(Notification.permission);
57 }
58
59 return Notification.permission;
60 }
61
62 onNotify(data) {
63 return data;
64 }
65
66 onClick() {}
67
68 close() {}
69 }
70
71 window.Notification = Notification;
72})();`;