diff options
author | Markus Hatvan <markus_hatvan@aon.at> | 2021-10-14 09:32:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-14 13:02:20 +0530 |
commit | 52211095aab71f8b59b093b19ae34c222be9f390 (patch) | |
tree | 28f24a9be5ee5577667cdd396fb3fc64fc861e65 /src/webview/notifications.ts | |
parent | chore: convert class components to functional (#2065) (diff) | |
download | ferdium-app-52211095aab71f8b59b093b19ae34c222be9f390.tar.gz ferdium-app-52211095aab71f8b59b093b19ae34c222be9f390.tar.zst ferdium-app-52211095aab71f8b59b093b19ae34c222be9f390.zip |
chore: convert various JS to TS (#2062)
Diffstat (limited to 'src/webview/notifications.ts')
-rw-r--r-- | src/webview/notifications.ts | 72 |
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 @@ | |||
1 | import { ipcRenderer } from 'electron'; | ||
2 | |||
3 | import { v1 as uuidV1 } from 'uuid'; | ||
4 | |||
5 | const debug = require('debug')('Ferdi:Notifications'); | ||
6 | |||
7 | export 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 | |||
33 | export 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 | })();`; | ||