-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAppContainerModel.ts
More file actions
426 lines (379 loc) · 15.6 KB
/
Copy pathAppContainerModel.ts
File metadata and controls
426 lines (379 loc) · 15.6 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* This file belongs to Hoist, an application development toolkit
* developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
*
* Copyright © 2026 Extremely Heavy Industries Inc.
*/
import {
AppSpec,
AppState,
CallContext,
createElement,
HoistAppModel,
HoistModel,
IdentityInfo,
InitContext,
managed,
RootRefreshContextModel,
TaskObserver,
XH
} from '@xh/hoist/core';
import {Icon} from '@xh/hoist/icon';
import {bindable, makeObservable, when as mobxWhen} from '@xh/hoist/mobx';
import {never, wait} from '@xh/hoist/promise';
import {ReactNode} from 'react';
import {createRoot} from 'react-dom/client';
import {
AlertBannerService,
AutoRefreshService,
ChangelogService,
ClientHealthService,
ConfigService,
EnvironmentService,
FetchService,
GridAutosizeService,
GridExportService,
IdentityService,
IdleService,
InspectorService,
JsonBlobService,
LocalStorageService,
MetricsService,
PrefService,
SessionStorageService,
TrackService,
TraceService,
WebSocketService
} from '@xh/hoist/svc';
import {createSingleton, throwIf} from '@xh/hoist/utils/js';
import {compact, isEmpty} from 'lodash';
import {AboutDialogModel} from './AboutDialogModel';
import {BannerSourceModel} from './BannerSourceModel';
import {ChangelogDialogModel} from './ChangelogDialogModel';
import {ExceptionDialogModel} from './ExceptionDialogModel';
import {FeedbackDialogModel} from './FeedbackDialogModel';
import {ImpersonationBarModel} from './ImpersonationBarModel';
import {MessageSourceModel} from './MessageSourceModel';
import {OptionsDialogModel} from './OptionsDialogModel';
import {SizingModeModel} from './SizingModeModel';
import {ViewportSizeModel} from './ViewportSizeModel';
import {ThemeModel} from './ThemeModel';
import {ToastSourceModel} from './ToastSourceModel';
import {BannerModel} from './BannerModel';
import {UserAgentModel} from './UserAgentModel';
import {AppStateModel} from './AppStateModel';
import {PageStateModel} from './PageStateModel';
import {RouterModel} from './RouterModel';
import {installServicesAsync} from '../core/impl/InstallServices';
/**
* Root object for Framework GUI State.
*/
export class AppContainerModel extends HoistModel {
override telemetryPrefix = 'xh.client';
private initCalled = false;
//---------------------------------
// Immutable Application State
//--------------------------------
appSpec: AppSpec = null;
appModel: HoistAppModel = null;
//------------
// Sub-models
//------------
@managed appLoadObserver = TaskObserver.trackAll();
@managed appStateModel = new AppStateModel();
@managed pageStateModel = new PageStateModel();
@managed routerModel = new RouterModel();
@managed aboutDialogModel = new AboutDialogModel();
@managed changelogDialogModel = new ChangelogDialogModel();
@managed exceptionDialogModel = new ExceptionDialogModel();
@managed feedbackDialogModel = new FeedbackDialogModel();
@managed impersonationBarModel = new ImpersonationBarModel();
@managed optionsDialogModel = new OptionsDialogModel();
@managed bannerSourceModel = new BannerSourceModel();
@managed messageSourceModel = new MessageSourceModel();
@managed toastSourceModel = new ToastSourceModel();
@managed refreshContextModel = new RootRefreshContextModel();
@managed sizingModeModel = new SizingModeModel();
@managed viewportSizeModel = new ViewportSizeModel();
@managed themeModel = new ThemeModel();
@managed userAgentModel = new UserAgentModel();
/**
* Message shown on spinner while the application is in a pre-running state.
* Update within `AppModel.initAsync()` to relay app-specific initialization status.
*/
@bindable initializingLoadMaskMessage: ReactNode;
/**
* The last interactive login in the app. Hoist's security package will mark the last
* time spent during user interactive login.
*
* Used by `Promise.track`, to ensure this time is not counted in any elapsed time tracking
* for the app.
* @internal
*/
lastRelogin: {started: number; completed: number} = null;
constructor() {
super();
makeObservable(this);
}
/**
* Main entry point. Initialize and render application code.
*/
renderApp<T extends HoistAppModel>(appSpec: AppSpec<T>) {
// Remove the preload exception handler installed by preflight.js
window.onerror = null;
const spinner = document.getElementById('xh-preload-spinner');
if (spinner) spinner.style.display = 'none';
this.appSpec = appSpec instanceof AppSpec ? appSpec : new AppSpec(appSpec);
const root = createRoot(document.getElementById('xh-root')),
rootView = createElement(appSpec.containerClass, {model: this});
root.render(rootView);
}
/**
* Called when {@link AppContainer} first mounted.
* Triggers initial authentication and initialization of Hoist and application.
*/
async initAsync() {
// Avoid bug where "Discarded" browser tabs can re-init an old version (see #3574)
if (window.document['wasDiscarded']) {
XH.reloadApp();
return never();
}
// Avoid multiple calls, which can occur if AppContainer remounted.
if (this.initCalled) return;
this.initCalled = true;
try {
// Install TraceService first so booting traceable; it will defer sampling and export until config available
await installServicesAsync([TraceService], {span: null});
await this.runner()
.span('load')
.run(async ctx => {
this.addGlobalListenersAndCss();
await installServicesAsync([FetchService], {span: ctx.span});
// Check auth, falling through to interactive login if SSO returns no identity.
this.setAppState('AUTHENTICATING');
XH.authModel = createSingleton(this.appSpec.authModelClass);
let identity = await this.runner(ctx)
.span('auth')
.run(ctx => XH.authModel.completeAuthAsync(ctx));
if (!identity) {
throwIf(
!this.appSpec.enableLoginForm,
'Unable to complete required authentication (SSO/Auth failure).'
);
this.setAppState('LOGIN_REQUIRED');
identity = await this.runner(ctx)
.span('interactiveLogin')
.run(() => this.awaitInteractiveLoginAsync());
}
await this.completeInitAsync(identity, ctx);
});
} catch (e) {
this.setAppState('LOAD_FAILED');
XH.handleException(e, {requireReload: true});
return;
}
}
/**
* Called by {@link LoginPanelModel} on successful credential submission to resume startup.
* @internal
*/
completeInteractiveLogin(identity: IdentityInfo) {
this.interactiveLoginDeferred?.resolve(identity);
this.interactiveLoginDeferred = null;
}
/**
* Show the update Banner. Called by EnvironmentService when the server reports that a
* new (or at least different) version is available and the user should be prompted.
*
* @param version - updated version from server.
* @param build - updated build from server - included for snapshot version prompts.
*/
showUpdateBanner(version: string, build?: string) {
// Display build tag for snaps only - not of much interest across actual version updates.
if (version.includes('SNAPSHOT') && build && build !== 'UNKNOWN') {
version += ` (b${build})`;
}
// Show Banner
const mobile = XH.isMobileApp,
message = mobile
? 'Update available!'
: `A new version of ${XH.clientAppName} is available!`,
buttonText = mobile ? version : `Update to ${version}`;
XH.showBanner({
category: 'xhAppUpdate',
message,
icon: Icon.rocket({size: 'lg'}),
intent: 'warning',
sortOrder: BannerModel.BANNER_SORTS.APP_UPDATE,
enableClose: false,
actionButtonProps: {
icon: Icon.refresh(),
text: buttonText,
onClick: () => XH.reloadApp()
}
});
}
hasAboutDialog() {
return !isEmpty(this.aboutDialogModel.getItems());
}
openAdmin() {
XH.openWindow('/admin', XH.appCode + '_xhAdmin');
}
//----------------------------
// Implementation
//-----------------------------
private interactiveLoginDeferred: {
resolve: (identity: IdentityInfo) => void;
reject: (err: unknown) => void;
} = null;
private awaitInteractiveLoginAsync(): Promise<IdentityInfo> {
return new Promise((resolve, reject) => {
this.interactiveLoginDeferred = {resolve, reject};
});
}
/**
* Complete initialization. Called after the client has confirmed that the user is generally
* authenticated and known to the server (regardless of application roles at this point).
*/
private async completeInitAsync(identity: IdentityInfo, ctx: CallContext) {
// Install identity and check roles
await installServicesAsync(IdentityService, ctx as InitContext);
XH.identityService.initIdentity(identity);
if (!this.appStateModel.checkAccess()) {
this.setAppState('ACCESS_DENIED');
return;
}
// Hoist init phase
this.setAppState('INITIALIZING_HOIST');
await this.runner(ctx)
.span('hoistInit')
.run(async ctx => {
await installServicesAsync(
[LocalStorageService, SessionStorageService],
ctx as InitContext
);
await installServicesAsync(
[EnvironmentService, ConfigService, PrefService, JsonBlobService],
ctx as InitContext
);
XH.traceService.noteConfigAvailable();
await installServicesAsync([TrackService, MetricsService], ctx as InitContext);
await installServicesAsync(
[
AlertBannerService,
AutoRefreshService,
ChangelogService,
ClientHealthService,
IdleService,
InspectorService,
GridAutosizeService,
GridExportService,
WebSocketService
],
ctx as InitContext
);
// init all models other than Router
const models = [
this.appLoadObserver,
this.appStateModel,
this.pageStateModel,
this.routerModel,
this.aboutDialogModel,
this.changelogDialogModel,
this.exceptionDialogModel,
this.feedbackDialogModel,
this.impersonationBarModel,
this.optionsDialogModel,
this.bannerSourceModel,
this.messageSourceModel,
this.toastSourceModel,
this.refreshContextModel,
this.sizingModeModel,
this.viewportSizeModel,
this.themeModel,
this.userAgentModel
];
models.forEach((m: any) => m.init?.());
this.bindInitSequenceToAppLoadObserver();
this.setDocTitle();
// Delay to workaround hot-reload styling issues in dev.
await wait(XH.isDevelopmentMode ? 300 : 1);
});
// App init phase
this.setAppState('INITIALIZING_APP');
this.appModel = createSingleton(this.appSpec.modelClass);
await this.runner(ctx)
.span('appInit')
.run(ctx => this.appModel.initAsync(ctx as InitContext));
this.startRouter();
this.startOptionsDialog();
this.setAppState('RUNNING');
}
private addGlobalListenersAndCss() {
const {isPhone, isTablet, isDesktop} = this.userAgentModel,
{isMobileApp} = this.appSpec;
// Add xh css classes to power Hoist CSS selectors.
document.body.classList.add(
...compact([
'xh-app',
isMobileApp ? 'xh-mobile' : 'xh-standard',
isDesktop ? 'xh-desktop' : null,
isPhone ? 'xh-phone' : null,
isTablet ? 'xh-tablet' : null
])
);
if (isMobileApp) {
// Disable browser context menu on long-press, used to show (app) context menus and as an
// alternate gesture for tree grid drill-own.
window.addEventListener('contextmenu', e => e.preventDefault(), {capture: true});
// Spec viewport-fit=cover to allow use of safe-area-inset envs for mobile styling
// (e.g. `env(safe-area-inset-top)`). This allows us to avoid overlap with OS-level
// controls like the iOS tab switcher, as well as to more easily set the background
// color of the (effectively) unusable portions of the screen via
this.setViewportContent(this.getViewportContent() + ', viewport-fit=cover');
// Temporarily set maximum-scale=1 on orientation change to force reset Safari iOS
// zoom level, and then remove to restore user zooming. This is a workaround for a bug
// where Safari full-screen re-zooms on orientation change if user has *ever* zoomed.
window.addEventListener(
'orientationchange',
() => {
const content = this.getViewportContent();
this.setViewportContent(content + ', maximum-scale=1');
setTimeout(() => this.setViewportContent(content), 0);
},
false
);
}
}
private setDocTitle() {
const env = XH.getEnv('appEnvironment'),
{clientAppName} = this.appSpec;
document.title = env === 'Production' ? clientAppName : `${clientAppName} (${env})`;
}
private startRouter() {
const routes = this.appModel.getRoutes(),
defaultRoute = routes.length ? routes[0].name : null;
this.routerModel.addRoutes(routes);
this.routerModel.router.setOption('defaultRoute', defaultRoute);
this.routerModel.router.start();
}
private startOptionsDialog() {
this.optionsDialogModel.setOptions(this.appModel.getAppOptions());
}
private setAppState(nextState: AppState) {
this.appStateModel.setAppState(nextState);
}
private bindInitSequenceToAppLoadObserver() {
const terminalStates: AppState[] = ['RUNNING', 'SUSPENDED', 'LOAD_FAILED', 'ACCESS_DENIED'],
loadingPromise = mobxWhen(() => terminalStates.includes(this.appStateModel.state));
loadingPromise.linkTo(this.appLoadObserver);
}
private setViewportContent(content: string) {
const vp = document.querySelector('meta[name=viewport]');
vp?.setAttribute('content', content);
}
private getViewportContent(): string {
const vp = document.querySelector('meta[name=viewport]');
return vp ? vp.getAttribute('content') : '';
}
}