Skip to content

Commit 9b8d0d3

Browse files
committed
fix(sidebar): keep sidebar app activation exclusive after plugin install
1 parent 2c7d05a commit 9b8d0d3

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

src/sidebarApps/index.js

Lines changed: 39 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,13 @@ 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+
setActiveApp(apps[0].id);
60+
return;
61+
}
62+
63+
if (!apps.length) {
64+
currentSection = null;
65+
localStorage.removeItem(SIDEBAR_APPS_LAST_SECTION);
6266
}
6367
}
6468

@@ -121,12 +125,16 @@ function setSponsorSidebarAppVisibility(visible) {
121125
* @returns {void}
122126
*/
123127
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);
128+
const activeApps = apps.filter((app) => app.active);
129+
if (activeApps.length === 1) return;
130+
131+
if (activeApps.length > 1) {
132+
setActiveApp(activeApps[0].id);
133+
return;
134+
}
135+
136+
if (apps.length > 0) {
137+
setActiveApp(apps[0].id);
130138
}
131139
}
132140

@@ -150,11 +158,24 @@ function onclick(e) {
150158

151159
if (action !== "sidebar-app") return;
152160

153-
localStorage.setItem(SIDEBAR_APPS_LAST_SECTION, id);
154-
const activeApp = apps.find((app) => app.active);
161+
setActiveApp(id);
162+
}
163+
164+
/**
165+
* Activates the given sidebar app and deactivates all others.
166+
* @param {string} id
167+
* @returns {void}
168+
*/
169+
function setActiveApp(id) {
155170
const app = apps.find((app) => app.id === id);
156-
if (activeApp) activeApp.active = false;
157-
if (app) app.active = true;
171+
if (!app) return;
172+
173+
currentSection = id;
174+
localStorage.setItem(SIDEBAR_APPS_LAST_SECTION, id);
175+
176+
for (const currentApp of apps) {
177+
currentApp.active = currentApp.id === id;
178+
}
158179
}
159180

160181
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)