Skip to content

Commit 5b8f8f3

Browse files
shivamhwpclaude
andcommitted
fix(mobile): ignore a thread sync error left from before reconnecting
A thread keeps its sync error until the new connection resubscribes, so a render right after Reconnect could see a connected environment with the old error and replace the route with a draft. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 384dc6e commit 5b8f8f3

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎apps/mobile/src/features/threads/ThreadRouteScreen.tsx‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ import {
9595
ThreadInspectorContentStack,
9696
type ThreadInspectorMode,
9797
} from "./thread-inspector-content-stack";
98-
import { threadRouteIsHydrating, threadRouteOpensDraft } from "./thread-route-hydration";
98+
import {
99+
threadDetailErrorIsCurrent,
100+
threadRouteIsHydrating,
101+
threadRouteOpensDraft,
102+
} from "./thread-route-hydration";
99103
import { ThreadHeader } from "./ThreadHeader";
100104
import {
101105
useAndroidWorkspaceBack,
@@ -265,6 +269,13 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) {
265269
? null
266270
: scopedThreadKey(selectedThread.environmentId, selectedThread.id);
267271
const selectedThreadDetailState = useSelectedThreadDetailState();
272+
const detailError = selectedThreadDetailState.error;
273+
const [errorBeforeConnected, setErrorBeforeConnected] = useState(() =>
274+
routeConnectionState === "connected" ? Option.none<string>() : detailError,
275+
);
276+
if (routeConnectionState !== "connected" && errorBeforeConnected !== detailError) {
277+
setErrorBeforeConnected(detailError);
278+
}
268279
const withInspectorHeader = (content: ReactNode) => (
269280
<>
270281
{props.inspectorPresentation ? (
@@ -297,7 +308,11 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) {
297308
shellStatus: routeEnvironmentShellState.status,
298309
shellHasError: routeEnvironmentShellState.hasError,
299310
detailStatus: selectedThreadDetailState.status,
300-
detailHasError: Option.isSome(selectedThreadDetailState.error),
311+
detailHasError: threadDetailErrorIsCurrent({
312+
connectionState: routeConnectionState,
313+
error: detailError,
314+
errorBeforeConnected,
315+
}),
301316
});
302317

303318
if (stillHydrating) {

‎apps/mobile/src/features/threads/thread-route-hydration.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import * as Option from "effect/Option";
12
import { describe, expect, it } from "vite-plus/test";
23

3-
import { threadRouteIsHydrating, threadRouteOpensDraft } from "./thread-route-hydration";
4+
import {
5+
threadDetailErrorIsCurrent,
6+
threadRouteIsHydrating,
7+
threadRouteOpensDraft,
8+
} from "./thread-route-hydration";
49

510
const settled = {
611
isLoadingConnections: false,
@@ -115,3 +120,36 @@ describe("threadRouteOpensDraft", () => {
115120
expect(threadRouteOpensDraft({ connectionState: "offline", detailStatus: "empty" })).toBe(true);
116121
});
117122
});
123+
124+
describe("threadDetailErrorIsCurrent", () => {
125+
const oldError = Option.some("Could not synchronize the thread.");
126+
127+
it("ignores the error a thread kept from before the connection came back", () => {
128+
const detailHasError = threadDetailErrorIsCurrent({
129+
connectionState: "connected",
130+
error: oldError,
131+
errorBeforeConnected: oldError,
132+
});
133+
expect(detailHasError).toBe(false);
134+
expect(threadRouteIsHydrating({ ...settled, detailStatus: "empty", detailHasError })).toBe(
135+
true,
136+
);
137+
});
138+
139+
it("counts an error the live connection reported", () => {
140+
expect(
141+
threadDetailErrorIsCurrent({
142+
connectionState: "connected",
143+
error: Option.some("Could not synchronize the thread."),
144+
errorBeforeConnected: oldError,
145+
}),
146+
).toBe(true);
147+
expect(
148+
threadDetailErrorIsCurrent({
149+
connectionState: "reconnecting",
150+
error: oldError,
151+
errorBeforeConnected: oldError,
152+
}),
153+
).toBe(true);
154+
});
155+
});

‎apps/mobile/src/features/threads/thread-route-hydration.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
22
import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell";
33
import type { EnvironmentThreadStatus } from "@t3tools/client-runtime/state/threads";
4+
import * as Option from "effect/Option";
45

56
export function threadRouteIsHydrating(input: {
67
readonly isLoadingConnections: boolean;
@@ -56,3 +57,19 @@ export function threadRouteOpensDraft(input: {
5657
}
5758
return input.connectionState !== "reconnecting" && input.connectionState !== "connecting";
5859
}
60+
61+
/**
62+
* A thread keeps its sync error until the new connection resubscribes, so the first renders after
63+
* a reconnect still show the old connection's error. Only an error the live connection reported
64+
* counts.
65+
*/
66+
export function threadDetailErrorIsCurrent<E>(input: {
67+
readonly connectionState: EnvironmentConnectionPhase;
68+
readonly error: Option.Option<E>;
69+
readonly errorBeforeConnected: Option.Option<E>;
70+
}): boolean {
71+
return (
72+
Option.isSome(input.error) &&
73+
!(input.connectionState === "connected" && input.error === input.errorBeforeConnected)
74+
);
75+
}

0 commit comments

Comments
 (0)