fix(vscode): restore previous view when exiting settings (closes #1776) (closes #1848)

This commit is contained in:
Catan
2026-07-06 11:16:23 +03:00
committed by GitHub
parent d8f0ef074b
commit 9a7d7a4379
@@ -108,6 +108,10 @@ export const VSCodeLayout: React.FC = () => {
}, []);
const [currentView, setCurrentView] = React.useState<VSCodeView>(() => (bootDraftOpen ? 'chat' : 'sessions'));
// Mirror currentView so the navigate event handler (registered once) can read the live value.
const currentViewRef = React.useRef(currentView);
// Snapshot of the view the user was on before opening Settings, so close restores it.
const viewBeforeSettingsRef = React.useRef<VSCodeView | null>(null);
const [containerWidth, setContainerWidth] = React.useState<number>(0);
const [expandedSidebarWidth, setExpandedSidebarWidth] = React.useState<number>(SESSIONS_SIDEBAR_WIDTH);
const [isResizingExpandedSidebar, setIsResizingExpandedSidebar] = React.useState(false);
@@ -185,6 +189,11 @@ export const VSCodeLayout: React.FC = () => {
}
}, [currentSessionId]);
// Keep currentViewRef in sync so the stable navigate handler reads the live view.
React.useEffect(() => {
currentViewRef.current = currentView;
}, [currentView]);
React.useEffect(() => {
const vscodeApi = runtimeApis.vscode;
if (!vscodeApi) {
@@ -347,6 +356,9 @@ export const VSCodeLayout: React.FC = () => {
const detail = (event as CustomEvent<{ view?: string }>).detail;
const view = detail?.view;
if (view === 'settings') {
if (currentViewRef.current !== 'settings') {
viewBeforeSettingsRef.current = currentViewRef.current;
}
setCurrentView('settings');
} else if (view === 'chat') {
setCurrentView('chat');
@@ -532,7 +544,11 @@ export const VSCodeLayout: React.FC = () => {
// Settings view
<React.Suspense fallback={null}>
<SettingsView
onClose={() => setCurrentView(usesExpandedLayout ? 'chat' : 'sessions')}
onClose={() => {
const previousView = viewBeforeSettingsRef.current;
viewBeforeSettingsRef.current = null;
setCurrentView(previousView ?? (usesExpandedLayout ? 'chat' : 'sessions'));
}}
forceMobile={usesMobileLayout}
/>
</React.Suspense>