Skip to content

Commit c84acfa

Browse files
only21milcodexclaude
authored
fix(web): keep later navigation when thread archival completes (#24109)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 34766e6 commit c84acfa

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎apps/web/src/hooks/useThreadActions.permissions.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ vi.mock("react", () => ({
4343
useRef: (current: unknown) => ({ current }),
4444
}));
4545
vi.mock("@tanstack/react-router", () => ({
46-
useRouter: () => ({ state: { matches: [] }, navigate: async () => {} }),
46+
useRouter: () => ({ state: { location: { href: "/" }, matches: [] }, navigate: async () => {} }),
4747
}));
4848
vi.mock("../state/session", () => ({
4949
environmentSession: { sessionStateAtom: "session" },

‎apps/web/src/hooks/useThreadActions.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ export function useThreadActions() {
374374
);
375375
}
376376

377+
const requestingRouteHref = router.state.location.href;
377378
const currentRouteThreadRef = getCurrentRouteThreadRef();
378379
const shouldNavigateToDraft =
379380
currentRouteThreadRef?.threadId === threadRef.threadId &&
@@ -400,7 +401,7 @@ export function useThreadActions() {
400401
failureTitle: "Failed to undo archive",
401402
});
402403

403-
if (shouldNavigateToDraft) {
404+
if (shouldNavigateToDraft && router.state.location.href === requestingRouteHref) {
404405
const navigationResult = await settlePromise(() =>
405406
handleNewThread(scopeProjectRef(thread.environmentId, thread.projectId)),
406407
);

‎apps/web/src/hooks/useThreadActions.undo.test.ts‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const commands = vi.hoisted(() => ({
1919
}));
2020
const router = vi.hoisted(() => ({
2121
navigate: vi.fn(async () => {}),
22-
state: { matches: [{ params: {} as Record<string, string> }] },
22+
state: { location: { href: "/" }, matches: [{ params: {} as Record<string, string> }] },
2323
}));
2424
vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() }));
2525
vi.mock("../state/session", async (original) => ({
@@ -34,7 +34,11 @@ vi.mock("react", async (original) => ({
3434
}));
3535
vi.mock("@tanstack/react-router", () => ({ useRouter: () => router }));
3636
vi.mock("./useSettings", () => ({ useClientSettings: () => false }));
37-
vi.mock("./useHandleNewThread", () => ({ useNewThreadHandler: () => vi.fn() }));
37+
vi.mock("./useHandleNewThread", () => ({
38+
useNewThreadHandler: () => async () => {
39+
router.state.location.href = "/draft/project";
40+
},
41+
}));
3842
vi.mock("../composerDraftStore", () => ({ useComposerDraftStore: () => vi.fn() }));
3943
vi.mock("../terminalUiStateStore", () => ({ useTerminalUiStateStore: () => vi.fn() }));
4044
vi.mock("../uiStateStore", () => ({ useUiStateStore: () => vi.fn() }));
@@ -94,6 +98,7 @@ beforeEach(() => {
9498
command.mockReset().mockResolvedValue({ _tag: "Success", value: undefined });
9599
}
96100
router.navigate.mockClear();
101+
router.state.location.href = "/";
97102
router.state.matches[0]!.params = {};
98103
threadShell.pinnedAt = null;
99104
threadShell.snoozedUntil = null;
@@ -201,6 +206,32 @@ describe("archive Undo", () => {
201206
});
202207
});
203208

209+
describe("archive navigation", () => {
210+
it.each([
211+
{ destination: "/undo-env/thread", expected: "/draft/project" },
212+
{ destination: "/undo-env/other-thread", expected: "/undo-env/other-thread" },
213+
{ destination: "/settings", expected: "/settings" },
214+
])("ends at $expected after opening $destination while archiving", async (route) => {
215+
router.state.location.href = "/undo-env/thread";
216+
router.state.matches[0]!.params = {
217+
environmentId: target.environmentId,
218+
threadId: target.threadId,
219+
};
220+
let finishArchive!: () => void;
221+
commands.archive.mockReturnValue(
222+
new Promise((resolve) => {
223+
finishArchive = () => resolve({ _tag: "Success", value: undefined });
224+
}),
225+
);
226+
const archived = useThreadActions().archiveThread(target);
227+
router.state.location.href = route.destination;
228+
finishArchive();
229+
expect((await archived)._tag).toBe("Success");
230+
expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: "Archived" });
231+
expect(router.state.location.href).toBe(route.expected);
232+
});
233+
});
234+
204235
describe("settle and snooze Undo", () => {
205236
it("un-settles from the notice and expires the Undo after a manual un-settle", async () => {
206237
const add = vi.spyOn(toastManager, "add").mockReturnValue("toast");

0 commit comments

Comments
 (0)