GeezerVison is showing github.com — fix(web): keep later navigation when thread deletion completes (#33953) · Pipyakas/t3code@451e375
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/web/src/hooks/useThreadActions.permissions.test.ts +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,7 +55,11 @@ vi.mock("react", () => ({ | 55 | 55 | useRef: (current: unknown) => ({ current }), | 56 | 56 | })); | 57 | 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 | + }), | 59 | 63 | })); | 60 | 64 | vi.mock("../state/session", () => ({ | 61 | 65 | environmentSession: { sessionStateAtom: "session" }, | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/hooks/useThreadActions.ts +8-2Lines changed: 8 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -588,10 +588,16 @@ export function useThreadActions() { | 588 | 588 | sortOrder: sidebarThreadSortOrder, | 589 | 589 | now: new Date().toISOString(), | 590 | 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 | + }); | 591 | 597 | const deleteResult = await deleteThreadMutation({ | 592 | 598 | environmentId: threadRef.environmentId, | 593 | 599 | input: { threadId: threadRef.threadId }, | 594 | | - }); | | 600 | + }).finally(stopWatchingHistory); | 595 | 601 | if (deleteResult._tag === "Failure") { | 596 | 602 | return deleteResult; | 597 | 603 | } | @@ -615,7 +621,7 @@ export function useThreadActions() { | 615 | 621 | clearTerminalUiState(threadRef); | 616 | 622 | clearDeletedThreadPanelState(threadRef); | 617 | 623 |
| 618 | | - if (shouldNavigateToFallback) { | | 624 | + if (shouldNavigateToFallback && !navigatedDuringDelete) { | 619 | 625 | const fallbackThread = fallbackThreadId | 620 | 626 | ? readThreadShell(scopeThreadRef(threadRef.environmentId, fallbackThreadId)) | 621 | 627 | : null; | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/hooks/useThreadActions.undo.test.ts +33-4Lines changed: 33 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,10 +19,20 @@ const commands = vi.hoisted(() => ({ | 19 | 19 | snooze: vi.fn(), | 20 | 20 | unsnooze: vi.fn(), | 21 | 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 | +}); | 26 | 36 | vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() })); | 27 | 37 | vi.mock("../state/session", async (original) => ({ | 28 | 38 | ...(await original<typeof import("../state/session")>()), | @@ -251,6 +261,25 @@ describe("archive navigation", () => { | 251 | 261 | }); | 252 | 262 | }); | 253 | 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 | + | 254 | 283 | describe("settle and snooze Undo", () => { | 255 | 284 | it("un-settles from the notice and expires the Undo after a manual un-settle", async () => { | 256 | 285 | const add = vi.spyOn(toastManager, "add").mockReturnValue("toast"); | |
|