Skip to content

Commit 1e9be77

Browse files
committed
Save active app in session
1 parent e4bf9d8 commit 1e9be77

File tree

10 files changed

+18
-37
lines changed

10 files changed

+18
-37
lines changed

src/headless/shared/_converse.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,11 @@ export class ConversePrivateGlobal extends EventEmitter(Object) {
147147
}
148148

149149
initSession () {
150-
this.session?.destroy();
151-
this.session = new Model();
150+
this.state.session?.destroy();
151+
this.state.session = new Model();
152152

153153
// XXX DEPRECATED
154+
this.session = this.state.session;
154155
Object.assign(
155156
this, {
156157
jid: undefined,

src/headless/shared/connection/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Strophe.Status.RECONNECTING = i + 1;
2020
*/
2121
export class Connection extends Strophe.Connection {
2222

23+
/**
24+
* @param {string} service - The BOSH or WebSocket service URL.
25+
* @param {import('strophe.js/src/types/connection').ConnectionOptions} options
26+
*/
2327
constructor (service, options) {
2428
super(service, options);
2529
// For new sessions, we need to send out a presence stanza to notify
@@ -215,6 +219,7 @@ export class Connection extends Strophe.Connection {
215219
const { api } = _converse;
216220

217221
delete this.reconnecting;
222+
218223
this.flush(); // Solves problem of returned PubSub BOSH response not received by browser
219224
await setUserJID(this.jid);
220225

src/headless/utils/init.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ export async function initSession(_converse, jid) {
221221
// Set `active` flag to false when the tab gets reloaded
222222
window.addEventListener(getUnloadEvent(), () => safeSave(_converse.session, { active: false }));
223223

224-
225224
/**
226225
* Triggered once the user's session has been initialized. The session is a
227226
* cache which stores information about the user's current session.

src/plugins/chatboxviews/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ converse.plugins.add('converse-chatboxviews', {
4343
<converse-roster />
4444
</div>`}
4545
`,
46-
active: true,
4746
});
4847

4948
// TODO: move to own plugin

src/plugins/rootview/api.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
1-
import { api } from '@converse/headless';
1+
import { _converse, api } from '@converse/headless';
22

33
const apps = new Map();
44

5-
function setCurrentAppInactive() {
6-
const currentApp = apps_api.apps.getActive();
7-
if (currentApp) {
8-
apps.set(currentApp.name, {
9-
...currentApp,
10-
active: false,
11-
});
12-
}
13-
}
14-
155
const apps_api = {
166
apps: {
177
/**
188
* @param {import('./types').App} app
199
*/
2010
add(app) {
2111
if (!app.name) throw new Error("Can't add app without a name");
22-
if (app.active) setCurrentAppInactive();
2312
apps.set(app.name, app);
2413
},
2514

2615
/**
2716
* @returns {import('./types').App}
2817
*/
2918
getActive() {
30-
return Array.from(apps.values()).find((app) => app.active);
19+
const name = _converse.state.session.get('active_app') ?? 'chat';
20+
const apps_array = Array.from(apps.values());
21+
return apps_array.find((app) => app.name === name) || apps_array[0];
3122
},
3223

3324
/**
3425
* @param {string} name
3526
*/
3627
switch(name) {
3728
if (apps.has(name)) {
38-
setCurrentAppInactive();
39-
apps.set(name, {
40-
...apps.get(name),
41-
active: true,
42-
});
29+
_converse.state.session.save('active_app', name);
4330
const app = apps.get(name);
4431
/**
4532
* Triggered when switching to a different app
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { api } from '@converse/headless';
1+
import { _converse, api } from '@converse/headless';
22
import { CustomElement } from 'shared/components/element.js';
33
import tplAppContainer from './templates/app-container.js';
44
import './app-switcher.js';
55

66
export default class AppContainer extends CustomElement {
77
initialize() {
88
api.listen.on('appSwitch', () => this.requestUpdate());
9+
this.listenTo(_converse.state.connfeedback, 'change:connection_status', () => this.requestUpdate());
910
}
1011

1112
render() {
1213
return tplAppContainer();
1314
}
1415
}
1516

17+
1618
api.elements.define('converse-app-container', AppContainer);

src/plugins/rootview/app-switcher.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@ import tplAppSwitcher from './templates/app-switcher.js';
55
import './styles/app-switcher.scss';
66

77
export default class AppSwitcher extends CustomElement {
8-
static get properties() {
9-
return {
10-
_activeApp: { type: String },
11-
};
12-
}
13-
14-
constructor() {
15-
super();
16-
this._activeApp = 'chat';
17-
}
18-
198
initialize() {
209
api.listen.on('appSwitch', () => this.requestUpdate());
2110
}

src/plugins/rootview/root.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { i18n } from 'i18n';
22
import { _converse, api } from '@converse/headless';
3-
import tplRoot from "./templates/root.js";
43
import { CustomElement } from 'shared/components/element.js';
54
import { getTheme } from './utils.js';
5+
import tplRoot from "./templates/root.js";
66

77
import './styles/root.scss';
88

src/plugins/rootview/templates/app-container.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { api } from '@converse/headless';
33

44
export default () => {
55
return html`
6-
<converse-app-switcher></converse-app-switcher>
6+
${api.connection.get().connected ? html`<converse-app-switcher></converse-app-switcher>` : ''}
77
${api.apps.getActive().render()}
88
`;
99
};

src/plugins/rootview/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ export type App = {
44
name: string;
55
render: () => TemplateResult;
66
renderControlbox?: () => TemplateResult;
7-
active: boolean;
87
};

0 commit comments

Comments
 (0)