Skip to content
TypeScript

fix(web): restore folder breadcrumb trails when reopening tabs (#30706)

Johan Vaz10f88f34edca
diff --git a/apps/web/src/reopenClosedView.test.ts b/apps/web/src/reopenClosedView.test.ts
index 04532373624..219a7792a1b 100644
--- a/apps/web/src/reopenClosedView.test.ts
+++ b/apps/web/src/reopenClosedView.test.ts
@@ -12,6 +12,7 @@ import { __setClientSettingsForTests } from "./hooks/useSettings";
import { readThreadPreviewState, resetPreviewStateForTests } from "./previewStateStore";
import { planNextReopen, type ReopenOwnerState, reopenClosedView } from "./reopenClosedView";
import {
+ hostRightPanel,
selectThreadRightPanelState,
type ThreadRightPanelState,
useRightPanelStore,
@@ -40,28 +41,117 @@ beforeEach(() => {
});
describe("reopenClosedView", () => {
- it("restores a file tab at its line without creating a browser session", async () => {
- const openPreview = vi.fn();
- const reopened = await reopenClosedView(
- {
- kind: "panel-tab",
+ it.each([
+ { relativePath: "src/app.ts", line: 18, folderTrail: undefined },
+ { relativePath: "apps", line: null, folderTrail: "apps/web/src" },
+ { relativePath: "", line: null, folderTrail: "apps/web/src" },
+ ])(
+ "restores the captured file or folder tab at '$relativePath'",
+ async ({ relativePath, line, folderTrail }) => {
+ const panels = useRightPanelStore.getState();
+ if (folderTrail === undefined) panels.openFile(threadRef, relativePath, line ?? undefined);
+ else {
+ panels.openFile(threadRef, folderTrail);
+ panels.navigateFolder(threadRef, `file:${folderTrail}`, relativePath, folderTrail);
+ }
+ const expected =
+ relativePath === ""
+ ? { kind: "files", id: "files", folderTrail }
+ : {
+ kind: "file",
+ id: `file:${relativePath}`,
+ relativePath,
+ revealLine: line,
+ revealRequestId: 1,
+ ...(folderTrail === undefined ? {} : { folderTrail }),
+ };
+ const before = selectThreadRightPanelState(
+ useRightPanelStore.getState().byThreadKey,
threadRef,
- surface: {
- kind: "file",
- id: "file:src/app.ts",
- relativePath: "src/app.ts",
- revealLine: 18,
- revealRequestId: 1,
- },
- },
- { openPreview, workspaceAvailable: true },
- );
- expect(reopened).toBe(true);
- const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef);
- expect(state.isOpen).toBe(true);
- expect(state.surfaces).toMatchObject([{ id: "file:src/app.ts", revealLine: 18 }]);
- expect(openPreview).not.toHaveBeenCalled();
- });
+ );
+ expect(before.surfaces).toEqual([expected]);
+ panels.closeSurface(threadRef, expected.id);
+ expect(
+ selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef).surfaces,
+ ).toEqual([]);
+ const entries = useClosedViewStore.getState().entries;
+ expect(entries).toEqual([
+ { id: expect.any(String), kind: "panel-tab", threadRef, surface: expected },
+ ]);
+ const openPreview = vi.fn();
+ const reopened = await reopenClosedView(entries[0]!, {
+ openPreview,
+ workspaceAvailable: true,
+ });
+ expect(reopened).toBe(true);
+ const state = selectThreadRightPanelState(
+ useRightPanelStore.getState().byThreadKey,
+ threadRef,
+ );
+ expect(state.isOpen).toBe(true);
+ expect(state.activeSurfaceId).toBe(expected.id);
+ expect(state.surfaces).toEqual([expected]);
+ expect(openPreview).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each([
+ { hosted: false, folderTrail: "packages/contracts/src" },
+ { hosted: false, folderTrail: undefined },
+ { hosted: true, folderTrail: "packages/contracts/src" },
+ { hosted: true, folderTrail: undefined },
+ ])(
+ "preserves a hidden root tab with trail $folderTrail (hosted: $hosted)",
+ async ({ hosted, folderTrail }) => {
+ const panels = useRightPanelStore.getState();
+ panels.openFile(threadRef, "apps/web/src");
+ panels.navigateFolder(threadRef, "file:apps/web/src", "", "apps/web/src");
+ panels.closeSurface(threadRef, "files");
+ const entry = useClosedViewStore.getState().entries[0]!;
+ const hostRef = hosted
+ ? { ...threadRef, threadId: "host" as ScopedThreadRef["threadId"] }
+ : threadRef;
+ const unhost = hosted ? hostRightPanel(threadRef, hostRef) : undefined;
+ try {
+ if (folderTrail === undefined) panels.open(threadRef, "files");
+ else {
+ panels.openFile(threadRef, folderTrail);
+ panels.navigateFolder(threadRef, `file:${folderTrail}`, "", folderTrail);
+ }
+ panels.close(threadRef);
+ const before = selectThreadRightPanelState(
+ useRightPanelStore.getState().byThreadKey,
+ hostRef,
+ );
+ expect(before.isOpen).toBe(false);
+ const plan = planNextReopen([entry], () => ({
+ environmentKnown: true,
+ catalogReady: true,
+ ownerExists: true,
+ sideChatExists: true,
+ shellLive: true,
+ panel: before,
+ }));
+ expect(plan.restore).toBe(entry);
+ const openPreview = vi.fn();
+ expect(
+ await reopenClosedView(plan.restore!, { openPreview, workspaceAvailable: true }),
+ ).toBe(true);
+ const after = selectThreadRightPanelState(
+ useRightPanelStore.getState().byThreadKey,
+ hostRef,
+ );
+ expect(after.isOpen).toBe(true);
+ expect(after.activeSurfaceId).toBe("files");
+ expect(after.surfaces).toEqual([
+ { kind: "files", id: "files", ...(folderTrail === undefined ? {} : { folderTrail }) },
+ ]);
+ expect(openPreview).not.toHaveBeenCalled();
+ } finally {
+ unhost?.();
+ }
+ },
+ );
it("does not reopen workspace tabs without an available project", async () => {
const options = {
@@ -69,7 +159,7 @@ describe("reopenClosedView", () => {
workspaceAvailable: false,
};
for (const surface of [
- { kind: "files", id: "files" },
+ { kind: "files", id: "files", folderTrail: "apps/web/src" },
{
kind: "file",
id: "file:src/app.ts",
@@ -77,6 +167,14 @@ describe("reopenClosedView", () => {
revealLine: null,
revealRequestId: 0,
},
+ {
+ kind: "file",
+ id: "file:apps",
+ relativePath: "apps",
+ revealLine: null,
+ revealRequestId: 1,
+ folderTrail: "apps/web/src",
+ },
] as const) {
expect(await reopenClosedView({ kind: "panel-tab", threadRef, surface }, options)).toBe(
false,
diff --git a/apps/web/src/reopenClosedView.ts b/apps/web/src/reopenClosedView.ts
index 005887ad81f..899a1b223ec 100644
--- a/apps/web/src/reopenClosedView.ts
+++ b/apps/web/src/reopenClosedView.ts
@@ -5,7 +5,9 @@ import type { ClosedView, ClosedViewEntry } from "./closedViewStore";
import type { PullRequestListPreferences } from "./components/pullRequest/pullRequestListPreferences";
import { openPreviewSession } from "./components/preview/openPreviewSession";
import {
+ rightPanelHostRef,
type RightPanelSurface,
+ selectThreadRightPanelState,
type ThreadRightPanelState,
useRightPanelStore,
} from "./rightPanelStore";
@@ -125,8 +127,24 @@ export async function reopenClosedView(
break;
case "file":
if (surface.attachment) panels.openAttachment(ref, surface.attachment);
- else panels.openFile(ref, surface.relativePath, surface.revealLine ?? undefined);
+ else {
+ panels.openFile(ref, surface.relativePath, surface.revealLine ?? undefined);
+ if (typeof surface.folderTrail === "string") {
+ panels.navigateFolder(ref, surface.id, surface.relativePath, surface.folderTrail);
+ }
+ }
+ break;
+ case "files": {
+ const rootAlreadyExists = selectThreadRightPanelState(
+ panels.byThreadKey,
+ rightPanelHostRef(ref),
+ ).surfaces.some((current) => current.kind === "files");
+ panels.open(ref, "files");
+ if (!rootAlreadyExists && typeof surface.folderTrail === "string") {
+ panels.navigateFolder(ref, surface.id, "", surface.folderTrail);
+ }
break;
+ }
case "device":
if (surface.target) {
panels.openDevice(ref, surface.target);