@@ -102,59 +102,9 @@ struct ContentView: View {
102102 columnVisibility = . detailOnly
103103 }
104104 }
105- . onChange ( of: ( payload? . connectionId ?? currentSession? . id) . flatMap { DatabaseManager . shared. connectionStatusVersions [ $0] } , initial: true ) { _, _ in
106- let sessions = DatabaseManager . shared. activeSessions
107- let connectionId = payload? . connectionId ?? currentSession? . id ?? DatabaseManager . shared. currentSessionId
108- guard let sid = connectionId else {
109- if currentSession != nil { currentSession = nil }
110- return
111- }
112- guard let newSession = sessions [ sid] else {
113- if currentSession? . id == sid {
114- rightPanelState? . teardown ( )
115- rightPanelState = nil
116- sessionState? . coordinator. teardown ( )
117- sessionState = nil
118- currentSession = nil
119- columnVisibility = . detailOnly
120- AppState . shared. isConnected = false
121- AppState . shared. safeModeLevel = . silent
122- AppState . shared. editorLanguage = . sql
123- AppState . shared. currentDatabaseType = nil
124- AppState . shared. supportsDatabaseSwitching = true
125-
126- // Close all native tab windows for this connection and
127- // force AppKit to deallocate them instead of pooling.
128- let tabbingId = " com.TablePro.main. \( sid. uuidString) "
129- DispatchQueue . main. async {
130- for window in NSApp . windows where window. tabbingIdentifier == tabbingId {
131- window. isReleasedWhenClosed = true
132- window. close ( )
133- }
134- }
135- }
136- return
137- }
138- if let existing = currentSession,
139- existing. isContentViewEquivalent ( to: newSession) {
140- return
141- }
142- currentSession = newSession
143- if rightPanelState == nil {
144- rightPanelState = RightPanelState ( )
145- }
146- if sessionState == nil {
147- sessionState = SessionStateFactory . create (
148- connection: newSession. connection,
149- payload: payload
150- )
151- }
152- AppState . shared. isConnected = true
153- AppState . shared. safeModeLevel = newSession. connection. safeModeLevel
154- AppState . shared. editorLanguage = PluginManager . shared. editorLanguage ( for: newSession. connection. type)
155- AppState . shared. currentDatabaseType = newSession. connection. type
156- AppState . shared. supportsDatabaseSwitching = PluginManager . shared. supportsDatabaseSwitching (
157- for: newSession. connection. type)
105+ . task { handleConnectionStatusChange ( ) }
106+ . onReceive ( NotificationCenter . default. publisher ( for: . connectionStatusDidChange) ) { _ in
107+ handleConnectionStatusChange ( )
158108 }
159109 . onReceive ( NotificationCenter . default. publisher ( for: NSWindow . didBecomeKeyNotification) ) { notification in
160110 // Only process notifications for our own window to avoid every
@@ -378,6 +328,61 @@ struct ContentView: View {
378328 )
379329 }
380330
331+ // MARK: - Connection Status
332+
333+ private func handleConnectionStatusChange( ) {
334+ let sessions = DatabaseManager . shared. activeSessions
335+ let connectionId = payload? . connectionId ?? currentSession? . id ?? DatabaseManager . shared. currentSessionId
336+ guard let sid = connectionId else {
337+ if currentSession != nil { currentSession = nil }
338+ return
339+ }
340+ guard let newSession = sessions [ sid] else {
341+ if currentSession? . id == sid {
342+ rightPanelState? . teardown ( )
343+ rightPanelState = nil
344+ sessionState? . coordinator. teardown ( )
345+ sessionState = nil
346+ currentSession = nil
347+ columnVisibility = . detailOnly
348+ AppState . shared. isConnected = false
349+ AppState . shared. safeModeLevel = . silent
350+ AppState . shared. editorLanguage = . sql
351+ AppState . shared. currentDatabaseType = nil
352+ AppState . shared. supportsDatabaseSwitching = true
353+
354+ let tabbingId = " com.TablePro.main. \( sid. uuidString) "
355+ DispatchQueue . main. async {
356+ for window in NSApp . windows where window. tabbingIdentifier == tabbingId {
357+ window. isReleasedWhenClosed = true
358+ window. close ( )
359+ }
360+ }
361+ }
362+ return
363+ }
364+ if let existing = currentSession,
365+ existing. isContentViewEquivalent ( to: newSession) {
366+ return
367+ }
368+ currentSession = newSession
369+ if rightPanelState == nil {
370+ rightPanelState = RightPanelState ( )
371+ }
372+ if sessionState == nil {
373+ sessionState = SessionStateFactory . create (
374+ connection: newSession. connection,
375+ payload: payload
376+ )
377+ }
378+ AppState . shared. isConnected = true
379+ AppState . shared. safeModeLevel = newSession. connection. safeModeLevel
380+ AppState . shared. editorLanguage = PluginManager . shared. editorLanguage ( for: newSession. connection. type)
381+ AppState . shared. currentDatabaseType = newSession. connection. type
382+ AppState . shared. supportsDatabaseSwitching = PluginManager . shared. supportsDatabaseSwitching (
383+ for: newSession. connection. type)
384+ }
385+
381386 // MARK: - Actions
382387
383388 private func connectToDatabase( _ connection: DatabaseConnection ) {
0 commit comments