The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 18:30 UTC
fix(web): keep later navigation when thread deletion completes (#33953)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Edge358 committed
Nov 12, 2026 at 15:51 UTC
451e3756a591eeaa65db6c710215c6ba77256fc9
3 files changed
+46
-7
apps/web/src/hooks/useThreadActions.permissions.test.ts
+5
-1
@@ -55,7 +55,11 @@ vi.mock("react", () => ({
55
useRef: (current: unknown) => ({ current }),
56
}));
57
vi.mock("@tanstack/react-router", () => ({
58
- useRouter: () => ({ state: { location: { href: "/" }, matches: [] }, navigate: async () => {} }),
58
+ useRouter: () => ({
59
+ state: { location: { href: "/" }, matches: [] },
60
+ history: { subscribe: () => () => {} },
61
+ navigate: async () => {},
62
+ }),
63
}));
64
vi.mock("../state/session", () => ({
65
environmentSession: { sessionStateAtom: "session" },
apps/web/src/hooks/useThreadActions.ts
+8
-2
@@ -588,10 +588,16 @@ export function useThreadActions() {
588
sortOrder: sidebarThreadSortOrder,
589
now: new Date().toISOString(),
590
});
591
+ let navigatedDuringDelete = false;
592
+ // The missing-thread redirect and the index draft replace this entry; any other history
593
+ // action while the delete is pending is the user's own choice, which must win.
594
+ const stopWatchingHistory = router.history.subscribe(({ action }) => {
595
+ if (action.type !== "REPLACE") navigatedDuringDelete = true;
596
+ });
597
const deleteResult = await deleteThreadMutation({
598
environmentId: threadRef.environmentId,
599
input: { threadId: threadRef.threadId },
594
- });
600
+ }).finally(stopWatchingHistory);
601
if (deleteResult._tag === "Failure") {
602
return deleteResult;
603
}
@@ -615,7 +621,7 @@ export function useThreadActions() {
621
clearTerminalUiState(threadRef);
622
clearDeletedThreadPanelState(threadRef);
623
618
- if (shouldNavigateToFallback) {
624
+ if (shouldNavigateToFallback && !navigatedDuringDelete) {
625
const fallbackThread = fallbackThreadId
626
? readThreadShell(scopeThreadRef(threadRef.environmentId, fallbackThreadId))
627
: null;
apps/web/src/hooks/useThreadActions.undo.test.ts
+33
-4
@@ -19,10 +19,20 @@ const commands = vi.hoisted(() => ({
19
snooze: vi.fn(),
20
unsnooze: vi.fn(),
21
}));
22
-const router = vi.hoisted(() => ({
23
- navigate: vi.fn(async () => {}),
24
- state: { location: { href: "/" }, matches: [{ params: {} as Record<string, string> }] },
25
-}));
22
+const router = vi.hoisted(() => {
23
+ const listeners = new Set<(event: { action: { type: string } }) => void>();
24
+ return {
25
+ navigate: vi.fn(async () => {}),
26
+ state: { location: { href: "/" }, matches: [{ params: {} as Record<string, string> }] },
27
+ history: {
28
+ listeners,
29
+ subscribe: (listener: (event: { action: { type: string } }) => void) => {
30
+ listeners.add(listener);
31
+ return () => listeners.delete(listener);
32
+ },
33
+ },
34
+ };
35
+});
36
vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() }));
37
vi.mock("../state/session", async (original) => ({
38
...(await original<typeof import("../state/session")>()),
@@ -251,6 +261,25 @@ describe("archive navigation", () => {
261
});
262
});
263
264
+describe("delete navigation", () => {
265
+ it.each([
266
+ { type: "REPLACE", by: "the app's missing-thread redirect", fallsBack: true },
267
+ { type: "PUSH", by: "the user opening another thread", fallsBack: false },
268
+ ])("falls back: $fallsBack after $by while deleting", async ({ type, fallsBack }) => {
269
+ router.state.matches[0]!.params = {
270
+ environmentId: target.environmentId,
271
+ threadId: target.threadId,
272
+ };
273
+ commands.delete.mockImplementation(async () => {
274
+ for (const listener of router.history.listeners) listener({ action: { type } });
275
+ return { _tag: "Success", value: undefined };
276
+ });
277
+ expect((await useThreadActions().deleteThread(target))._tag).toBe("Success");
278
+ expect(router.navigate).toHaveBeenCalledTimes(fallsBack ? 1 : 0);
279
+ expect(router.history.listeners.size).toBe(0);
280
+ });
281
+});
282
+
283
describe("settle and snooze Undo", () => {
284
it("un-settles from the notice and expires the Undo after a manual un-settle", async () => {
285
const add = vi.spyOn(toastManager, "add").mockReturnValue("toast");