Skip to content

Commit ab9e6a2

Browse files
fix(web): closing Settings no longer leaves it as a back/forward stop (pingdotgg#36437)
1 parent 3c1be11 commit ab9e6a2

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎apps/web/src/components/sidebar/mainAppLocation.browser.test.tsx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,25 @@ describe("main app return location", () => {
7474
sidebarBack: THREAD_PATH,
7575
});
7676
});
77+
78+
it("leaves no stop on Settings for history back after it closes", async () => {
79+
const otherThreadPath = "/environment-one/thread-two";
80+
await using screen = await render(<ReturnControls />, { router: THREAD_PATH });
81+
const router = screen.router!;
82+
using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
83+
84+
await settled(router, router.navigate({ href: otherThreadPath }));
85+
await settled(router, router.navigate({ href: "/settings/general" }));
86+
await userEvent.click(screen.getByRole("button", { name: "Close modal" }));
87+
await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
88+
89+
router.history.forward();
90+
await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
91+
router.history.back();
92+
await vi.waitFor(() => expect(router.state.location.pathname).toBe(THREAD_PATH));
93+
router.history.forward();
94+
await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
95+
router.history.forward();
96+
await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
97+
});
7798
});

‎apps/web/src/components/sidebar/mainAppLocation.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useNavigate, type AnyRouter } from "@tanstack/react-router";
1+
import { useNavigate, useRouter, type AnyRouter } from "@tanstack/react-router";
22

33
import { GUEST_PAGE_PATH } from "../../guestPage";
44
import { withoutAssistantCitationHash } from "../../lib/assistantCitationNavigation";
@@ -20,6 +20,7 @@ const SHELL_LESS_PATHS = new Set(["/pair", "/connect", "/connect-agent", GUEST_P
2020

2121
let mainAppHref: string | null = null;
2222
let appModalReturnHref: string | null = null;
23+
let appModalReturnHistoryIndex: number | null = null;
2324
let appModalReturnThreadTarget: ThreadRouteTarget | null = null;
2425

2526
/** Remembers where Settings, Usage, and the utility pages lead back to. */
@@ -29,6 +30,9 @@ export function startMainAppLocationTracking(router: AnyRouter): () => void {
2930
if (isAppModalPage(pathname) || SHELL_LESS_PATHS.has(pathname)) return;
3031
const href = withoutAssistantCitationHash(router.state.location.href);
3132
appModalReturnHref = href;
33+
// Only an entry that is exactly the return location can be stepped back to.
34+
appModalReturnHistoryIndex =
35+
href === router.state.location.href ? router.state.location.state.__TSR_index : null;
3236
appModalReturnThreadTarget = resolveThreadRouteTarget(
3337
router.state.matches.at(-1)?.params ?? {},
3438
);
@@ -51,5 +55,18 @@ export function useNavigateToMainApp() {
5155
/** Leaves Settings or Usage for the page that opened it. */
5256
export function useCloseAppModal() {
5357
const navigate = useNavigate();
54-
return () => navigate({ href: appModalReturnHref ?? mainAppHref ?? "/" });
58+
const router = useRouter();
59+
return () => {
60+
const href = appModalReturnHref ?? mainAppHref ?? "/";
61+
const returnIndex = appModalReturnHistoryIndex ?? 0;
62+
const steps = router.state.location.state.__TSR_index - returnIndex;
63+
if (returnIndex === 0 || steps <= 0) return navigate({ href });
64+
// Go back to the entry before the return page and push it again: only a push drops the
65+
// modal's entries from forward history, and reusing the old entry avoids a duplicate stop.
66+
const unsubscribe = router.history.subscribe(() => {
67+
unsubscribe();
68+
void navigate({ href });
69+
});
70+
router.history.go(-(steps + 1));
71+
};
5572
}

0 commit comments

Comments
 (0)