Skip to content

Commit b8d24ca

Browse files
committed
feat: Add API for downloading and installing updates
1 parent cd8e5d5 commit b8d24ca

6 files changed

Lines changed: 47 additions & 17 deletions

File tree

packages/neuron-ui/src/components/GeneralSetting/index.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next'
33
import { Stack, PrimaryButton, Spinner, Text, ProgressIndicator } from 'office-ui-fabric-react'
44
import { StateWithDispatch } from 'states/stateProvider/reducer'
55
import { addPopup } from 'states/stateProvider/actionCreators'
6-
import { checkForUpdates, clearCellCache } from 'services/remote'
6+
import { checkForUpdates, downloadUpdate, installUpdate, clearCellCache } from 'services/remote'
77

88
const UpdateDownloadStatus = ({
99
progress = 0,
@@ -14,13 +14,19 @@ const UpdateDownloadStatus = ({
1414
const downloaded = progress >= 1
1515

1616
if (available) {
17+
const download = () => {
18+
downloadUpdate()
19+
}
20+
1721
return (
1822
<Stack>
1923
<Text as="p" variant="medium">
2024
{t('updates.updates-found-do-you-want-to-update', { version: newVersion })}
2125
</Text>
2226
<Stack horizontal horizontalAlign="start">
2327
<PrimaryButton
28+
onClick={download}
29+
disabled={available}
2430
styles={{
2531
root: {
2632
minWidth: 180,
@@ -35,13 +41,19 @@ const UpdateDownloadStatus = ({
3541
}
3642

3743
if (downloaded) {
44+
const quitAndInstall = () => {
45+
installUpdate()
46+
}
47+
3848
return (
3949
<Stack>
4050
<Text as="p" variant="medium">
4151
{t('updates.updates-downloaded-about-to-quit-and-install')}
4252
</Text>
4353
<Stack horizontal horizontalAlign="start">
4454
<PrimaryButton
55+
onClick={quitAndInstall}
56+
disabled={!downloaded}
4557
styles={{
4658
root: {
4759
minWidth: 180,

packages/neuron-ui/src/services/remote/app.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ export const getNeuronWalletState = apiMethodWrapper<void>(api => () => api.load
55
export const handleViewError = apiMethodWrapper<string>(api => errorMessage => api.handleViewError(errorMessage))
66
export const contextMenu = apiMethodWrapper<{ type: string; id: string }>(api => params => api.contextMenu(params))
77

8-
export const checkForUpdates = apiMethodWrapper<void>(api => () => api.checkForUpdates())
98
export const clearCellCache = apiMethodWrapper<void>(api => () => api.clearCellCache())
109

1110
export default {
1211
getNeuronWalletState,
1312
handleViewError,
1413
contextMenu,
15-
checkForUpdates,
1614
clearCellCache,
1715
}

packages/neuron-ui/src/services/remote/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './app'
22
export * from './wallets'
33
export * from './networks'
44
export * from './transactions'
5+
export * from './updater'
56

67
const REMOTE_MODULE_NOT_FOUND =
78
'The remote module is not found, please make sure the UI is running inside the Electron App'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { apiMethodWrapper } from './apiMethodWrapper'
2+
3+
export const checkForUpdates = apiMethodWrapper<void>(api => () => api.checkForUpdates())
4+
export const downloadUpdate = apiMethodWrapper<void>(api => () => api.downloadUpdate())
5+
export const installUpdate = apiMethodWrapper<void>(api => () => api.quitAndInstall())
6+
7+
export default {
8+
checkForUpdates,
9+
downloadUpdate,
10+
installUpdate,
11+
}

packages/neuron-wallet/src/controllers/api.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,23 @@ export default class ApiController {
279279
return DaoController.getDaoCells(params)
280280
}
281281

282-
// settings
282+
// Settings
283283

284284
@MapApiResponse
285285
public static async checkForUpdates() {
286286
return new UpdateController().checkUpdates()
287287
}
288288

289+
@MapApiResponse
290+
public static async downloadUpdate() {
291+
return new UpdateController(false).downloadUpdate()
292+
}
293+
294+
@MapApiResponse
295+
public static async quitAndInstall() {
296+
return new UpdateController(false).quitAndInstall()
297+
}
298+
289299
@MapApiResponse
290300
public static async clearCellCache() {
291301
await SyncController.stopSyncing()

packages/neuron-wallet/src/controllers/update.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import AppUpdaterSubject from 'models/subjects/app-updater'
66
export default class UpdateController {
77
static isChecking = false // One instance is already running and checking
88

9-
constructor() {
9+
constructor(check: boolean = true) {
1010
autoUpdater.autoDownload = false
1111

12-
if (!UpdateController.isChecking) {
12+
if (check && !UpdateController.isChecking) {
1313
this.bindEvents()
1414
}
1515
}
@@ -26,7 +26,15 @@ export default class UpdateController {
2626
})
2727
}
2828

29-
bindEvents() {
29+
public quitAndInstall() {
30+
autoUpdater.quitAndInstall()
31+
}
32+
33+
public downloadUpdate() {
34+
autoUpdater.downloadUpdate()
35+
}
36+
37+
private bindEvents() {
3038
autoUpdater.removeAllListeners()
3139

3240
autoUpdater.on('error', error => {
@@ -53,16 +61,6 @@ export default class UpdateController {
5361
})
5462

5563
autoUpdater.on('update-downloaded', () => {
56-
dialog
57-
.showMessageBox({
58-
type: 'info',
59-
message: i18n.t('updater.updates-downloaded-about-to-quit-and-install'),
60-
buttons: [i18n.t('common.ok')],
61-
})
62-
.then(() => {
63-
setImmediate(() => autoUpdater.quitAndInstall())
64-
})
65-
6664
UpdateController.isChecking = false
6765
this.notify(1, 'toto', '')
6866
})

0 commit comments

Comments
 (0)