-
Notifications
You must be signed in to change notification settings - Fork 295
/
AppStore.ts
260 lines (232 loc) · 8.37 KB
/
AppStore.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { observable, computed, action, runInAction } from 'mobx';
import path from 'path';
import Store from './lib/Store';
import LocalizableError from '../i18n/LocalizableError';
import { buildRoute } from '../utils/routing';
import { ROUTES } from '../routes-config';
import { DIALOGS, PAGES, NOTIFICATIONS } from '../../../common/ipc/constants';
import { openExternalUrlChannel } from '../ipc/open-external-url';
import { showSaveDialogChannel } from '../ipc/show-file-dialog-channels';
import {
toggleUiPartChannel,
showUiPartChannel,
} from '../ipc/control-ui-parts';
import { getGPUStatusChannel } from '../ipc/get-gpu-status.ipc';
import { generateFileNameWithTimestamp } from '../../../common/utils/files';
import type { GpuStatus } from '../types/gpuStatus';
import type { ApplicationDialog } from '../types/applicationDialogTypes';
import { EventCategories } from '../analytics';
export default class AppStore extends Store {
@observable
error: LocalizableError | null | undefined = null;
@observable
isDownloadNotificationVisible = false;
@observable
gpuStatus: GpuStatus | null | undefined = null;
@observable
activeDialog: ApplicationDialog = null;
@observable
newsFeedIsOpen = false;
setup() {
this.actions.router.goToRoute.listen(this._updateRouteLocation);
this.actions.app.getGpuStatus.listen(this._getGpuStatus);
this._getGpuStatus();
// About dialog actions
this.actions.app.closeAboutDialog.listen(() => {
this._closeActiveDialog();
});
this.actions.app.openAboutDialog.listen(() => {
// @ts-ignore ts-migrate(2345) FIXME: Argument of type 'string' is not assignable to par... Remove this comment to see the full error message
this._updateActiveDialog(DIALOGS.ABOUT);
});
// Daedalus Diagnostics dialog actions
this.actions.app.closeDaedalusDiagnosticsDialog.listen(() => {
this._closeActiveDialog();
});
this.actions.app.openDaedalusDiagnosticsDialog.listen(() => {
// @ts-ignore ts-migrate(2345) FIXME: Argument of type 'string' is not assignable to par... Remove this comment to see the full error message
this._updateActiveDialog(DIALOGS.DAEDALUS_DIAGNOSTICS);
});
this.actions.app.closeToggleRTSFlagsModeDialog.listen(() => {
this._closeActiveDialog();
});
this.actions.app.openToggleRTSFlagsModeDialog.listen(() => {
// @ts-ignore ts-migrate(2345) FIXME: Argument of type 'string' is not assignable to par... Remove this comment to see the full error message
this._updateActiveDialog(DIALOGS.TOGGLE_RTS_FLAGS_MODE);
});
this.actions.app.downloadLogs.listen(this._downloadLogs);
this.actions.app.setIsDownloadingLogs.listen(this._setIsDownloadingLogs);
this.actions.app.toggleNewsFeed.listen(this._toggleNewsFeed);
this.actions.app.closeNewsFeed.listen(this._closeNewsFeed);
toggleUiPartChannel.onReceive(this.toggleUiPart);
showUiPartChannel.onReceive(this.showUiPart);
}
@computed
get currentRoute(): string {
const { location } = this.stores.router;
return location ? location.pathname : '';
}
@computed
get currentPage(): string {
return this.currentRoute.split('/').pop();
}
openExternalLink(url: string, event?: MouseEvent): void {
if (event) event.preventDefault();
openExternalUrlChannel.send(url);
}
isActiveDialog = (dialog: ApplicationDialog): boolean => {
return this.activeDialog === dialog;
};
@action
_toggleNewsFeed = () => {
this.newsFeedIsOpen = !this.newsFeedIsOpen;
if (this.newsFeedIsOpen) {
this.analytics.sendEvent(EventCategories.LAYOUT, 'Opened newsfeed');
}
};
@action
_closeNewsFeed = () => {
this.newsFeedIsOpen = false;
};
/**
* Toggles the dialog specified by the constant string identifier.
*/
toggleUiPart = (uiPart: string) => {
switch (uiPart) {
default:
}
return Promise.resolve();
};
/**
* Shows the screen specified by the constant string identifier.
*/
showUiPart = (uiPart: string) => {
const { wallets } = this.stores;
switch (uiPart) {
case DIALOGS.ABOUT:
// @ts-ignore ts-migrate(2345) FIXME: Argument of type 'string' is not assignable to par... Remove this comment to see the full error message
this._updateActiveDialog(DIALOGS.ABOUT);
this.analytics.sendEvent(
EventCategories.SYSTEM_MENU,
'Showed about dialog'
);
break;
case DIALOGS.DAEDALUS_DIAGNOSTICS:
// @ts-ignore ts-migrate(2345) FIXME: Argument of type 'string' is not assignable to par... Remove this comment to see the full error message
this._updateActiveDialog(DIALOGS.DAEDALUS_DIAGNOSTICS);
this.analytics.sendEvent(
EventCategories.SYSTEM_MENU,
'Showed diagnostics dialog'
);
break;
case DIALOGS.TOGGLE_RTS_FLAGS_MODE:
// @ts-ignore ts-migrate(2345) FIXME: Argument of type 'string' is not assignable to par... Remove this comment to see the full error message
this._updateActiveDialog(DIALOGS.TOGGLE_RTS_FLAGS_MODE);
this.analytics.sendEvent(
EventCategories.SYSTEM_MENU,
'Showed toggle RTS flags dialog'
);
break;
case DIALOGS.ITN_REWARDS_REDEMPTION:
// @ts-ignore ts-migrate(2554) FIXME: Expected 1 arguments, but got 0.
this.actions.staking.onRedeemStart.trigger();
this.analytics.sendEvent(
EventCategories.SYSTEM_MENU,
'Showed ITN rewards redemption dialog'
);
break;
case NOTIFICATIONS.DOWNLOAD_LOGS:
this._downloadLogs();
this.analytics.sendEvent(
EventCategories.SYSTEM_MENU,
'Downloaded logs'
);
break;
case PAGES.SETTINGS:
this.actions.router.goToRoute.trigger({
route: ROUTES.SETTINGS.ROOT,
});
// @ts-ignore ts-migrate(2554) FIXME: Expected 1 arguments, but got 0.
this.actions.dialogs.closeActiveDialog.trigger();
break;
case PAGES.WALLET_SETTINGS:
if (wallets.active && wallets.active.id) {
this.actions.router.goToRoute.trigger({
route: ROUTES.WALLETS.PAGE,
params: {
id: wallets.active.id,
page: 'settings',
},
});
// @ts-ignore ts-migrate(2554) FIXME: Expected 1 arguments, but got 0.
this.actions.dialogs.closeActiveDialog.trigger();
}
break;
default:
}
return Promise.resolve();
};
@computed
get isSetupPage(): boolean {
return (
this.currentRoute === ROUTES.PROFILE.INITIAL_SETTINGS ||
this.currentRoute === ROUTES.PROFILE.TERMS_OF_USE ||
this.currentRoute === ROUTES.PROFILE.ANALYTICS
);
}
// ===================== PRIVATE ======================= //
_getGpuStatus = async () => {
const gpuStatus = await getGPUStatusChannel.request();
runInAction('get gpu status', () => {
this.gpuStatus = gpuStatus;
});
};
@action
_updateRouteLocation = (options: {
route: string;
params?: Record<string, any> | null | undefined;
}) => {
const newRoutePath = buildRoute(options.route, options.params);
if (this.currentRoute !== newRoutePath) {
this.stores.router.push(newRoutePath);
}
};
@action
_updateActiveDialog = (currentDialog: ApplicationDialog) => {
if (this.newsFeedIsOpen) {
this.newsFeedIsOpen = false;
}
if (this.activeDialog !== currentDialog) this.activeDialog = currentDialog;
};
@action
_closeActiveDialog = () => {
if (this.activeDialog !== null) this.activeDialog = null;
};
@action
_downloadLogs = async () => {
if (this.isDownloadNotificationVisible) {
return;
}
const fileName = generateFileNameWithTimestamp();
const { desktopDirectoryPath } = this.stores.profile;
const defaultPath = path.join(desktopDirectoryPath, fileName);
const params = {
defaultPath,
};
const { filePath } = await showSaveDialogChannel.send(params);
if (filePath) {
this.actions.app.setIsDownloadingLogs.trigger(true);
this.actions.profile.downloadLogs.trigger({
fileName,
destination: filePath,
fresh: true,
});
} else {
this.actions.app.setIsDownloadingLogs.trigger(false);
}
};
@action
_setIsDownloadingLogs = (isDownloadNotificationVisible: boolean) => {
this.isDownloadNotificationVisible = isDownloadNotificationVisible;
};
}