Files
threshold-ui/ui/desktop/app/routes/application.js
T
Zhihe Li 7dadc53fe1 Remove renderer url (#3194)
* feat: 🎸 Refactor how we store cluster URL
2026-05-20 16:36:16 -04:00

98 lines
2.5 KiB
JavaScript

/**
* Copyright IBM Corp. 2021, 2026
* SPDX-License-Identifier: BUSL-1.1
*/
import Route from '@ember/routing/route';
import { service } from '@ember/service';
import { action } from '@ember/object';
import { A } from '@ember/array';
import { notifyError } from 'core/decorators/notify';
export default class ApplicationRoute extends Route {
// =services
@service session;
@service clusterUrl;
@service intl;
@service store;
@service rdp;
@service terminal;
// =attributes
/**
* @type {string}
*/
routeIfUnauthenticated = 'index';
@notifyError(({ message }) => message, {
catch: true,
log: { origin: 'ApplicationRoute:beforeModel' },
})
async beforeModel() {
this.intl.setLocale(['en-us']);
const clusterUrl = await this.clusterUrl.getClusterUrl();
if (clusterUrl) {
const adapter = this.store.adapterFor('application');
adapter.host = clusterUrl;
}
await this.session.setup();
const theme = this.session.get('data.theme');
/* eslint-disable-next-line ember/no-controller-access-in-routes */
const controller = this.controllerFor(this.routeName);
controller.toggleTheme(theme);
// Add token to cache daemon after a successful authentication restoration
if (this.session.isAuthenticated) {
const sessionData = this.session.data?.authenticated;
await window.desktop.daemon.addTokenToDaemons({
tokenId: sessionData?.id,
token: sessionData?.token,
});
await this.session.loadAuthenticatedAccount();
}
// initialize RDP service with rdp client data
await this.rdp.initialize();
}
/**
* Add window frame config on controller.
*/
async setupController(controller) {
controller.set(
'hasMacOSChrome',
await window.desktop.windowAction.hasMacOSChrome(),
);
controller.set(
'showWindowActions',
await window.desktop.windowAction.showWindowActions(),
);
}
// =actions
/**
* Invalidates the session if a 401 error occurs and returns false to
* prevent further error handling.
* Returns true in all other cases, allowing error handling to occur (such
* as displaying the `error.hbs` template, if one exists).
* @param {Error} e
*/
@action
error(e) {
const isUnauthenticated = A(e?.errors)?.[0]?.isUnauthenticated;
// clean up terminal view for all errors
this.terminal.cleanup();
if (isUnauthenticated) {
this.session.invalidate();
return false;
}
return true;
}
}