Skip to content

Commit ddbe290

Browse files
committed
fix(settings): the terminal shortcut returns to the terminal tab you last used
1 parent 5aca64f commit ddbe290

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎apps/web/src/rightPanelStore.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ describe("rightPanelStore", () => {
158158
expect(resolve()).toEqual({ kind: "show", surfaceId: "terminal:term-1" });
159159

160160
store.toggle(refA, "diff");
161+
expect(resolve()).toEqual({ kind: "show", surfaceId: "terminal:term-1" });
162+
163+
store.closeSurface(refA, "terminal:term-1");
161164
expect(resolve()).toEqual({ kind: "show", surfaceId: "terminal:term-2" });
162165
});
163166

‎apps/web/src/rightPanelStore.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ export interface ThreadRightPanelState {
9393
dismissedDeviceSurfaceIds?: string[];
9494
maximized?: true;
9595
proactiveDiffTurnId?: RunId;
96+
/** The terminal tab last in front, which the terminal shortcut returns to. */
97+
lastTerminalSurfaceId?: string;
9698
}
9799

98100
export interface ThreadPanelVisibility {
@@ -300,13 +302,26 @@ const upsertSurface = (
300302
activeSurfaceId: activate ? surface.id : current.activeSurfaceId,
301303
});
302304

305+
const rememberTerminalSurface = (state: ThreadRightPanelState): ThreadRightPanelState => {
306+
const active = state.surfaces.find((surface) => surface.id === state.activeSurfaceId);
307+
const last =
308+
active?.kind === "terminal"
309+
? active.id
310+
: state.surfaces.some((surface) => surface.id === state.lastTerminalSurfaceId)
311+
? state.lastTerminalSurfaceId
312+
: undefined;
313+
if (last === state.lastTerminalSurfaceId) return state;
314+
const { lastTerminalSurfaceId: _previous, ...rest } = state;
315+
return last === undefined ? rest : { ...rest, lastTerminalSurfaceId: last };
316+
};
317+
303318
const updateThreadStateMap = (
304319
byThreadKey: Record<string, ThreadRightPanelState>,
305320
threadKey: string,
306321
updater: (current: ThreadRightPanelState) => ThreadRightPanelState,
307322
): Record<string, ThreadRightPanelState> => {
308323
const current = byThreadKey[threadKey] ?? EMPTY_THREAD_STATE;
309-
const next = updater(current);
324+
const next = rememberTerminalSurface(updater(current));
310325
if (
311326
!next.isOpen &&
312327
next.activeSurfaceId === null &&
@@ -1193,7 +1208,9 @@ export function resolveRightPanelTerminalToggle(
11931208
const terminal =
11941209
selected?.kind === "terminal"
11951210
? selected
1196-
: state.surfaces.findLast((surface) => surface.kind === "terminal");
1211+
: (state.surfaces.find(
1212+
(surface) => surface.kind === "terminal" && surface.id === state.lastTerminalSurfaceId,
1213+
) ?? state.surfaces.findLast((surface) => surface.kind === "terminal"));
11971214
return terminal ? { kind: "show", surfaceId: terminal.id } : { kind: "create" };
11981215
}
11991216

0 commit comments

Comments
 (0)