fix(filePreview): reset breadcrumb history when restoring closed tab

When a file preview tab is closed and later restored via Cmd+Shift+T,
the navigation history could become corrupted or stale, causing
confusing breadcrumb behavior (e.g., pressing forward navigates to
an unexpected file).

Fix by resetting the navigation history to just the current file
when restoring a tab. The restored tab will show the last viewed
file with a fresh, empty breadcrumb history.

Changes:
- Modified reopenUnifiedClosedTab() to reset navigationHistory and
  navigationIndex when restoring file tabs
- Added test case to verify navigation history is reset on restore
This commit is contained in:
Pedram Amini
2026-02-02 21:13:07 -06:00
parent 48a2741208
commit 8b78f2988d
2 changed files with 49 additions and 0 deletions

View File

@@ -1842,6 +1842,49 @@ describe('tabHelpers', () => {
expect(result!.session.unifiedClosedTabHistory).toHaveLength(0);
});
it('resets navigation history when restoring file tab', () => {
const aiTab = createMockTab({ id: 'ai-1' });
// Create a file tab with stale navigation history (multiple entries)
const closedFileTab = createMockFileTab({
id: 'closed-file',
path: '/test/fileB.ts',
name: 'fileB',
scrollTop: 100,
navigationHistory: [
{ path: '/test/fileA.ts', name: 'fileA', scrollTop: 0 },
{ path: '/test/fileB.ts', name: 'fileB', scrollTop: 100 },
{ path: '/test/fileC.ts', name: 'fileC', scrollTop: 200 },
],
navigationIndex: 1, // Currently viewing fileB
});
const closedEntry = {
type: 'file' as const,
tab: closedFileTab,
unifiedIndex: 1,
closedAt: Date.now(),
};
const session = createMockSession({
aiTabs: [aiTab],
activeTabId: 'ai-1',
filePreviewTabs: [],
activeFileTabId: null,
unifiedTabOrder: [{ type: 'ai', id: 'ai-1' }],
unifiedClosedTabHistory: [closedEntry],
});
const result = reopenUnifiedClosedTab(session);
expect(result).not.toBeNull();
expect(result!.tabType).toBe('file');
const restoredTab = result!.session.filePreviewTabs[0];
// Navigation history should be reset to just the current file
expect(restoredTab.navigationHistory).toHaveLength(1);
expect(restoredTab.navigationHistory![0].path).toBe('/test/fileB.ts');
expect(restoredTab.navigationHistory![0].name).toBe('fileB');
expect(restoredTab.navigationHistory![0].scrollTop).toBe(100);
expect(restoredTab.navigationIndex).toBe(0);
});
it('reopens AI tab from unified history', () => {
const existingAiTab = createMockTab({ id: 'ai-existing' });
const closedAiTab = createMockTab({ id: 'ai-closed', agentSessionId: 'session-456' });

View File

@@ -724,12 +724,18 @@ export function reopenUnifiedClosedTab(session: Session): ReopenUnifiedClosedTab
}
// No duplicate - restore the tab
// Reset navigation history to just the current file to avoid stale/corrupted breadcrumbs
const restoredTab: FilePreviewTab = {
...tabToRestore,
id: generateId(),
// Clear any unsaved edit content since we're restoring from history
editContent: undefined,
editMode: false,
// Reset breadcrumb history - start fresh with just the current file
navigationHistory: [
{ path: tabToRestore.path, name: tabToRestore.name, scrollTop: tabToRestore.scrollTop },
],
navigationIndex: 0,
};
// Add to filePreviewTabs