feat(editor): recover last closed query tab draft in the next blank tab#1687
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3f07666d21
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| from: tabManager.tabs, | ||
| selectedTabId: tabManager.selectedTabId | ||
| ) { | ||
| ClosedTabDraftStorage.shared.saveQuery(draft, connectionId: connectionId) |
There was a problem hiding this comment.
Avoid saving drafts during database-switch cleanup
When switching databases, switchDatabase(to:) calls closeSiblingNativeWindows(), which closes sibling native tabs via sibling.close() before clearing the current tab (MainContentCoordinator+Navigation.swift lines 373-397). Those programmatic closes now run this same windowWillClose path and save a draft keyed only by connection id, so after the switch the next blank tab for that connection can be prefilled with SQL from the previous database instead of starting blank. This defeats the database-switch cleanup and can lead users to run stale SQL in the newly selected database; suppress draft capture for those programmatic closes or include the active database in the draft scope.
Useful? React with 👍 / 👎.
Closes #1686
Problem
Closing a query tab (Cmd+W or the close button) dropped its unsaved SQL with no way to get it back.
Root cause
Three compounding gaps:
performClosecleared the window's tabs with no prior persist.saveOrClearAggregatedsee 0 tabs and delete the session JSON.The existing
TabPersistencepipeline can't hold the draft because it clears on 0 tabs, so this adds a separate per-connection store (same pattern asDatabaseTreeFilterStorage, and the iOSlastQuery.{connectionId}precedent).Fix
ClosedTabDraftStoragepersists the SQL of a closed query tab and hands it back to the next blank tab for the same connection. Silent recovery, no dialog, which matches the macOS state-restoration model for a non-document editor (and VS Code hot-exit / Postico autosave).performClose(last-tab close) and inhandleWindowWillClose(X button, closing one of several tabs), guarded by!isAppTerminatingso app-quit doesn't pollute it.newTabconsumes the draft (read-once) into the new tab.draftCandidateignores blank, file-backed, and table tabs and prefers the selected tab. 500k cap mirrored fromTabQueryContent.Invariants
saveOrClearAggregatedonly touches the session JSON, never the newUserDefaultsstore.Tests
ClosedTabDraftStorageTests(11 tests): round-trip, consume-once, per-connection isolation, blank/file-backed/table exclusion, selected-vs-first preference, 500k cap, clear.