From 52211095aab71f8b59b093b19ae34c222be9f390 Mon Sep 17 00:00:00 2001 From: Markus Hatvan Date: Thu, 14 Oct 2021 09:32:20 +0200 Subject: chore: convert various JS to TS (#2062) --- src/webview/notifications.ts | 72 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/webview/notifications.ts (limited to 'src/webview/notifications.ts') 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 @@ +import { ipcRenderer } from 'electron'; + +import { v1 as uuidV1 } from 'uuid'; + +const debug = require('debug')('Ferdi:Notifications'); + +export class NotificationsHandler { + onNotify = (data: { title: string; options: any; notificationId: string }) => + data; + + displayNotification(title: string, options: any) { + return new Promise(resolve => { + debug('New notification', title, options); + + const notificationId = uuidV1(); + + ipcRenderer.sendToHost( + 'notification', + this.onNotify({ + title, + options, + notificationId, + }), + ); + + ipcRenderer.once(`notification-onclick:${notificationId}`, () => { + resolve(true); + }); + }); + } +} + +export const notificationsClassDefinition = `(() => { + class Notification { + static permission = 'granted'; + + constructor(title = '', options = {}) { + this.title = title; + this.options = options; + window.ferdi.displayNotification(title, options) + .then(() => { + 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; +})();`; -- cgit v1.2.3-70-g09d2