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

Add app menu item for updating #1766

Merged
merged 1 commit into from
Mar 17, 2021
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
23 changes: 20 additions & 3 deletions desktop/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,36 @@ for (let i = 0; i < process.argv.length; i++) {
// Add the listeners and variables required to ensure that auto-updating
// happens correctly.
let hasUpdate = false;
let downloadedVersion;

const quitAndInstallWithUpdate = (version) => {
const quitAndInstallWithUpdate = () => {
if (!downloadedVersion) {
return;
}
app.relaunch({
args: [`${EXPECTED_UPDATE_VERSION_FLAG}=${version}`],
args: [`${EXPECTED_UPDATE_VERSION_FLAG}=${downloadedVersion}`],
});
hasUpdate = true;
autoUpdater.quitAndInstall();
};

// Defines the system-level menu item for manually triggering an update after
const updateAppMenuItem = new MenuItem({
label: 'Update Expensify.cash',
enabled: false,
click: quitAndInstallWithUpdate,
});

// Actual auto-update listeners
const electronUpdater = browserWindow => ({
init: () => {
autoUpdater.on('update-downloaded', (info) => {
downloadedVersion = info.version;
updateAppMenuItem.enabled = true;
if (browserWindow.isVisible()) {
browserWindow.webContents.send('update-downloaded', info.version);
} else {
quitAndInstallWithUpdate(info.version);
quitAndInstallWithUpdate();
}
});

Expand Down Expand Up @@ -134,6 +148,9 @@ const mainWindow = (() => {
}],
}));

const appMenu = systemMenu.items.find(item => item.role === 'appmenu');
appMenu.submenu.insert(1, updateAppMenuItem);

// 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.
const viewWindow = systemMenu.items.find(item => item.role === 'viewmenu');
Expand Down
12 changes: 6 additions & 6 deletions src/Expensify.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ const propTypes = {
accountID: PropTypes.number,
}),

// Version of newly downloaded update.
version: PropTypes.string,
// Whether a new update is available and ready to install.
updateAvailable: PropTypes.bool,
};

const defaultProps = {
session: {
authToken: null,
accountID: null,
},
version: '',
updateAvailable: false,
};

class Expensify extends PureComponent {
Expand Down Expand Up @@ -98,7 +98,7 @@ class Expensify extends PureComponent {
return (
<>
{/* We include the modal for showing a new update at the top level so the option is always present. */}
{this.props.version ? <UpdateAppModal updateVersion={this.props.version} /> : null}
{this.props.updateAvailable ? <UpdateAppModal /> : null}
<NavigationRoot authenticated={Boolean(authToken)} />
</>
);
Expand All @@ -111,8 +111,8 @@ export default withOnyx({
session: {
key: ONYXKEYS.SESSION,
},
version: {
key: ONYXKEYS.UPDATE_VERSION,
updateAvailable: {
key: ONYXKEYS.UPDATE_AVAILABLE,
initWithStoredValues: false,
},
})(Expensify);
4 changes: 2 additions & 2 deletions src/ONYXKEYS.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export default {
// Contains the user preference for the LHN priority mode
PRIORITY_MODE: 'priorityMode',

// Contains the version of the update that has newly been downloaded.
UPDATE_VERSION: 'updateVersion',
// Indicates whether an update is available and ready to beinstalled.
UPDATE_AVAILABLE: 'updateAvailable',

// Saves the current country code which is displayed when the user types a phone number without
// an international code
Expand Down
5 changes: 0 additions & 5 deletions src/components/UpdateAppModal/UpdateAppModalPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ import PropTypes from 'prop-types';
const propTypes = {
// Callback to fire when we want to trigger the update.
onSubmit: PropTypes.func,

// Version string for the app to update to.
// eslint-disable-next-line react/no-unused-prop-types
version: PropTypes.string,
};

const defaultProps = {
onSubmit: null,
version: '',
};

export {propTypes, defaultProps};
2 changes: 1 addition & 1 deletion src/components/UpdateAppModal/index.desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const UpdateAppModal = (props) => {
if (props.onSubmit) {
props.onSubmit();
}
ipcRenderer.sendSync('start-update', props.version);
ipcRenderer.sendSync('start-update');
};
return <BaseUpdateAppModal onSubmit={updateApp} />;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,14 @@ export default {

/**
* Create a notification to indicate that an update is available.
*
* @param {Object} params
* @param {String} params.version
*/
pushUpdateAvailableNotification({version}) {
pushUpdateAvailableNotification() {
push({
title: 'Update available',
body: 'A new version of Expensify.cash is available!',
delay: 0,
onClick: () => {
Onyx.merge(ONYXKEYS.UPDATE_VERSION, version);
Onyx.merge(ONYXKEYS.UPDATE_AVAILABLE, true);
},
});
},
Expand Down
4 changes: 2 additions & 2 deletions src/libs/Notification/LocalNotification/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ function showCommentNotification({reportAction, onClick}) {
BrowserNotifications.pushReportCommentNotification({reportAction, onClick});
}

function showUpdateAvailableNotification({version}) {
BrowserNotifications.pushUpdateAvailableNotification({version});
function showUpdateAvailableNotification() {
BrowserNotifications.pushUpdateAvailableNotification();
}

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/setup/index.desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function () {
rootTag: document.getElementById('root'),
});

ipcRenderer.on('update-downloaded', (_, version) => {
LocalNotification.showUpdateAvailableNotification({version});
ipcRenderer.on('update-downloaded', () => {
LocalNotification.showUpdateAvailableNotification();
});
}