Files
threshold-ui/ui/desktop/app/controllers/scopes/scope/projects/sessions/index.js
T
Priya Patel f48e6dc049 Refactor: Load terminal using web contents view (#3190)
* chore: 🤖 create terminal preload

* chore: 🤖 created terminal-manager service and updated handlers

* chore: 🤖 adds terminal view and bundling script

* chore: 🤖 addede terminal service, updated tabs component

* chore: 🤖 hides terminal on signout modal

* test: 💍 added terminal service tests

* chore: 🤖 removed debug file

* chore: 🤖 adds windows event handler

* docs: ✏️ adds jsdoc for terminal view

* chore: 🤖 removed xterm dependency from desktop package.json

* chore: 🤖 updated sessionId check

* chore: 🤖 fixed zoom sync with main window

* chore: 🤖 adds CSP for terminal view

* chore: 🤖 hides terminal on error

* chore: 🤖 cleamup listener, fixed failing tests

* chore: 🤖 fixed terminal view cleanup

* chore: 🤖 addressed feedback

* fix: 🐛 adds null check for destroy, hdie terminal view

* fix: 🐛 fixed terminal view on logout/app quit modal

* fix: 🐛 fixed hidding modal on app quit

* chore: 🤖 addressed feedback

* chore: 🤖 hides terminal view when sidenav is expanded

* chore: 🤖 removed canDisplayExistingTerminal
2026-03-30 10:08:55 -07:00

182 lines
4.3 KiB
JavaScript

/**
* Copyright IBM Corp. 2021, 2026
* SPDX-License-Identifier: BUSL-1.1
*/
import Controller from '@ember/controller';
import { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { notifySuccess, notifyError } from 'core/decorators/notify';
import orderBy from 'lodash/orderBy';
import {
STATUS_SESSION_ACTIVE,
STATUS_SESSION_PENDING,
STATUS_SESSION_CANCELING,
statusTypes,
} from 'api/models/session';
export default class ScopesScopeProjectsSessionsIndexController extends Controller {
// =services
@service intl;
@service store;
@service session;
@service router;
@service abilities;
@service terminal;
// =attributes
queryParams = [
{ targets: { type: 'array' } },
{ status: { type: 'array' } },
{ scopes: { type: 'array' } },
'page',
'pageSize',
'sortAttribute',
'sortDirection',
];
@tracked targets = [];
@tracked status = [
STATUS_SESSION_ACTIVE,
STATUS_SESSION_PENDING,
STATUS_SESSION_CANCELING,
];
@tracked scopes = [];
@tracked page = 1;
@tracked pageSize = 10;
@tracked sortAttribute;
@tracked sortDirection;
// =methods
/**
* A list of sessions sorted by created time then sorted by availability it no sort
* query params are set, otherwise sorted by the specified attribute and direction.
* @type {[SessionModel]}
*/
get sortedSessions() {
if (this.sortAttribute && this.sortDirection) {
return this.model.sessions;
}
return orderBy(
this.model.sessions,
['isAvailable', 'created_time'],
['desc', 'desc'],
);
}
/**
* Returns all status types for sessions
* @returns {[object]}
*/
get sessionStatusOptions() {
return statusTypes.map((status) => ({
id: status,
name: this.intl.t(`resources.session.status.${status}`),
}));
}
/**
* Returns scopes that are associated with all sessions the user has access to
* @returns {[ScopeModel]}
*/
get availableScopes() {
const uniqueSessionScopeIds = new Set(
this.model.allSessions.map((session) => session.scope.id),
);
return this.model.projects.filter((project) =>
uniqueSessionScopeIds.has(project.id),
);
}
/**
* Returns object of filters to be used for displaying selected filters
* @returns {object}
*/
get filters() {
return {
allFilters: {
targets: this.model.associatedTargets,
status: this.sessionStatusOptions,
scopes: this.availableScopes,
},
selectedFilters: {
targets: this.targets,
status: this.status,
scopes: this.scopes,
},
};
}
// =actions
/**
* Sets the query params to value of selectedItems
* to trigger a query and closes the dropdown
* @param {string} filter
* @param {object} selectedItems
*/
@action
applyFilter(filter, selectedItems) {
this[filter] = [...selectedItems];
this.page = 1;
}
/**
* Sets sort values and sets page to 1
* @param {string} sortBy
* @param {string} sortOrder
*/
@action
onSort(sortBy, sortOrder) {
this.sortAttribute = sortBy;
this.sortDirection = sortOrder;
this.page = 1;
}
/**
* Cancels the specified session and notifies user of success or error.
* @param {SessionModel}
*/
@action
@loading
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.canceled-success')
async cancelSession(session) {
let updatedSession = session;
// fetch session from API to verify we have most up to date record
if (this.abilities.can('read session', session)) {
updatedSession = await this.store.findRecord('session', session.id, {
reload: true,
});
}
try {
await updatedSession.cancelSession();
await window.desktop.session.stopSession({ session_id: session.id });
if (
this.router.currentRoute.name ===
'scopes.scope.projects.sessions.session.index'
) {
this.router.replaceWith('scopes.scope.projects.targets.index');
}
} catch (e) {
// hide terminal view
this.terminal.hideTerminalView();
throw e;
}
}
/**
* Refreshes the all data for the current page.
*/
@action
async refresh() {
await this.currentRoute.refreshAll();
}
}