TypeScript
fix(web): keep later navigation when thread archival completes (#24109)
Victor Vogelc84acfa2054c
diff --git a/apps/web/src/hooks/useThreadActions.permissions.test.ts b/apps/web/src/hooks/useThreadActions.permissions.test.tsindex 814c25e04da..fd27270f338 100644--- a/apps/web/src/hooks/useThreadActions.permissions.test.ts+++ b/apps/web/src/hooks/useThreadActions.permissions.test.ts@@ -43,7 +43,7 @@ vi.mock("react", () => ({useRef: (current: unknown) => ({ current }),}));vi.mock("@tanstack/react-router", () => ({- useRouter: () => ({ state: { matches: [] }, navigate: async () => {} }),+ useRouter: () => ({ state: { location: { href: "/" }, matches: [] }, navigate: async () => {} }),}));vi.mock("../state/session", () => ({environmentSession: { sessionStateAtom: "session" },diff --git a/apps/web/src/hooks/useThreadActions.ts b/apps/web/src/hooks/useThreadActions.tsindex 32020316496..3bfc7e30592 100644--- a/apps/web/src/hooks/useThreadActions.ts+++ b/apps/web/src/hooks/useThreadActions.ts@@ -374,6 +374,7 @@ export function useThreadActions() {);}+ const requestingRouteHref = router.state.location.href;const currentRouteThreadRef = getCurrentRouteThreadRef();const shouldNavigateToDraft =currentRouteThreadRef?.threadId === threadRef.threadId &&@@ -400,7 +401,7 @@ export function useThreadActions() {failureTitle: "Failed to undo archive",});- if (shouldNavigateToDraft) {+ if (shouldNavigateToDraft && router.state.location.href === requestingRouteHref) {const navigationResult = await settlePromise(() =>handleNewThread(scopeProjectRef(thread.environmentId, thread.projectId)),);diff --git a/apps/web/src/hooks/useThreadActions.undo.test.ts b/apps/web/src/hooks/useThreadActions.undo.test.tsindex 23ecd41f211..1863e523fec 100644--- a/apps/web/src/hooks/useThreadActions.undo.test.ts+++ b/apps/web/src/hooks/useThreadActions.undo.test.ts@@ -19,7 +19,7 @@ const commands = vi.hoisted(() => ({}));const router = vi.hoisted(() => ({navigate: vi.fn(async () => {}),- state: { matches: [{ params: {} as Record<string, string> }] },+ state: { location: { href: "/" }, matches: [{ params: {} as Record<string, string> }] },}));vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() }));vi.mock("../state/session", async (original) => ({@@ -34,7 +34,11 @@ vi.mock("react", async (original) => ({}));vi.mock("@tanstack/react-router", () => ({ useRouter: () => router }));vi.mock("./useSettings", () => ({ useClientSettings: () => false }));-vi.mock("./useHandleNewThread", () => ({ useNewThreadHandler: () => vi.fn() }));+vi.mock("./useHandleNewThread", () => ({+ useNewThreadHandler: () => async () => {+ router.state.location.href = "/draft/project";+ },+}));vi.mock("../composerDraftStore", () => ({ useComposerDraftStore: () => vi.fn() }));vi.mock("../terminalUiStateStore", () => ({ useTerminalUiStateStore: () => vi.fn() }));vi.mock("../uiStateStore", () => ({ useUiStateStore: () => vi.fn() }));@@ -94,6 +98,7 @@ beforeEach(() => {command.mockReset().mockResolvedValue({ _tag: "Success", value: undefined });}router.navigate.mockClear();+ router.state.location.href = "/";router.state.matches[0]!.params = {};threadShell.pinnedAt = null;threadShell.snoozedUntil = null;@@ -201,6 +206,32 @@ describe("archive Undo", () => {});});+describe("archive navigation", () => {+ it.each([+ { destination: "/undo-env/thread", expected: "/draft/project" },+ { destination: "/undo-env/other-thread", expected: "/undo-env/other-thread" },+ { destination: "/settings", expected: "/settings" },+ ])("ends at $expected after opening $destination while archiving", async (route) => {+ router.state.location.href = "/undo-env/thread";+ router.state.matches[0]!.params = {+ environmentId: target.environmentId,+ threadId: target.threadId,+ };+ let finishArchive!: () => void;+ commands.archive.mockReturnValue(+ new Promise((resolve) => {+ finishArchive = () => resolve({ _tag: "Success", value: undefined });+ }),+ );+ const archived = useThreadActions().archiveThread(target);+ router.state.location.href = route.destination;+ finishArchive();+ expect((await archived)._tag).toBe("Success");+ expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: "Archived" });+ expect(router.state.location.href).toBe(route.expected);+ });+});+describe("settle and snooze Undo", () => {it("un-settles from the notice and expires the Undo after a manual un-settle", async () => {const add = vi.spyOn(toastManager, "add").mockReturnValue("toast");