diff --git a/src/init.ts b/src/init.ts index 9a0fdf6..637db63 100644 --- a/src/init.ts +++ b/src/init.ts @@ -9,6 +9,7 @@ import { app, protocol } from "electron"; import createWindow from "./window"; import main from "./main"; import { backgroundMitt } from '@/composables/useEmitter'; +import {setWindow, getWindow } from './store'; console.log('Starting Crimata electron app.'); @@ -19,11 +20,9 @@ protocol.registerSchemesAsPrivileged([ const isDev = require('electron-is-dev'); -let win: boolean; - // Listen for window creation. backgroundMitt.on('window-active', (state: boolean) => { - win = state; + setWindow(state) }); /* Start main process on ready */ @@ -41,7 +40,7 @@ app.on("window-all-closed", () => { // When user clicks app icon (re-open) app.on("activate", () => { - if (!win) createWindow(); + if (!getWindow()) createWindow(); }); // Exit cleanly on request from parent process in development mode. 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..be6fdd3 100644 --- a/src/session.ts +++ b/src/session.ts @@ -4,13 +4,27 @@ import UIState from "@/uiState"; import { config } from "@/config"; import useWebsockets from "./composables/useWebsockets"; import { backgroundMitt, ipcEmit } from "@/composables/useEmitter"; +import { Notification } from 'electron'; +import { getWindow } from './store'; + +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 +36,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 (!getWindow()) { + 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 +99,4 @@ export function endSession() { uiState.reset(); } + diff --git a/src/store.ts b/src/store.ts index 0200681..8ac280e 100644 --- a/src/store.ts +++ b/src/store.ts @@ -3,7 +3,10 @@ const Store = require('electron-store'); const schema = { token: { type: 'string', - } + }, + window: { + type: 'boolean' + } }; const store = new Store({ @@ -11,9 +14,12 @@ const store = new Store({ encryptionKey: "super user test" }); +/* + * Token state management +* */ export const setToken = (token: string): void => { store.set("token", token); -} +} export const getToken = (): string | undefined => { return store.get("token"); @@ -21,4 +27,19 @@ export const getToken = (): string | undefined => { export const clearToken = (): void => { store.delete("token"); -} \ No newline at end of file +} + +/* + * Window state management +* */ +export const setWindow = (state: boolean): void => { + store.set("window", state); +} + +export const getWindow = (): boolean | undefined => { + return store.get("window"); +} + +export const clearWindow = (): void => { + store.delete("window"); +}