From 6d830ae98be97a11549a09eca81227861728b664 Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Mon, 20 Oct 2025 14:52:37 -0700 Subject: [PATCH] Update browser window calls. --- package.json | 2 +- src/main/graphql/graphql-client.ts | 5 +++-- src/main/index.ts | 13 +++++++------ src/main/watcher/watcher.ts | 9 +++++---- src/util/getMainWindow.ts | 5 +++++ src/util/memUsage.ts | 2 +- 6 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 src/util/getMainWindow.ts diff --git a/package.json b/package.json index 4aef937..7aa79bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bodyshop-desktop", - "version": "1.0.7", + "version": "1.0.7-alpha.1", "description": "Shop Management System Partner", "main": "./out/main/index.js", "author": "Convenient Brands, LLC", diff --git a/src/main/graphql/graphql-client.ts b/src/main/graphql/graphql-client.ts index d1dad22..0dba4b5 100644 --- a/src/main/graphql/graphql-client.ts +++ b/src/main/graphql/graphql-client.ts @@ -4,6 +4,7 @@ import { GraphQLClient, RequestMiddleware } from "graphql-request"; import errorTypeCheck from "../../util/errorTypeCheck.js"; import ipcTypes from "../../util/ipcTypes.json"; import store from "../store/store.js"; +import getMainWindow from "../../util/getMainWindow.js"; const requestMiddleware: RequestMiddleware = async (request) => { const token = await getTokenFromRenderer(); @@ -32,9 +33,9 @@ const client: GraphQLClient = new GraphQLClient( export async function getTokenFromRenderer(): Promise { return new Promise((resolve) => { try { - const mainWindow = BrowserWindow.getAllWindows()[0]; //TODO: Filter to only main window once a proper key has been set. + const mainWindow = getMainWindow(); //TODO: Verify that this will work if the app is minimized/closed. - mainWindow.webContents.send(ipcTypes.toRenderer.user.getToken); + mainWindow?.webContents.send(ipcTypes.toRenderer.user.getToken); } catch (error) { log.error( "Unable to send request to renderer process for token", diff --git a/src/main/index.ts b/src/main/index.ts index 8ea52d1..ec3b11d 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -501,6 +501,7 @@ app.whenReady().then(async () => { // Do nothing if already running return; } else { + log.info("Received Media URL: ", url); openInExplorer(url); } } @@ -568,19 +569,19 @@ app.whenReady().then(async () => { autoUpdater.on("checking-for-update", () => { log.info("Checking for update..."); - const mainWindow = BrowserWindow.getAllWindows()[0]; + const mainWindow = getMainWindow(); mainWindow?.webContents.send(ipcTypes.toRenderer.updates.checking); }); autoUpdater.on("update-available", (info) => { log.info("Update available.", info); - const mainWindow = BrowserWindow.getAllWindows()[0]; + const mainWindow = getMainWindow(); mainWindow?.webContents.send(ipcTypes.toRenderer.updates.available, info); }); autoUpdater.on("download-progress", (progress) => { log.info(`Download speed: ${progress.bytesPerSecond}`); log.info(`Downloaded ${progress.percent}%`); log.info(`Total downloaded ${progress.transferred}/${progress.total}`); - const mainWindow = BrowserWindow.getAllWindows()[0]; + const mainWindow = getMainWindow(); mainWindow?.webContents.send( ipcTypes.toRenderer.updates.downloading, progress, @@ -588,7 +589,7 @@ app.whenReady().then(async () => { }); autoUpdater.on("update-downloaded", (info) => { log.info("Update downloaded", info); - const mainWindow = BrowserWindow.getAllWindows()[0]; + const mainWindow = getMainWindow(); mainWindow?.webContents.send(ipcTypes.toRenderer.updates.downloaded, info); }); @@ -599,7 +600,7 @@ app.whenReady().then(async () => { } //The update itself will run when the bodyshop record is queried to know what release channel to use. - createWindow(); + openMainWindow(); ongoingMemoryDump(); app.on("activate", function () { @@ -658,7 +659,7 @@ function preQuitMethods(): void { } function openMainWindow(): void { - const mainWindow = BrowserWindow.getAllWindows()[0]; + const mainWindow = getMainWindow(); if (mainWindow) { mainWindow.show(); } else { diff --git a/src/main/watcher/watcher.ts b/src/main/watcher/watcher.ts index 3477f27..cd3bf11 100644 --- a/src/main/watcher/watcher.ts +++ b/src/main/watcher/watcher.ts @@ -7,6 +7,7 @@ import errorTypeCheck from "../../util/errorTypeCheck"; import ipcTypes from "../../util/ipcTypes.json"; import ImportJob from "../decoder/decoder"; import store from "../store/store"; +import getMainWindow from "../../util/getMainWindow"; let watcher: FSWatcher | null; async function StartWatcher(): Promise { @@ -107,23 +108,23 @@ function addWatcherPath(path: string | string[]): void { function onWatcherReady(): void { if (watcher) { - const mainWindow = BrowserWindow.getAllWindows()[0]; //TODO: Filter to only main window once a proper key has been set. + const mainWindow = getMainWindow(); new Notification({ title: "Watcher Started", body: "Newly exported estimates will be automatically uploaded.", }).show(); log.info("Confirmed watched paths:", watcher.getWatched()); - mainWindow.webContents.send(ipcTypes.toRenderer.watcher.started); + mainWindow?.webContents.send(ipcTypes.toRenderer.watcher.started); } } async function StopWatcher(): Promise { - const mainWindow = BrowserWindow.getAllWindows()[0]; //TODO: Filter to only main window once a proper key has been set. + const mainWindow = getMainWindow(); if (watcher) { await watcher.close(); log.info("Watcher stopped."); - mainWindow.webContents.send(ipcTypes.toRenderer.watcher.stopped); + mainWindow?.webContents.send(ipcTypes.toRenderer.watcher.stopped); new Notification({ title: "Watcher Stopped", diff --git a/src/util/getMainWindow.ts b/src/util/getMainWindow.ts new file mode 100644 index 0000000..2046a39 --- /dev/null +++ b/src/util/getMainWindow.ts @@ -0,0 +1,5 @@ +import { BrowserWindow } from "electron"; + +export default function getMainWindow(): BrowserWindow | null { + return BrowserWindow.getAllWindows()[0] || null; +} diff --git a/src/util/memUsage.ts b/src/util/memUsage.ts index c257b84..4bd9559 100644 --- a/src/util/memUsage.ts +++ b/src/util/memUsage.ts @@ -297,7 +297,7 @@ function ongoingMemoryDump() { log.warn("Unexpected error while writing memory stats log", err); } }, - 5000, // 60 * 60 * 1000, + 60 * 60 * 1000, ); // every 60 minutes }