fix: restore duplicate browser pages by index

This commit is contained in:
Neil
2026-05-31 09:05:18 -07:00
committed by GitHub
parent e73e531179
commit 5639483a2c
2 changed files with 33 additions and 4 deletions
@@ -390,6 +390,35 @@ describe('createBrowserSlice floating tabs', () => {
})
})
describe('createBrowserSlice closed browser workspaces', () => {
it('reopens duplicate-URL browser pages on the originally active page', () => {
const store = createTestStore()
const tab = store.getState().createBrowserTab('wt-1', 'https://example.com/dashboard', {
title: 'First copy'
})
const secondPage = store.getState().createBrowserPage(tab.id, 'https://example.com/dashboard', {
title: 'Second copy'
})
if (!secondPage) {
throw new Error('Expected a second browser page')
}
store.getState().closeBrowserTab(tab.id)
const restored = store.getState().reopenClosedBrowserTab('wt-1')
if (!restored) {
throw new Error('Expected a reopened browser workspace')
}
const restoredPages = store.getState().browserPagesByWorkspace[restored.id] ?? []
const activePage = restoredPages.find((page) => page.id === restored.activePageId)
expect(restoredPages.map((page) => page.url)).toEqual([
'https://example.com/dashboard',
'https://example.com/dashboard'
])
expect(activePage?.title).toBe('Second copy')
})
})
describe('createBrowserSlice runtime guard', () => {
beforeEach(() => {
vi.clearAllMocks()
+4 -4
View File
@@ -760,13 +760,13 @@ export const createBrowserSlice: StateCreator<AppState, [], [], BrowserSlice> =
})
}
// Activate the originally-active page if it wasn't the first one
// Why: duplicate URLs are valid browser pages; restoring by URL can select
// the wrong copy. The restore path preserves page order, so map by index.
const activePageId = snap.activePageId
if (activePageId) {
const restoredPages = get().browserPagesByWorkspace[restored.id] ?? []
const targetPage = restoredPages.find(
(p) => p.url === pages.find((orig) => orig.id === activePageId)?.url
)
const activePageIndex = pages.findIndex((orig) => orig.id === activePageId)
const targetPage = activePageIndex >= 0 ? restoredPages[activePageIndex] : null
if (targetPage && targetPage.id !== restoredPages[0]?.id) {
get().setActiveBrowserPage(restored.id, targetPage.id)
}