11// src/components/sidebar-controller.js
22// Controls right sidebar visibility and tab switching
33
4+ import { getActiveDocumentId , onDocumentChange } from "../document-manager.js" ;
5+
46let rightSidebar = null ;
57
8+ // Store sidebar state per document
9+ // Map<documentId, { visible: boolean, activeTab: string }>
10+ const documentSidebarStates = new Map ( ) ;
11+
12+ // Default state
13+ const DEFAULT_STATE = {
14+ visible : false ,
15+ activeTab : 'timeline'
16+ } ;
17+
618/**
719 * Initialize the sidebar controller
820 */
@@ -16,6 +28,16 @@ export function initSidebarController() {
1628
1729 // Initialize tab switching
1830 initSidebarTabs ( ) ;
31+
32+ // Listen for active document changes to restore state
33+ onDocumentChange ( ( event , doc ) => {
34+ if ( event === "activeChange" && doc ) {
35+ restoreSidebarState ( doc . id ) ;
36+ } else if ( event === "close" && doc ) {
37+ // Clean up state when document is closed
38+ documentSidebarStates . delete ( doc . id ) ;
39+ }
40+ } ) ;
1941}
2042
2143/**
@@ -33,10 +55,55 @@ function initSidebarTabs() {
3355}
3456
3557/**
36- * Switch the active sidebar tab
37- * @param {string } tabId - The tab ID to switch to
58+ * Save current state for active document
3859 */
39- export function switchSidebarTab ( tabId ) {
60+ function saveCurrentState ( ) {
61+ const docId = getActiveDocumentId ( ) ;
62+ if ( ! docId || ! rightSidebar ) return ;
63+
64+ const visible = ! rightSidebar . classList . contains ( 'hidden' ) ;
65+ const activeTabEl = document . querySelector ( '.sidebar-tab.active' ) ;
66+ const activeTab = activeTabEl ? activeTabEl . dataset . tab : 'timeline' ;
67+
68+ documentSidebarStates . set ( docId , { visible, activeTab } ) ;
69+ }
70+
71+ /**
72+ * Restore state for a document
73+ * @param {string } docId - Document ID
74+ */
75+ function restoreSidebarState ( docId ) {
76+ if ( ! rightSidebar ) return ;
77+
78+ const state = documentSidebarStates . get ( docId ) || DEFAULT_STATE ;
79+
80+ // Restore visibility
81+ if ( state . visible ) {
82+ rightSidebar . classList . remove ( 'hidden' ) ;
83+ } else {
84+ rightSidebar . classList . add ( 'hidden' ) ;
85+ }
86+
87+ // Restore active tab (even if hidden, so it's ready when opened)
88+ // Avoid calling switchSidebarTab if it triggers events or complex logic we don't want during restore?
89+ // switchSidebarTab updates the UI classes and display, which is what we want.
90+ // It also saves the state again, which is redundant but harmless.
91+ // To avoid recursion or side effects, we can manually update UI if needed,
92+ // but using the function ensures consistency.
93+ // However, switchSidebarTab calls saveCurrentState (if we add it there).
94+ // Let's make sure switchSidebarTab doesn't rely on existing DOM state that might be stale.
95+ // Actually, switchSidebarTab takes an argument and sets the state.
96+
97+ // We should just update the UI specific to the tab without "saving" first.
98+ updateSidebarTabUI ( state . activeTab ) ;
99+ }
100+
101+
102+ /**
103+ * Update the UI for a specific tab without changing business logic state
104+ * @param {string } tabId
105+ */
106+ function updateSidebarTabUI ( tabId ) {
40107 // Update tab buttons
41108 document . querySelectorAll ( '.sidebar-tab' ) . forEach ( t => {
42109 t . classList . toggle ( 'active' , t . dataset . tab === tabId ) ;
@@ -55,6 +122,15 @@ export function switchSidebarTab(tabId) {
55122 } ) ) ;
56123}
57124
125+ /**
126+ * Switch the active sidebar tab
127+ * @param {string } tabId - The tab ID to switch to
128+ */
129+ export function switchSidebarTab ( tabId ) {
130+ updateSidebarTabUI ( tabId ) ;
131+ saveCurrentState ( ) ;
132+ }
133+
58134/**
59135 * Show the right sidebar, optionally switching to a specific tab
60136 * @param {string } [tab] - Optional tab to show: 'timeline' or 'comments'
@@ -69,7 +145,9 @@ export function showRightSidebar(tab = null) {
69145
70146 // Switch to requested tab if specified
71147 if ( tab ) {
72- switchSidebarTab ( tab ) ;
148+ switchSidebarTab ( tab ) ; // This saves state
149+ } else {
150+ saveCurrentState ( ) ; // Save visibility change
73151 }
74152 }
75153}
@@ -84,6 +162,7 @@ export function hideRightSidebar() {
84162
85163 if ( rightSidebar ) {
86164 rightSidebar . classList . add ( 'hidden' ) ;
165+ saveCurrentState ( ) ;
87166 }
88167}
89168
0 commit comments