Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: silently update desktop app #40253

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions desktop/ELECTRON_EVENTS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ELECTRON_EVENTS = {
KEYBOARD_SHORTCUTS_PAGE: 'keyboard-shortcuts-page',
START_UPDATE: 'start-update',
UPDATE_DOWNLOADED: 'update-downloaded',
SILENT_UPDATE: 'silent-update',
} as const;

export default ELECTRON_EVENTS;
1 change: 1 addition & 0 deletions desktop/contextBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const WHITELIST_CHANNELS_RENDERER_TO_MAIN = [
ELECTRON_EVENTS.REQUEST_VISIBILITY,
ELECTRON_EVENTS.START_UPDATE,
ELECTRON_EVENTS.LOCALE_UPDATED,
ELECTRON_EVENTS.SILENT_UPDATE,
] as const;

const WHITELIST_CHANNELS_MAIN_TO_RENDERER = [ELECTRON_EVENTS.KEYBOARD_SHORTCUTS_PAGE, ELECTRON_EVENTS.UPDATE_DOWNLOADED, ELECTRON_EVENTS.FOCUS, ELECTRON_EVENTS.BLUR] as const;
Expand Down
36 changes: 27 additions & 9 deletions desktop/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ process.argv.forEach((arg) => {
// happens correctly.
let hasUpdate = false;
let downloadedVersion: string;
let isSilentUpdating = false;

// Note that we have to subscribe to this separately and cannot use Localize.translateLocal,
// because the only way code can be shared between the main and renderer processes at runtime is via the context bridge
Expand All @@ -127,16 +128,20 @@ const quitAndInstallWithUpdate = () => {
autoUpdater.quitAndInstall();
};

/** Menu Item callback to triggers an update check */
const manuallyCheckForUpdates = (menuItem: MenuItem, browserWindow?: 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;
/** Menu Item callback to trigger an update check */
const manuallyCheckForUpdates = (menuItem?: MenuItem, browserWindow?: BrowserWindow) => {
if (menuItem) {
// Disable item until the check (and download) is complete
// eslint-disable-next-line no-param-reassign -- menu item flags like enabled or visible can be dynamically toggled by mutating the object
menuItem.enabled = false;
}

autoUpdater
.checkForUpdates()
.catch((error) => ({error}))
.catch((error) => {
isSilentUpdating = false;
return {error};
})
.then((result) => {
const downloadPromise = result && 'downloadPromise' in result ? result.downloadPromise : undefined;

Expand All @@ -148,7 +153,7 @@ const manuallyCheckForUpdates = (menuItem: MenuItem, browserWindow?: BrowserWind
dialog.showMessageBox(browserWindow, {
type: 'info',
message: Localize.translate(preferredLocale, 'checkForUpdatesModal.available.title'),
detail: Localize.translate(preferredLocale, 'checkForUpdatesModal.available.message'),
detail: Localize.translate(preferredLocale, 'checkForUpdatesModal.available.message', {isSilentUpdating}),
buttons: [Localize.translate(preferredLocale, 'checkForUpdatesModal.available.soundsGood')],
});
} else if (result && 'error' in result && result.error) {
Expand All @@ -172,6 +177,10 @@ const manuallyCheckForUpdates = (menuItem: MenuItem, browserWindow?: BrowserWind
return downloadPromise;
})
.finally(() => {
isSilentUpdating = false;
if (!menuItem) {
return;
}
// eslint-disable-next-line no-param-reassign
menuItem.enabled = true;
});
Expand Down Expand Up @@ -201,7 +210,7 @@ const electronUpdater = (browserWindow: BrowserWindow): PlatformSpecificUpdater
if (checkForUpdatesMenuItem) {
checkForUpdatesMenuItem.visible = false;
}
if (browserWindow.isVisible()) {
if (browserWindow.isVisible() && !isSilentUpdating) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gijoe0295 I still think that we should show a notification to the user after updating version successfully

cc @Beamanator

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm well this is why we're calling it "silent" updating I guess haha. At this point, if the user already clicked "Update" I'm not sure this would require another notification to the user -> Like on Web, we would just refresh & BAM it's updated, no extra notification 🤔

browserWindow.webContents.send(ELECTRON_EVENTS.UPDATE_DOWNLOADED, info.version);
} else {
quitAndInstallWithUpdate();
Expand Down Expand Up @@ -604,6 +613,15 @@ const mainWindow = (): Promise<void> => {
}
});

// Automatically check for and install the latest version in the background
ipcMain.on(ELECTRON_EVENTS.SILENT_UPDATE, () => {
if (isSilentUpdating) {
return;
}
isSilentUpdating = true;
manuallyCheckForUpdates(undefined, browserWindow);
});

return browserWindow;
})

Expand Down
3 changes: 2 additions & 1 deletion src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2623,7 +2623,8 @@ export default {
checkForUpdatesModal: {
available: {
title: 'Update Available',
message: "The new version will be available shortly. We'll notify you when we're ready to update.",
message: ({isSilentUpdating}: {isSilentUpdating: boolean}) =>
`The new version will be available shortly.${!isSilentUpdating ? " We'll notify you when we're ready to update." : ''}`,
soundsGood: 'Sounds good',
},
notAvailable: {
Expand Down
3 changes: 2 additions & 1 deletion src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2655,7 +2655,8 @@ export default {
checkForUpdatesModal: {
available: {
title: 'Actualización disponible',
message: 'La nueva versión estará disponible dentro de poco. Te notificaremos cuando esté lista.',
message: ({isSilentUpdating}: {isSilentUpdating: boolean}) =>
`La nueva versión estará disponible dentro de poco.${isSilentUpdating ? ' Te notificaremos cuando esté lista.' : ''}`,
soundsGood: 'Suena bien',
},
notAvailable: {
Expand Down
5 changes: 2 additions & 3 deletions src/libs/actions/AppUpdate/updateApp/index.desktop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Linking} from 'react-native';
import CONST from '@src/CONST';
import ELECTRON_EVENTS from '@desktop/ELECTRON_EVENTS';

export default function updateApp() {
Linking.openURL(CONST.APP_DOWNLOAD_LINKS.DESKTOP);
window.electron.send(ELECTRON_EVENTS.SILENT_UPDATE);
}
Loading