From 53defd6244e8b43d95440af8507173bf2e3739fa Mon Sep 17 00:00:00 2001 From: Vijay A Date: Tue, 17 Aug 2021 08:49:04 +0530 Subject: refactor: Minor refactoring to handle NaN in 'setBadge' --- src/helpers/url-helpers.ts | 2 +- src/index.js | 1 - src/webview/badge.js | 19 +++++++++++-------- 3 files changed, 12 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/helpers/url-helpers.ts b/src/helpers/url-helpers.ts index e330fae40..3657ae693 100644 --- a/src/helpers/url-helpers.ts +++ b/src/helpers/url-helpers.ts @@ -30,7 +30,7 @@ export async function openPath(folderName: string) { // TODO: Need to verify and fix/remove the skipping logic. Ideally, we should never skip this check export function openExternalUrl(url: string | URL, skipValidityCheck: boolean = false) { - debug('for url:', url, 'skipValidityCheck:', skipValidityCheck); + debug('Open url:', url, 'with skipValidityCheck:', skipValidityCheck); if (skipValidityCheck || isValidExternalURL(url)) { shell.openExternal(url.toString()); } diff --git a/src/index.js b/src/index.js index 563ad08ed..7634fd35b 100644 --- a/src/index.js +++ b/src/index.js @@ -342,7 +342,6 @@ const createWindow = () => { app.isMaximized = mainWindow.isMaximized(); mainWindow.webContents.on('new-window', (e, url) => { - debug('Open url', url); e.preventDefault(); openExternalUrl(url); }); diff --git a/src/webview/badge.js b/src/webview/badge.js index 1e02fb56a..6be4cf609 100644 --- a/src/webview/badge.js +++ b/src/webview/badge.js @@ -10,19 +10,22 @@ export class BadgeHandler { }; } - setBadge(direct, indirect) { - if (this.countCache.direct === direct - && this.countCache.indirect === indirect) return; - + _normalizeNumber(count) { // Parse number to integer // This will correct errors that recipes may introduce, e.g. // by sending a String instead of an integer - const directInt = parseInt(direct, 10); - const indirectInt = parseInt(indirect, 10); + const parsedNumber = parseInt(count, 10); + const adjustedNumber = Number.isNaN(parsedNumber) ? 0 : parsedNumber; + return Math.max(adjustedNumber, 0); + } + + setBadge(direct, indirect) { + if (this.countCache.direct === direct + && this.countCache.indirect === indirect) return; const count = { - direct: Math.max(directInt, 0), - indirect: Math.max(indirectInt, 0), + direct: this._normalizeNumber(direct), + indirect: this._normalizeNumber(indirect), }; ipcRenderer.sendToHost('message-counts', count); -- cgit v1.2.3-54-g00ecf