Skip to content

Commit 7031927

Browse files
refactor: use existing functions and panels map (#3)
* chore: use this.panels instead of DOM querying * chore: remove debounce functionality - removes isToggling and lastToggleTime * refactor: togglePanel uses panel map * refactor: cleanupOrphanedPanels calls - add cleanUpOrphanedPanels() to initialization - add cleanupOrphanedPanels() to closeAllPanels() * refactor: use existing functions in togglePanel * style: formatting and removing redundant comments --------- Co-authored-by: dustin-jw <dustin@heysparkbox.com>
1 parent 9708806 commit 7031927

File tree

1 file changed

+33
-44
lines changed

1 file changed

+33
-44
lines changed

src/core/ExtensionManager.js

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
// Import Panel class
21
import { Panel } from './Panel.js';
32

4-
// Core extension management class
53
class ExtensionManager {
64
constructor(browserAPI = null) {
75
this.browserAPI = browserAPI || (typeof browser !== 'undefined' ? browser : chrome);
86
this.panels = new Map();
97
this.initialized = false;
10-
this.isToggling = false;
11-
this.lastToggleTime = 0;
8+
9+
// TODO: Set the other panel types in the panels map
10+
this.panels.set('welcome', new Panel('welcome', {}, this.browserAPI));
1211
}
1312

1413
async initialize() {
@@ -26,6 +25,10 @@ class ExtensionManager {
2625
});
2726
}
2827

28+
await this.loadCoreCSS();
29+
30+
this.cleanupOrphanedPanels();
31+
2932
this.initialized = true;
3033
}
3134

@@ -42,53 +45,47 @@ class ExtensionManager {
4245
await this.openPanel(message.panelType || 'welcome', message.data);
4346
break;
4447
default:
45-
4648
break;
4749
}
4850
} catch (error) {
4951
console.error(error);
5052
}
5153
}
5254

55+
async loadCoreCSS() {
56+
if (document.getElementById('carbon-visualizer-core-css')) return;
5357

54-
async togglePanel(panelType) {
55-
const now = Date.now();
56-
57-
if (now - this.lastToggleTime < 500) {
58-
return;
59-
}
60-
61-
if (this.isToggling) {
62-
return;
58+
try {
59+
const cssUrl = this.browserAPI.runtime.getURL('src/styles/core.css');
60+
const response = await fetch(cssUrl);
61+
const css = await response.text();
62+
63+
const style = document.createElement('style');
64+
style.id = 'carbon-visualizer-core-css';
65+
style.textContent = css;
66+
document.head.appendChild(style);
67+
} catch (error) {
68+
console.error(error);
6369
}
70+
}
6471

65-
this.isToggling = true;
66-
this.lastToggleTime = now;
67-
72+
async togglePanel(panelType) {
6873
try {
69-
const existingPanelInDOM = document.querySelector('.cv-panel--welcome');
70-
71-
if (existingPanelInDOM) {
72-
existingPanelInDOM.classList.remove('cv-panel--visible');
73-
// exit animation completes and then remove panel from DOM
74-
setTimeout(() => {
75-
existingPanelInDOM.remove();
76-
}, 300);
74+
const panel = this.panels.get(panelType);
75+
76+
if (panel && panel.isVisible) {
77+
this.closePanel(panelType);
7778
} else {
7879
await this.openPanel(panelType);
7980
}
80-
} finally {
81-
82-
setTimeout(() => {
83-
this.isToggling = false;
84-
}, 100);
81+
} catch (error) {
82+
console.error('Error in togglePanel():', error);
8583
}
8684
}
8785

8886
cleanupOrphanedPanels() {
89-
// Remove any orphaned panels from DOM
9087
const orphanedPanels = document.querySelectorAll('.cv-panel');
91-
orphanedPanels.forEach(panel => {
88+
orphanedPanels.forEach((panel) => {
9289
panel.remove();
9390
});
9491

@@ -103,41 +100,34 @@ class ExtensionManager {
103100
async openPanel(panelType, data = {}) {
104101
let panel = this.panels.get(panelType);
105102

106-
// Always create a fresh panel to avoid state issues
103+
// Create a new panel if one does not already exist
107104
if (!panel) {
108105
panel = new Panel(panelType, data, this.browserAPI);
109106
this.panels.set(panelType, panel);
110107
}
111108

112109
// Always reinitialize to ensure clean state
113110
await panel.initialize();
114-
115-
// Show the panel
116111
await panel.show();
117112
}
118113

119114
closePanel(panelType) {
120115
const panel = this.panels.get(panelType);
121116
if (panel) {
122117
panel.hide();
123-
// Don't delete from map - keep for reuse
124118
}
125119
}
126120

127121
closeAllPanels() {
128-
// Hide all tracked panels immediately
129122
for (const [type, panel] of this.panels) {
130123
if (panel && panel.container) {
131124
panel.hideImmediate();
132125
}
133126
}
134-
this.panels.clear();
135127

136-
// Also remove any orphaned panels from DOM
137-
const existingPanels = document.querySelectorAll('.cv-panel');
138-
existingPanels.forEach(panel => {
139-
panel.remove();
140-
});
128+
// Clear the panels map and remove orphaned panels from DOM
129+
this.panels.clear();
130+
this.cleanupOrphanedPanels();
141131
}
142132

143133
// TODO: Assessment workflow methods will be added later
@@ -146,5 +136,4 @@ class ExtensionManager {
146136
// async showResults(assessmentData) { ... }
147137
}
148138

149-
// Export the class for dynamic imports
150139
export { ExtensionManager };

0 commit comments

Comments
 (0)