From 246d70cf747749af9fbecf558e55739deb7f70bc Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Wed, 9 Nov 2022 19:53:08 +0200 Subject: [PATCH 1/4] Desktop - Add "Check For Update" system menu item --- desktop/main.js | 64 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/desktop/main.js b/desktop/main.js index 78c13e4d448cb..b02f926b24c8e 100644 --- a/desktop/main.js +++ b/desktop/main.js @@ -1,5 +1,6 @@ const { app, + dialog, BrowserWindow, Menu, MenuItem, @@ -80,6 +81,51 @@ const quitAndInstallWithUpdate = () => { autoUpdater.quitAndInstall(); }; +/** + * Menu Item callback to triggers an update check + * @param {MenuItem} menuItem + * @param {BrowserWindow} browserWindow + */ +const manuallyCheckForUpdates = (menuItem, browserWindow) => { + // Disable item until the check (and download) is complete + // eslint: menu item flags like enabled or visible can be dynamically toggled by mutating the object + // eslint-disable-next-line no-param-reassign + menuItem.enabled = false; + + autoUpdater.checkForUpdates() + .then((result) => { + const downloadPromise = result && result.downloadPromise; + const icon = `${__dirname}/../icon-dev.png`; + const type = 'info'; + + if (downloadPromise) { + dialog.showMessageBox(browserWindow, { + icon, + type, + message: 'Update Available', + detail: 'The new version will be available shortly. We’ll notify you when we’re ready to apply the update', + buttons: ['Can’t wait to update'], + }); + } else { + dialog.showMessageBox(browserWindow, { + icon, + type, + message: 'Update Not Available', + detail: 'We don’t have an update right now, but you get a star 🌟 for trying', + buttons: ['Funny', 'Not Funny', 'Close'], + cancelId: 2, + }); + } + + // By returning the `downloadPromise` we keep "check for updates" disabled if any updates are being downloaded + return downloadPromise; + }) + .finally(() => { + // eslint-disable-next-line no-param-reassign + menuItem.enabled = true; + }); +}; + /** * Trigger event to show keyboard shortcuts * @param {BrowserWindow} browserWindow @@ -91,13 +137,21 @@ const showKeyboardShortcutsModal = (browserWindow) => { browserWindow.webContents.send(ELECTRON_EVENTS.SHOW_KEYBOARD_SHORTCUTS_MODAL); }; -// Defines the system-level menu item for manually triggering an update after +// Defines the system-level menu item to manually apply an update +// This menu item should become visible after an update is download and ready to be applied const updateAppMenuItem = new MenuItem({ label: 'Update New Expensify', - enabled: false, + visible: false, click: quitAndInstallWithUpdate, }); +// System-level menu item to manually check for App updates +const checkForUpdateMenuItem = new MenuItem({ + label: 'Check For Updates', + visible: true, + click: manuallyCheckForUpdates, +}); + // Defines the system-level menu item for opening keyboard shortcuts modal const keyboardShortcutsMenu = new MenuItem({ label: 'View Keyboard Shortcuts', @@ -109,7 +163,8 @@ const electronUpdater = browserWindow => ({ init: () => { autoUpdater.on(ELECTRON_EVENTS.UPDATE_DOWNLOADED, (info) => { downloadedVersion = info.version; - updateAppMenuItem.enabled = true; + updateAppMenuItem.visible = true; + checkForUpdateMenuItem.visible = false; if (browserWindow.isVisible()) { browserWindow.webContents.send(ELECTRON_EVENTS.UPDATE_DOWNLOADED, info.version); } else { @@ -221,7 +276,8 @@ const mainWindow = (() => { const appMenu = _.find(systemMenu.items, item => item.role === 'appmenu'); appMenu.submenu.insert(1, updateAppMenuItem); - appMenu.submenu.insert(2, keyboardShortcutsMenu); + appMenu.submenu.insert(2, checkForUpdateMenuItem); + appMenu.submenu.insert(3, keyboardShortcutsMenu); // On mac, pressing cmd++ actually sends a cmd+=. cmd++ is generally the zoom in shortcut, but this is // not properly listened for by electron. Adding in an invisible cmd+= listener fixes this. From d7b6f55c060b18fad5b2fd61d8d79e559736bf3f Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Wed, 9 Nov 2022 21:26:18 +0200 Subject: [PATCH 2/4] Desktop: cover "Check For Update" offline usage or other errors --- desktop/main.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/desktop/main.js b/desktop/main.js index b02f926b24c8e..60fcd1b792e17 100644 --- a/desktop/main.js +++ b/desktop/main.js @@ -93,23 +93,26 @@ const manuallyCheckForUpdates = (menuItem, browserWindow) => { menuItem.enabled = false; autoUpdater.checkForUpdates() + .catch(error => ({error})) .then((result) => { const downloadPromise = result && result.downloadPromise; - const icon = `${__dirname}/../icon-dev.png`; - const type = 'info'; if (downloadPromise) { dialog.showMessageBox(browserWindow, { - icon, - type, + type: 'info', message: 'Update Available', detail: 'The new version will be available shortly. We’ll notify you when we’re ready to apply the update', buttons: ['Can’t wait to update'], }); + } else if (result && result.error) { + dialog.showMessageBox(browserWindow, { + type: 'error', + message: 'Update check failed', + detail: 'We were unable to check for updates at this time', + }); } else { dialog.showMessageBox(browserWindow, { - icon, - type, + type: 'info', message: 'Update Not Available', detail: 'We don’t have an update right now, but you get a star 🌟 for trying', buttons: ['Funny', 'Not Funny', 'Close'], From 672fbb534a34a8437db96fe480efdf9ec75700d2 Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Tue, 15 Nov 2022 20:35:50 +0200 Subject: [PATCH 3/4] Desktop: update "Check For Update" text labels and messages --- desktop/main.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/desktop/main.js b/desktop/main.js index 60fcd1b792e17..fd38bf5abcfba 100644 --- a/desktop/main.js +++ b/desktop/main.js @@ -101,21 +101,22 @@ const manuallyCheckForUpdates = (menuItem, browserWindow) => { dialog.showMessageBox(browserWindow, { type: 'info', message: 'Update Available', - detail: 'The new version will be available shortly. We’ll notify you when we’re ready to apply the update', - buttons: ['Can’t wait to update'], + detail: 'The new version will be available shortly. We’ll notify you when we’re ready to update.', + buttons: ['Sounds good'], }); } else if (result && result.error) { dialog.showMessageBox(browserWindow, { type: 'error', - message: 'Update check failed', - detail: 'We were unable to check for updates at this time', + message: 'Update Check Failed', + detail: 'We couldn’t look for an update. Please check again in a bit!', + buttons: ['Okay'], }); } else { dialog.showMessageBox(browserWindow, { type: 'info', message: 'Update Not Available', - detail: 'We don’t have an update right now, but you get a star 🌟 for trying', - buttons: ['Funny', 'Not Funny', 'Close'], + detail: 'There is no update available as of now! Check again at a later time.', + buttons: ['Okay'], cancelId: 2, }); } From fd53eebbd011b595b37967129942cf12f4f68a7d Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Wed, 23 Nov 2022 20:38:16 +0200 Subject: [PATCH 4/4] Update desktop/main.js code comment Co-authored-by: Rocio Perez --- desktop/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/desktop/main.js b/desktop/main.js index fd38bf5abcfba..caa4f720a4cf9 100644 --- a/desktop/main.js +++ b/desktop/main.js @@ -142,7 +142,7 @@ const showKeyboardShortcutsModal = (browserWindow) => { }; // Defines the system-level menu item to manually apply an update -// This menu item should become visible after an update is download and ready to be applied +// This menu item should become visible after an update is downloaded and ready to be applied const updateAppMenuItem = new MenuItem({ label: 'Update New Expensify', visible: false,