+ public getSessions(): Session[] {
+ try {
+ const sessions = JSON.parse(this.getStorage().getItem("sessions") || '');
+ return sessions;
+ } catch {
+ return [];
+ }
+ }
+
+ public saveSessions(sessions: Session[]) {
+ this.removeSessions();
+ this.getStorage().setItem("sessions", JSON.stringify(sessions));
+ }
+
+ public removeSessions() {
+ localStorage.removeItem("sessions");
+ sessionStorage.removeItem("sessions");
+ }
+
+ public buildSessions(cfg: Config, user?: User) {
+ const currentSession = {
+ clusterId: cfg.uuidPrefix,
+ remoteHost: cfg.rootUrl,
+ baseUrl: cfg.baseUrl,
+ name: user ? getUserDisplayName(user) : '',
+ email: user ? user.email : '',
+ userIsActive: user ? user.isActive : false,
+ token: this.getApiToken(),
+ loggedIn: true,
+ active: true,
+ uuid: user ? user.uuid : '',
+ status: SessionStatus.VALIDATED,
+ apiRevision: cfg.apiRevision,
+ } as Session;
+ const localSessions = this.getSessions().map(s => ({
+ ...s,
+ active: false,
+ status: SessionStatus.INVALIDATED
+ }));
+
+ const cfgSessions = Object.keys(cfg.remoteHosts).map(clusterId => {
+ const remoteHost = cfg.remoteHosts[clusterId];
+ return {
+ clusterId,
+ remoteHost,
+ baseUrl: '',
+ name: '',
+ email: '',
+ token: '',
+ loggedIn: false,
+ active: false,
+ uuid: '',
+ status: SessionStatus.INVALIDATED,
+ apiRevision: 0,
+ } as Session;
+ });
+ const sessions = [currentSession]
+ .concat(cfgSessions)
+ .concat(localSessions)
+ .filter((r: Session) => r.clusterId !== "*");
+
+ const uniqSessions = uniqBy(sessions, 'clusterId');
+
+ return uniqSessions;