Skip to content

Commit bef17a8

Browse files
Copilotrajbos
andcommitted
Refactor: Extract tab activation into helper function
Co-authored-by: rajbos <6085745+rajbos@users.noreply.github.com>
1 parent 0e7230f commit bef17a8

1 file changed

Lines changed: 21 additions & 23 deletions

File tree

src/webview/diagnostics/main.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -774,22 +774,31 @@ function renderLayout(data: DiagnosticsData): void {
774774
}
775775
});
776776

777+
// Helper function to activate a tab by its ID
778+
function activateTab(tabId: string): boolean {
779+
const tabButton = document.querySelector(`.tab[data-tab="${tabId}"]`);
780+
const tabContent = document.getElementById(`tab-${tabId}`);
781+
782+
if (tabButton && tabContent) {
783+
// Remove active class from all tabs and contents
784+
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
785+
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
786+
787+
// Activate the specified tab
788+
tabButton.classList.add('active');
789+
tabContent.classList.add('active');
790+
return true;
791+
}
792+
return false;
793+
}
794+
777795
// Wire up tab switching
778796
document.querySelectorAll('.tab').forEach(tab => {
779797
tab.addEventListener('click', () => {
780798
const tabId = (tab as HTMLElement).getAttribute('data-tab');
781799

782-
// Update active tab
783-
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
784-
tab.classList.add('active');
785-
786-
// Update active content
787-
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
788-
const content = document.getElementById(`tab-${tabId}`);
789-
if (content) { content.classList.add('active'); }
790-
791-
// Save the active tab state
792-
if (tabId) {
800+
if (tabId && activateTab(tabId)) {
801+
// Save the active tab state
793802
vscode.setState({ activeTab: tabId });
794803
}
795804
});
@@ -947,18 +956,7 @@ function renderLayout(data: DiagnosticsData): void {
947956
// Restore active tab from saved state
948957
const savedState = vscode.getState();
949958
if (savedState?.activeTab) {
950-
const tabToActivate = document.querySelector(`.tab[data-tab="${savedState.activeTab}"]`);
951-
const contentToActivate = document.getElementById(`tab-${savedState.activeTab}`);
952-
953-
if (tabToActivate && contentToActivate) {
954-
// Remove active class from all tabs and contents
955-
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
956-
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
957-
958-
// Activate the saved tab
959-
tabToActivate.classList.add('active');
960-
contentToActivate.classList.add('active');
961-
}
959+
activateTab(savedState.activeTab);
962960
}
963961
}
964962

0 commit comments

Comments
 (0)