]> Repos - mime-chat/commitdiff
add notification on new message when window is closed
authorEnrique H. Oliva <hernandeze2@xavier.edu>
Tue, 27 Jul 2021 13:42:14 +0000 (08:42 -0500)
committerEnrique H. Oliva <hernandeze2@xavier.edu>
Wed, 28 Jul 2021 13:11:34 +0000 (08:11 -0500)
src/main.ts
src/session.ts

index 99c1dfaf9c472eaac891ba5d9ea472c948330ccb..334a6fe34481e24f604f13244ebc5b46722e8355 100644 (file)
@@ -13,7 +13,6 @@ import initIpcMain from "@/ipc/index";
 import  { accountAuth } from "./account";
 import createWindow from "./window";
 
-
 export default async function main() {
 
     /* initiate controls for frontend to use when needed */
index 9446cd0b896759e82f967db7e9b654b62ca28e56..0bcd4e41330d854df5533e4b3a22845f2c135472 100644 (file)
@@ -4,13 +4,33 @@ import UIState from "@/uiState";
 import { config } from "@/config";
 import useWebsockets from "./composables/useWebsockets";
 import { backgroundMitt, ipcEmit } from "@/composables/useEmitter";
+import { Notification } from 'electron';
+
+let win: boolean;
+
+// Listen for window creation.
+backgroundMitt.on('window-active', (state: boolean) => {
+    win = state;
+});
+
+function showNotification (options: {
+    title: string;
+    subtitle?: string;
+    body: string;
+}) {
+    new Notification(
+        {
+            title: options.title,
+            subtitle: options.subtitle,
+            body: options.body,
+        }).show()
+}
 
 // UI state
-const uiState = new UIState()
+const uiState = new UIState();
 
 // let audio = new Audio();
 
-
 const onMessageCallback = (payload: string) => {
 
     if (payload === "CLOSE_AUTH_FAIL") {
@@ -22,7 +42,23 @@ const onMessageCallback = (payload: string) => {
 
     if (message.header === "init") {
         uiState.set(message.body as Init);
-    } else uiState.update(message.body as Update);
+    } else {
+        const body = message.body as Update;
+        uiState.update(body);
+
+        // show notification if window is not active
+        if (!win) {
+            if (body.name === "add") {
+                const data = body.data as Message;
+                showNotification({
+                    title: 'New Message',
+                    subtitle: data.context.text as string,
+                    body: data.content[0].text,
+                });
+            }
+
+        }
+    }
 
 }
 
@@ -69,3 +105,4 @@ export function endSession() {
     uiState.reset();
 
 }
+