Files
threshold-ui/ui/desktop/app/controllers/application.js
T
LaurenandZhihe Li 990e2d6b9d [Desktop][Bug fix]: Stop target sessions before unauthenticating user session (#2891)
* add stopAll invocation to ipc handler

* trigger modal on both signout btns

* refactor invalidateSession fn

* fix application controller tests

* fix failing tests settings-card/user

* add test to Acceptance | authentication

* migrate electron dialog.showMessageBoxSync to Hds

* rename channel and tracked prop, consolidate cancel fns

* combine modals, add willDestroy, refactor

* test signout and close sessions modal

* address feedback

* refactor window.electron in authentication-test

* address feedback

* refactor: 💡 Fix failing tests

---------

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>
2025-08-04 16:44:27 -04:00

131 lines
3.1 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Controller from '@ember/controller';
import { service } from '@ember/service';
import { getOwner } from '@ember/application';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
export default class ApplicationController extends Controller {
// =services
@service clusterUrl;
@service flashMessages;
@service ipc;
@service session;
@service('browser/window') window;
@tracked isLoggingOut = false;
@tracked isAppQuitting = false;
// =attributes
removeOnAppQuitListener;
constructor() {
super(...arguments);
// Listen for when user attempts to quit app
// Setup removeOnAppQuitListener to destroy the listener afterwards
this.removeOnAppQuitListener = this.window.electron?.onAppQuit(() => {
this.isAppQuitting = true;
});
}
// =actions
/**
* Stop all active/pending target sessions
* Logout or close app
*/
@action
async confirmCloseSessions() {
await this.ipc.invoke('stopAll');
if (this.isAppQuitting) {
this.isAppQuitting = false;
this.close();
} else {
// this.session.invalidate() comes from Ember Simple Auth BaseSessionService
this.session.invalidate();
}
this.isLoggingOut = false;
}
/**
* Only renders the signout modal if target sessions are running
*/
@action
async showModalOrLogout() {
const hasRunningSessions = await this.ipc.invoke('hasRunningSessions');
if (hasRunningSessions) {
this.isLoggingOut = true;
} else {
this.session.invalidate();
}
}
/**
* Disconnects from clusterUrl and invalidates session, thereby resetting
* the client and reloading to the onboarding clusterUrl screen.
*/
@action
disconnect() {
this.clusterUrl.resetClusterUrl();
this.session.invalidate();
}
@action
minimize() {
this.ipc.invoke('minimizeWindow');
}
@action
toggleFullScreen() {
this.ipc.invoke('toggleFullscreenWindow');
}
@action
close() {
this.ipc.invoke('closeWindow');
}
/**
* Applies the specified color theme to the root ember element.
* @param {string} theme - "light", "dark", or nullish (system default)
*/
@action
toggleTheme(theme) {
const rootElementSelector = getOwner(this).rootElement;
const rootEl = getOwner(this)
.lookup('service:-document')
.querySelector(rootElementSelector);
this.session.set('data.theme', theme);
switch (theme) {
case 'light':
rootEl.classList.add('rose-theme-light');
rootEl.classList.remove('rose-theme-dark');
break;
case 'dark':
rootEl.classList.add('rose-theme-dark');
rootEl.classList.remove('rose-theme-light');
break;
default:
rootEl.classList.remove('rose-theme-dark');
rootEl.classList.remove('rose-theme-light');
}
}
@action
cancel() {
this.isLoggingOut = false;
this.isAppQuitting = false;
}
willDestroy() {
super.willDestroy(...arguments);
this.removeOnAppQuitListener?.();
}
}