Skip to content

Commit a767053

Browse files
authored
fix: sidebar apps active and removal things with fallback (Acode-Foundation#1749)
1 parent 45d286f commit a767053

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

src/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ async function onDeviceReady() {
231231
// load plugins
232232
try {
233233
await loadPlugins();
234+
// Ensure at least one sidebar app is active after all plugins are loaded
235+
// This handles cases where the stored section was from an uninstalled plugin
236+
sidebarApps.ensureActiveApp();
234237

235238
// Re-emit events for active file after plugins are loaded
236239
const { activeFile } = editorManager;

src/sidebarApps/index.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ function add(
4848
function remove(id) {
4949
const app = apps.find((app) => app.id === id);
5050
if (!app) return;
51+
const wasActive = app.active;
5152
app.remove();
5253
apps.splice(apps.indexOf(app), 1);
53-
if (app.active) {
54+
if (wasActive && apps.length > 0) {
5455
const firstApp = apps[0];
5556
firstApp.active = true;
57+
currentSection = firstApp.id;
58+
localStorage.setItem(SIDEBAR_APPS_LAST_SECTION, firstApp.id);
5659
}
5760
}
5861

@@ -78,6 +81,22 @@ async function loadApps() {
7881
$apps.append(<span className="icon favorite" onclick={Sponsors} />);
7982
}
8083

84+
/**
85+
* Ensures that at least one app is active.
86+
* Call this AFTER all plugins have been loaded to handle cases where
87+
* the stored section was from an uninstalled plugin.
88+
* @returns {void}
89+
*/
90+
function ensureActiveApp() {
91+
const hasActiveApp = apps.some((app) => app.active);
92+
if (!hasActiveApp && apps.length > 0) {
93+
const firstApp = apps[0];
94+
firstApp.active = true;
95+
currentSection = firstApp.id;
96+
localStorage.setItem(SIDEBAR_APPS_LAST_SECTION, firstApp.id);
97+
}
98+
}
99+
81100
/**
82101
* Gets the container of the app with the given ID.
83102
* @param {string} id
@@ -101,8 +120,8 @@ function onclick(e) {
101120
localStorage.setItem(SIDEBAR_APPS_LAST_SECTION, id);
102121
const activeApp = apps.find((app) => app.active);
103122
const app = apps.find((app) => app.id === id);
104-
activeApp.active = false;
105-
app.active = true;
123+
if (activeApp) activeApp.active = false;
124+
if (app) app.active = true;
106125
}
107126

108127
export default {
@@ -111,4 +130,5 @@ export default {
111130
get,
112131
remove,
113132
loadApps,
133+
ensureActiveApp,
114134
};

src/sidebarApps/sidebarApp.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,29 @@ export default class SidebarApp {
8989
this.#active = !!value;
9090
this.#icon.classList.toggle("active", this.#active);
9191
if (this.#active) {
92-
const child = getContainer(this.#container);
93-
$sidebar.replaceChild($contaienr, child);
92+
const oldContainer = getContainer(this.#container);
93+
// Try to replace the old container, or append if it's not in the DOM
94+
try {
95+
if (oldContainer && oldContainer.parentNode === $sidebar) {
96+
$sidebar.replaceChild($contaienr, oldContainer);
97+
} else {
98+
// Old container not in sidebar, just append the new one
99+
const existingContainer = $sidebar.get(".container");
100+
if (existingContainer) {
101+
$sidebar.replaceChild($contaienr, existingContainer);
102+
} else {
103+
$sidebar.appendChild($contaienr);
104+
}
105+
}
106+
} catch (error) {
107+
// Fallback: append the new container
108+
console.warn("Error switching sidebar container:", error);
109+
const existingContainer = $sidebar.get(".container");
110+
if (existingContainer) {
111+
existingContainer.remove();
112+
}
113+
$sidebar.appendChild($contaienr);
114+
}
94115
this.#onselect(this.#container);
95116
}
96117
}
@@ -111,10 +132,14 @@ export default class SidebarApp {
111132
}
112133

113134
remove() {
114-
this.#icon.remove();
115-
this.#container.remove();
116-
this.#icon = null;
117-
this.#container = null;
135+
if (this.#icon) {
136+
this.#icon.remove();
137+
this.#icon = null;
138+
}
139+
if (this.#container) {
140+
this.#container.remove();
141+
this.#container = null;
142+
}
118143
}
119144
}
120145

0 commit comments

Comments
 (0)