Skip to content

Commit d79a906

Browse files
authored
fix(sidebar): keep sidebar app activation exclusive after plugin install (#1932)
1 parent f3f3147 commit d79a906

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

src/sidebarApps/index.js

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ function add(
3535
) {
3636
currentSection ??= id;
3737

38-
const active = currentSection === id;
3938
const app = new SidebarApp(icon, id, title, initFunction, onSelected);
40-
41-
app.active = active;
42-
app.install(prepend);
4339
apps.push(app);
40+
app.install(prepend);
41+
42+
if (currentSection === id) {
43+
setActiveApp(id);
44+
}
4445
}
4546

4647
/**
@@ -55,10 +56,14 @@ function remove(id) {
5556
app.remove();
5657
apps.splice(apps.indexOf(app), 1);
5758
if (wasActive && apps.length > 0) {
58-
const firstApp = apps[0];
59-
firstApp.active = true;
60-
currentSection = firstApp.id;
61-
localStorage.setItem(SIDEBAR_APPS_LAST_SECTION, firstApp.id);
59+
const preferredApp = apps.find((app) => app.id === currentSection);
60+
setActiveApp(preferredApp?.id || apps[0].id);
61+
return;
62+
}
63+
64+
if (!apps.length) {
65+
currentSection = null;
66+
localStorage.removeItem(SIDEBAR_APPS_LAST_SECTION);
6267
}
6368
}
6469

@@ -121,12 +126,20 @@ function setSponsorSidebarAppVisibility(visible) {
121126
* @returns {void}
122127
*/
123128
function ensureActiveApp() {
124-
const hasActiveApp = apps.some((app) => app.active);
125-
if (!hasActiveApp && apps.length > 0) {
126-
const firstApp = apps[0];
127-
firstApp.active = true;
128-
currentSection = firstApp.id;
129-
localStorage.setItem(SIDEBAR_APPS_LAST_SECTION, firstApp.id);
129+
const activeApps = apps.filter((app) => app.active);
130+
if (activeApps.length === 1) return;
131+
132+
if (activeApps.length > 1) {
133+
const preferredActiveApp = activeApps.find(
134+
(app) => app.id === currentSection,
135+
);
136+
setActiveApp(preferredActiveApp?.id || activeApps[0].id);
137+
return;
138+
}
139+
140+
if (apps.length > 0) {
141+
const preferredApp = apps.find((app) => app.id === currentSection);
142+
setActiveApp(preferredApp?.id || apps[0].id);
130143
}
131144
}
132145

@@ -150,11 +163,24 @@ function onclick(e) {
150163

151164
if (action !== "sidebar-app") return;
152165

153-
localStorage.setItem(SIDEBAR_APPS_LAST_SECTION, id);
154-
const activeApp = apps.find((app) => app.active);
166+
setActiveApp(id);
167+
}
168+
169+
/**
170+
* Activates the given sidebar app and deactivates all others.
171+
* @param {string} id
172+
* @returns {void}
173+
*/
174+
function setActiveApp(id) {
155175
const app = apps.find((app) => app.id === id);
156-
if (activeApp) activeApp.active = false;
157-
if (app) app.active = true;
176+
if (!app) return;
177+
178+
currentSection = id;
179+
localStorage.setItem(SIDEBAR_APPS_LAST_SECTION, id);
180+
181+
for (const currentApp of apps) {
182+
currentApp.active = currentApp.id === id;
183+
}
158184
}
159185

160186
export default {

src/sidebarApps/sidebarApp.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ export default class SidebarApp {
8686

8787
/**@param {boolean} value */
8888
set active(value) {
89-
this.#active = !!value;
89+
const nextValue = !!value;
90+
if (this.#active === nextValue) return;
91+
92+
this.#active = nextValue;
9093
this.#icon.classList.toggle("active", this.#active);
9194
if (this.#active) {
9295
const oldContainer = getContainer(this.#container);

0 commit comments

Comments
 (0)