From 2dee81fb365b705daf51188342f239872a3afa65 Mon Sep 17 00:00:00 2001 From: "Enrique H. Oliva" Date: Tue, 27 Jul 2021 08:42:14 -0500 Subject: [PATCH] add notification on new message when window is closed --- src/main.ts | 1 - src/session.ts | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index 99c1dfa..334a6fe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 */ diff --git a/src/session.ts b/src/session.ts index 9446cd0..0bcd4e4 100644 --- a/src/session.ts +++ b/src/session.ts @@ -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(); } + -- 2.43.0