forked from trezor/trezor-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesktopUpdater.tsx
149 lines (130 loc) · 5.24 KB
/
DesktopUpdater.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React, { useCallback, useEffect, useMemo } from 'react';
import { analytics, AppUpdateEventStatus, EventType } from '@trezor/suite-analytics';
import { desktopApi } from '@trezor/suite-desktop-api';
import { useActions, useSelector } from 'src/hooks/suite';
import * as desktopUpdateActions from 'src/actions/suite/desktopUpdateActions';
import { UpdateState } from 'src/reducers/suite/desktopUpdateReducer';
import { ModalContextProvider } from 'src/support/suite/ModalContext';
import { getAppUpdatePayload } from 'src/utils/suite/analytics';
import { selectRouteName } from 'src/reducers/suite/routerReducer';
import { Available } from './DesktopUpdater/Available';
import { Downloading } from './DesktopUpdater/Downloading';
import { Ready } from './DesktopUpdater/Ready';
import { EarlyAccessEnable } from './DesktopUpdater/EarlyAccessEnable';
import { EarlyAccessDisable } from './DesktopUpdater/EarlyAccessDisable';
interface DesktopUpdaterProps {
children: React.ReactNode;
}
export const DesktopUpdater = ({ children }: DesktopUpdaterProps) => {
const {
checking,
available,
notAvailable,
downloading,
ready,
error,
setUpdateWindow,
allowPrerelease,
} = useActions({
checking: desktopUpdateActions.checking,
available: desktopUpdateActions.available,
notAvailable: desktopUpdateActions.notAvailable,
downloading: desktopUpdateActions.downloading,
ready: desktopUpdateActions.ready,
error: desktopUpdateActions.error,
setUpdateWindow: desktopUpdateActions.setUpdateWindow,
allowPrerelease: desktopUpdateActions.allowPrerelease,
});
const { desktopUpdate } = useSelector(state => state);
const routeName = useSelector(selectRouteName);
// Closing a modal opened by auto updater outside of device settings might confuse some users,
// especially on app start if closing it unexpectedly by clicking outside.
const isSettingsRoute = routeName === 'settings-index';
useEffect(() => {
desktopApi.on('update/allow-prerelease', allowPrerelease);
if (!desktopUpdate.enabled) {
return;
}
desktopApi.on('update/checking', checking);
desktopApi.on('update/available', available);
desktopApi.on('update/not-available', notAvailable);
desktopApi.on('update/downloaded', ready);
desktopApi.on('update/downloading', downloading);
desktopApi.on('update/error', error);
// Initial check for updates
desktopApi.checkForUpdates();
// Check for updates every hour
const checkForUpdatesInterval = setInterval(() => {
desktopApi.checkForUpdates();
}, 60 * 60 * 1000);
return () => clearInterval(checkForUpdatesInterval);
}, [
available,
checking,
downloading,
notAvailable,
ready,
error,
desktopUpdate.enabled,
allowPrerelease,
]);
const hideWindow = useCallback(() => {
setUpdateWindow('hidden');
const payload = getAppUpdatePayload(
AppUpdateEventStatus.Closed,
desktopUpdate.allowPrerelease,
desktopUpdate.latest,
);
analytics.report({
type: EventType.AppUpdate,
payload,
});
}, [setUpdateWindow, desktopUpdate.latest, desktopUpdate.allowPrerelease]);
const isVisible = useMemo(() => {
// Not displayed as a modal
if (desktopUpdate.window !== 'maximized') {
return false;
}
// Non visible states
if ([UpdateState.Checking, UpdateState.NotAvailable].includes(desktopUpdate.state)) {
return false;
}
const isEarlyAccessState = [
UpdateState.EarlyAccessDisable,
UpdateState.EarlyAccessEnable,
].includes(desktopUpdate.state);
// Enable to setup Early Access even after updater error (when desktopUpdate.latest is undefined).
if (!isEarlyAccessState && !desktopUpdate.latest) {
return false;
}
return true;
}, [desktopUpdate.window, desktopUpdate.state, desktopUpdate.latest]);
const getUpdateModal = () => {
switch (desktopUpdate.state) {
case UpdateState.EarlyAccessEnable:
return <EarlyAccessEnable hideWindow={hideWindow} />;
case UpdateState.EarlyAccessDisable:
return <EarlyAccessDisable hideWindow={hideWindow} />;
case UpdateState.Available:
return (
<Available
hideWindow={hideWindow}
latest={desktopUpdate.latest}
isCancelable={isSettingsRoute}
/>
);
case UpdateState.Downloading:
return <Downloading hideWindow={hideWindow} progress={desktopUpdate.progress} />;
case UpdateState.Ready:
return <Ready hideWindow={hideWindow} isCancelable={isSettingsRoute} />;
default:
return null;
}
};
return (
<>
{isVisible && getUpdateModal()}
<ModalContextProvider isDisabled={isVisible}>{children}</ModalContextProvider>
</>
);
};