Skip to content

Commit f7d3b5c

Browse files
committed
fix: read safe mode level from live toolbar state instead of immutable connection
1 parent 72320ad commit f7d3b5c

8 files changed

+22
-14
lines changed

TablePro/ContentView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ struct ContentView: View {
193193
AppState.shared.supportsDatabaseSwitching = true
194194
}
195195
}
196+
.onChange(of: sessionState?.toolbarState.safeModeLevel) { _, newLevel in
197+
if let level = newLevel {
198+
AppState.shared.safeModeLevel = level
199+
}
200+
}
196201
}
197202

198203
// MARK: - View Components

TablePro/Views/Main/Child/MainEditorContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ struct MainEditorContentView: View {
321321
changeManager: currentChangeManager,
322322
resultVersion: tab.resultVersion,
323323
metadataVersion: tab.metadataVersion,
324-
isEditable: tab.isEditable && !tab.isView && !connection.safeModeLevel.blocksAllWrites,
324+
isEditable: tab.isEditable && !tab.isView && !coordinator.safeModeLevel.blocksAllWrites,
325325
onRefresh: onRefresh,
326326
onCellEdit: onCellEdit,
327327
onUndo: { [binding = _selectedRowIndices, coordinator] in

TablePro/Views/Main/Extensions/MainContentCoordinator+Discard.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension MainContentCoordinator {
1717
let sqlPreview = statements.map(\.sql).joined(separator: "\n")
1818
let window = await MainActor.run { NSApp.keyWindow }
1919
let permission = await SafeModeGuard.checkPermission(
20-
level: connection.safeModeLevel,
20+
level: safeModeLevel,
2121
isWriteOperation: true,
2222
sql: sqlPreview,
2323
operationDescription: String(localized: "Save Sidebar Changes"),

TablePro/Views/Main/Extensions/MainContentCoordinator+RowOperations.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extension MainContentCoordinator {
1111
// MARK: - Row Operations
1212

1313
func addNewRow(selectedRowIndices: inout Set<Int>, editingCell: inout CellPosition?) {
14-
guard !connection.safeModeLevel.blocksAllWrites,
14+
guard !safeModeLevel.blocksAllWrites,
1515
let tabIndex = tabManager.selectedTabIndex,
1616
tabIndex < tabManager.tabs.count else { return }
1717

@@ -31,7 +31,7 @@ extension MainContentCoordinator {
3131
}
3232

3333
func deleteSelectedRows(indices: Set<Int>, selectedRowIndices: inout Set<Int>) {
34-
guard !connection.safeModeLevel.blocksAllWrites,
34+
guard !safeModeLevel.blocksAllWrites,
3535
let tabIndex = tabManager.selectedTabIndex,
3636
tabIndex < tabManager.tabs.count,
3737
tabManager.tabs[tabIndex].isEditable,
@@ -53,7 +53,7 @@ extension MainContentCoordinator {
5353
}
5454

5555
func duplicateSelectedRow(index: Int, selectedRowIndices: inout Set<Int>, editingCell: inout CellPosition?) {
56-
guard !connection.safeModeLevel.blocksAllWrites,
56+
guard !safeModeLevel.blocksAllWrites,
5757
let tabIndex = tabManager.selectedTabIndex,
5858
tabIndex < tabManager.tabs.count else { return }
5959

@@ -154,7 +154,7 @@ extension MainContentCoordinator {
154154
}
155155

156156
func pasteRows(selectedRowIndices: inout Set<Int>, editingCell: inout CellPosition?) {
157-
guard !connection.safeModeLevel.blocksAllWrites,
157+
guard !safeModeLevel.blocksAllWrites,
158158
let index = tabManager.selectedTabIndex else { return }
159159

160160
var tab = tabManager.tabs[index]

TablePro/Views/Main/Extensions/MainContentCoordinator+SaveChanges.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension MainContentCoordinator {
1717
pendingDeletes: inout Set<String>,
1818
tableOperationOptions: inout [String: TableOperationOptions]
1919
) {
20-
guard !connection.safeModeLevel.blocksAllWrites else {
20+
guard !safeModeLevel.blocksAllWrites else {
2121
if let index = tabManager.selectedTabIndex {
2222
tabManager.tabs[index].errorMessage = "Cannot save changes: connection is read-only"
2323
}
@@ -60,7 +60,7 @@ extension MainContentCoordinator {
6060
return
6161
}
6262

63-
let level = connection.safeModeLevel
63+
let level = safeModeLevel
6464
if level.requiresConfirmation {
6565
let sqlPreview = allStatements.map(\.sql).joined(separator: "\n")
6666
// Snapshot inout values before clearing — needed for executeCommitStatements

TablePro/Views/Main/Extensions/MainContentCoordinator+SidebarActions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extension MainContentCoordinator {
1313
// MARK: - View Operations
1414

1515
func createView() {
16-
guard !connection.safeModeLevel.blocksAllWrites else { return }
16+
guard !safeModeLevel.blocksAllWrites else { return }
1717

1818
let driver = DatabaseManager.shared.driver(for: connection.id)
1919
let template = driver?.createViewTemplate()
@@ -63,7 +63,7 @@ extension MainContentCoordinator {
6363
}
6464

6565
func openImportDialog() {
66-
guard !connection.safeModeLevel.blocksAllWrites else { return }
66+
guard !safeModeLevel.blocksAllWrites else { return }
6767
guard PluginManager.shared.supportsImport(for: connection.type) else {
6868
AlertHelper.showErrorSheet(
6969
title: String(localized: "Import Not Supported"),

TablePro/Views/Main/Extensions/MainContentView+Bindings.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension MainContentView {
3434

3535
/// Determine if sidebar should be in editable mode
3636
var isSidebarEditable: Bool {
37-
guard !coordinator.connection.safeModeLevel.blocksAllWrites,
37+
guard !coordinator.safeModeLevel.blocksAllWrites,
3838
let tab = coordinator.tabManager.selectedTab,
3939
tab.tabType == .table || tab.tableName != nil,
4040
!selectedRowIndices.isEmpty else {

TablePro/Views/Main/MainContentCoordinator.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ final class MainContentCoordinator {
4949

5050
let connection: DatabaseConnection
5151
var connectionId: UUID { connection.id }
52+
/// Live safe mode level — reads from toolbar state (user-editable),
53+
/// not from the immutable connection snapshot.
54+
var safeModeLevel: SafeModeLevel { toolbarState.safeModeLevel }
5255
let tabManager: QueryTabManager
5356
let changeManager: DataChangeManager
5457
let filterStateManager: FilterStateManager
@@ -508,7 +511,7 @@ final class MainContentCoordinator {
508511
guard !statements.isEmpty else { return }
509512

510513
// Safe mode enforcement for query execution
511-
let level = connection.safeModeLevel
514+
let level = safeModeLevel
512515

513516
if level == .readOnly {
514517
let writeStatements = statements.filter { isWriteQuery($0) }
@@ -584,7 +587,7 @@ final class MainContentCoordinator {
584587
let sql = tabManager.tabs[index].query
585588
guard !sql.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return }
586589

587-
let level = connection.safeModeLevel
590+
let level = safeModeLevel
588591
if level.appliesToAllQueries && level.requiresConfirmation,
589592
tabManager.tabs[index].lastExecutedAt == nil
590593
{
@@ -688,7 +691,7 @@ final class MainContentCoordinator {
688691
let statements = SQLStatementScanner.allStatements(in: trimmed)
689692
guard let stmt = statements.first else { return }
690693

691-
let level = connection.safeModeLevel
694+
let level = safeModeLevel
692695
let needsConfirmation = level.appliesToAllQueries && level.requiresConfirmation
693696

694697
// Multi-variant EXPLAIN: use plugin-declared variants if available

0 commit comments

Comments
 (0)