Skip to content

Commit 38d9384

Browse files
fix(web): keep thread links open until the shell is live
Opening a link to a thread created since the client's last visit redirected to an unrelated thread: the route treated any shell snapshot, including the stale cache, as authoritative and reported the thread missing. Routes now decide a thread is missing only once the environment shell is live. While it is cached or synchronizing, the link stays put, and thread detail or a local draft that has already loaded is shown. A sync error leaves the shell non-live, so it no longer redirects either. Fixes #14689 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent b33eda1 commit 38d9384

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎apps/web/src/components/ThreadRouteView.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { useEnvironmentQuery } from "../state/query";
2626
import { environmentShell } from "../state/shell";
2727
import {
2828
buildThreadRouteParams,
29+
isThreadRouteSnapshotAuthoritative,
2930
resolveThreadRouteRenderState,
3031
type ThreadRouteTarget,
3132
} from "../threadRoutes";
@@ -82,7 +83,7 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
8283
const serverThreadDetail = useThreadDetail(serverThreadRef);
8384
const serverThreadStatus = useThreadStatus(serverThreadRef);
8485
const environmentThreadRefs = useEnvironmentThreadRefs(serverThreadRef?.environmentId ?? null);
85-
const bootstrapComplete = shell.data?.snapshot._tag === "Some";
86+
const bootstrapComplete = isThreadRouteSnapshotAuthoritative(shell.data?.status);
8687
const draftThread = useComposerDraftStore((store) =>
8788
serverThreadRef ? store.getDraftThreadByRef(serverThreadRef) : null,
8889
);

‎apps/web/src/threadRoutes.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ import { DraftId } from "./composerDraftStore";
66
import {
77
buildDraftThreadRouteParams,
88
buildThreadRouteParams,
9+
isThreadRouteSnapshotAuthoritative,
910
resolveActiveThreadRouteRef,
1011
resolveThreadRouteRenderState,
1112
resolveThreadRouteRef,
1213
resolveThreadRouteTarget,
1314
} from "./threadRoutes";
1415

1516
describe("threadRoutes", () => {
17+
it("does not treat cached thread lists as authoritative for deep links", () => {
18+
expect(isThreadRouteSnapshotAuthoritative("empty")).toBe(false);
19+
expect(isThreadRouteSnapshotAuthoritative("cached")).toBe(false);
20+
expect(isThreadRouteSnapshotAuthoritative("synchronizing")).toBe(false);
21+
expect(isThreadRouteSnapshotAuthoritative("live")).toBe(true);
22+
});
23+
1624
it("builds canonical thread route params from a scoped ref", () => {
1725
const ref = scopeThreadRef("env-1" as never, ThreadId.make("thread-1"));
1826

@@ -127,6 +135,26 @@ describe("threadRoutes", () => {
127135
).toBe("ready");
128136
});
129137

138+
it.each([
139+
{ status: "cached", source: "detail" },
140+
{ status: "synchronizing", source: "detail" },
141+
{ status: "cached", source: "draft" },
142+
{ status: "synchronizing", source: "draft" },
143+
] as const)(
144+
"keeps available $source visible while the shell is $status",
145+
({ status, source }) => {
146+
expect(
147+
resolveThreadRouteRenderState({
148+
bootstrapComplete: isThreadRouteSnapshotAuthoritative(status),
149+
serverThreadShellExists: false,
150+
serverThreadDetailExists: source === "detail",
151+
serverThreadDetailDeleted: false,
152+
draftThreadExists: source === "draft",
153+
}),
154+
).toBe("ready");
155+
},
156+
);
157+
130158
it("distinguishes bootstrap loading from a missing thread", () => {
131159
expect(
132160
resolveThreadRouteRenderState({
@@ -159,4 +187,16 @@ describe("threadRoutes", () => {
159187
}),
160188
).toBe("missing");
161189
});
190+
191+
it("waits for the live shell before redirecting a deleted thread", () => {
192+
expect(
193+
resolveThreadRouteRenderState({
194+
bootstrapComplete: isThreadRouteSnapshotAuthoritative("cached"),
195+
serverThreadShellExists: true,
196+
serverThreadDetailExists: false,
197+
serverThreadDetailDeleted: true,
198+
draftThreadExists: false,
199+
}),
200+
).toBe("loading");
201+
});
162202
});

‎apps/web/src/threadRoutes.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2+
import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell";
23
import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
34
import type { DraftId } from "./composerDraftStore";
45

@@ -20,19 +21,25 @@ type DraftThreadRouteState = {
2021

2122
export type ThreadRouteRenderState = "loading" | "ready" | "missing";
2223

24+
export function isThreadRouteSnapshotAuthoritative(
25+
status: EnvironmentShellStatus | undefined,
26+
): boolean {
27+
return status === "live";
28+
}
29+
2330
export function resolveThreadRouteRenderState(input: {
2431
bootstrapComplete: boolean;
2532
serverThreadShellExists: boolean;
2633
serverThreadDetailExists: boolean;
2734
serverThreadDetailDeleted: boolean;
2835
draftThreadExists: boolean;
2936
}): ThreadRouteRenderState {
30-
if (!input.bootstrapComplete) {
31-
return "loading";
32-
}
3337
if (input.serverThreadDetailExists || input.draftThreadExists) {
3438
return "ready";
3539
}
40+
if (!input.bootstrapComplete) {
41+
return "loading";
42+
}
3643
if (input.serverThreadDetailDeleted) {
3744
return "missing";
3845
}

0 commit comments

Comments
 (0)