Skip to content
TypeScript

fix(web): closing Settings or Usage no longer jumps back to a quote's source (#29013)

Julius Marminge31eb1d007656
diff --git a/apps/web/src/components/sidebar/mainAppLocation.browser.test.tsx b/apps/web/src/components/sidebar/mainAppLocation.browser.test.tsx
new file mode 100644
index 00000000000..dbcae07fca8
--- /dev/null
+++ b/apps/web/src/components/sidebar/mainAppLocation.browser.test.tsx
@@ -0,0 +1,77 @@
+import { EnvironmentId, MessageId, ThreadId, type AssistantCitation } from "@t3tools/contracts";
+import type { AnyRouter } from "@tanstack/react-router";
+import { describe, expect, it, vi } from "vite-plus/test";
+import { userEvent } from "vite-plus/test/browser";
+
+import { render } from "../../../test/render";
+import {
+ assistantCitationFromLocation,
+ assistantCitationNavigation,
+} from "../../lib/assistantCitationNavigation";
+import {
+ startMainAppLocationTracking,
+ useCloseAppModal,
+ useNavigateToMainApp,
+} from "./mainAppLocation";
+
+const THREAD_PATH = "/environment-one/thread-one";
+
+const citation: AssistantCitation = {
+ version: 1,
+ environmentId: EnvironmentId.make("environment-one"),
+ threadId: ThreadId.make("thread-one"),
+ messageId: MessageId.make("assistant-one"),
+ text: "quoted reply",
+ start: 0,
+ end: 12,
+ prefix: "",
+ suffix: "",
+};
+
+function ReturnControls() {
+ const closeAppModal = useCloseAppModal();
+ const navigateToMainApp = useNavigateToMainApp();
+ return (
+ <>
+ <button type="button" onClick={() => void closeAppModal()}>
+ Close modal
+ </button>
+ <button type="button" onClick={() => void navigateToMainApp()}>
+ Back to app
+ </button>
+ </>
+ );
+}
+
+// The tracker records on onResolved, which the router emits only once React commits the page.
+async function settled(router: AnyRouter, navigation: Promise<void>) {
+ await navigation;
+ await vi.waitFor(() =>
+ expect(router.state.resolvedLocation?.href).toBe(router.state.location.href),
+ );
+}
+
+describe("main app return location", () => {
+ it("returns to the thread without re-opening the quote source it was opened at", async () => {
+ await using screen = await render(<ReturnControls />, { router: THREAD_PATH });
+ const router = screen.router!;
+ using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
+
+ const returnLocation = async (from: string, control: string) => {
+ await settled(router, router.navigate(assistantCitationNavigation(citation)));
+ expect(assistantCitationFromLocation(router.state.location.href)).toEqual(citation);
+ await settled(router, router.navigate({ href: from }));
+ await userEvent.click(screen.getByRole("button", { name: control }));
+ await vi.waitFor(() => expect(router.state.location.pathname).toBe(THREAD_PATH));
+ return router.state.location.href;
+ };
+
+ const closingUsage = await returnLocation("/usage", "Close modal");
+ const sidebarBack = await returnLocation("/pull-requests", "Back to app");
+
+ expect({ closingUsage, sidebarBack }).toEqual({
+ closingUsage: THREAD_PATH,
+ sidebarBack: THREAD_PATH,
+ });
+ });
+});
diff --git a/apps/web/src/components/sidebar/mainAppLocation.ts b/apps/web/src/components/sidebar/mainAppLocation.ts
index a732d9f435b..8e4267af738 100644
--- a/apps/web/src/components/sidebar/mainAppLocation.ts
+++ b/apps/web/src/components/sidebar/mainAppLocation.ts
@@ -1,6 +1,7 @@
import { useNavigate, type AnyRouter } from "@tanstack/react-router";
import { GUEST_PAGE_PATH } from "../../guestPage";
+import { withoutAssistantCitationHash } from "../../lib/assistantCitationNavigation";
/** Settings and Usage open as modals over the last main-app view. */
export function isAppModalPage(pathname: string) {
@@ -22,8 +23,9 @@ let appModalReturnHref: string | null = null;
/** Remembers where Settings, Usage, and the utility pages lead back to. */
export function startMainAppLocationTracking(router: AnyRouter): () => void {
const record = () => {
- const { pathname, href } = router.state.location;
+ const { pathname } = router.state.location;
if (isAppModalPage(pathname) || SHELL_LESS_PATHS.has(pathname)) return;
+ const href = withoutAssistantCitationHash(router.state.location.href);
appModalReturnHref = href;
if (!isSidebarUtilityPage(pathname)) mainAppHref = href;
};
diff --git a/apps/web/src/lib/assistantCitationNavigation.ts b/apps/web/src/lib/assistantCitationNavigation.ts
index 1320b73aa33..3300d086d08 100644
--- a/apps/web/src/lib/assistantCitationNavigation.ts
+++ b/apps/web/src/lib/assistantCitationNavigation.ts
@@ -39,6 +39,13 @@ export function assistantCitationFromLocation(href: string) {
}
}
+/** Return paths drop the quote hash so going back does not reveal its source again. */
+export function withoutAssistantCitationHash(href: string) {
+ const hashIndex = href.indexOf("#");
+ if (hashIndex === -1 || !href.startsWith(CITATION_HASH_PREFIX, hashIndex + 1)) return href;
+ return href.slice(0, hashIndex);
+}
+
/** A fresh activation lets the same link reveal its source again after dismissal. */
export function assistantCitationNavigation(citation: AssistantCitation) {
return {