TypeScript
fix(mobile): a thread opened while reconnecting no longer turns into a new draft (#26565)
shivama42bdd4e5164
diff --git a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx b/apps/mobile/src/features/threads/ThreadRouteScreen.tsxindex ce352bf17eb..737e1aff90a 100644--- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx+++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx@@ -95,7 +95,11 @@ import {ThreadInspectorContentStack,type ThreadInspectorMode,} from "./thread-inspector-content-stack";-import { threadRouteIsHydrating } from "./thread-route-hydration";+import {+ threadDetailErrorIsCurrent,+ threadRouteIsHydrating,+ threadRouteOpensDraft,+} from "./thread-route-hydration";import { ThreadHeader } from "./ThreadHeader";import {useAndroidWorkspaceBack,@@ -265,6 +269,13 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) {? null: scopedThreadKey(selectedThread.environmentId, selectedThread.id);const selectedThreadDetailState = useSelectedThreadDetailState();+ const detailError = selectedThreadDetailState.error;+ const [errorBeforeConnected, setErrorBeforeConnected] = useState(() =>+ routeConnectionState === "connected" ? Option.none<string>() : detailError,+ );+ if (routeConnectionState !== "connected" && errorBeforeConnected !== detailError) {+ setErrorBeforeConnected(detailError);+ }const withInspectorHeader = (content: ReactNode) => (<>{props.inspectorPresentation ? (@@ -297,14 +308,25 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) {shellStatus: routeEnvironmentShellState.status,shellHasError: routeEnvironmentShellState.hasError,detailStatus: selectedThreadDetailState.status,- detailHasError: Option.isSome(selectedThreadDetailState.error),+ detailHasError: threadDetailErrorIsCurrent({+ connectionState: routeConnectionState,+ error: detailError,+ errorBeforeConnected,+ }),});if (stillHydrating) {return withInspectorHeader(<OpeningThreadLoadingScreen />);}- if (routeName === "Thread" && !props.inspectorPresentation) {+ if (+ routeName === "Thread" &&+ !props.inspectorPresentation &&+ threadRouteOpensDraft({+ connectionState: routeConnectionState,+ detailStatus: selectedThreadDetailState.status,+ })+ ) {return <UnavailableThreadDraftRedirect environmentId={environmentId} />;}diff --git a/apps/mobile/src/features/threads/thread-route-hydration.test.ts b/apps/mobile/src/features/threads/thread-route-hydration.test.tsindex 9d8ce88506f..9a7f1043609 100644--- a/apps/mobile/src/features/threads/thread-route-hydration.test.ts+++ b/apps/mobile/src/features/threads/thread-route-hydration.test.ts@@ -1,6 +1,11 @@+import * as Option from "effect/Option";import { describe, expect, it } from "vite-plus/test";-import { threadRouteIsHydrating } from "./thread-route-hydration";+import {+ threadDetailErrorIsCurrent,+ threadRouteIsHydrating,+ threadRouteOpensDraft,+} from "./thread-route-hydration";const settled = {isLoadingConnections: false,@@ -89,3 +94,62 @@ describe("threadRouteIsHydrating", () => {).toBe(false);});});++describe("threadRouteOpensDraft", () => {+ it("keeps the route while a reconnecting environment may still deliver the thread", () => {+ expect(threadRouteOpensDraft({ connectionState: "reconnecting", detailStatus: "empty" })).toBe(+ false,+ );+ });++ it("keeps the route while Reconnect retries a thread that failed to sync", () => {+ const failedDetail = {+ ...settled,+ connectionState: "connecting" as const,+ detailStatus: "empty" as const,+ detailHasError: true,+ };+ expect(threadRouteIsHydrating(failedDetail)).toBe(false);+ expect(threadRouteOpensDraft(failedDetail)).toBe(false);+ });++ it("opens a draft once the thread can't arrive", () => {+ expect(+ threadRouteOpensDraft({ connectionState: "reconnecting", detailStatus: "deleted" }),+ ).toBe(true);+ expect(threadRouteOpensDraft({ connectionState: "offline", detailStatus: "empty" })).toBe(true);+ });+});++describe("threadDetailErrorIsCurrent", () => {+ const oldError = Option.some("Could not synchronize the thread.");++ it("ignores the error a thread kept from before the connection came back", () => {+ const detailHasError = threadDetailErrorIsCurrent({+ connectionState: "connected",+ error: oldError,+ errorBeforeConnected: oldError,+ });+ expect(detailHasError).toBe(false);+ expect(threadRouteIsHydrating({ ...settled, detailStatus: "empty", detailHasError })).toBe(+ true,+ );+ });++ it("counts an error the live connection reported", () => {+ expect(+ threadDetailErrorIsCurrent({+ connectionState: "connected",+ error: Option.some("Could not synchronize the thread."),+ errorBeforeConnected: oldError,+ }),+ ).toBe(true);+ expect(+ threadDetailErrorIsCurrent({+ connectionState: "reconnecting",+ error: oldError,+ errorBeforeConnected: oldError,+ }),+ ).toBe(true);+ });+});diff --git a/apps/mobile/src/features/threads/thread-route-hydration.ts b/apps/mobile/src/features/threads/thread-route-hydration.tsindex 626e34cd7bb..a6e3454d476 100644--- a/apps/mobile/src/features/threads/thread-route-hydration.ts+++ b/apps/mobile/src/features/threads/thread-route-hydration.ts@@ -1,6 +1,7 @@import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell";import type { EnvironmentThreadStatus } from "@t3tools/client-runtime/state/threads";+import * as Option from "effect/Option";export function threadRouteIsHydrating(input: {readonly isLoadingConnections: boolean;@@ -41,3 +42,34 @@ export function threadRouteIsHydrating(input: {(input.connectionState === "connected" && input.detailStatus === "empty"));}++/**+ * Replacing the route with a draft can't be undone, so a thread that may still arrive keeps its+ * route: a reconnecting environment often delivers it moments later, and Reconnect starts a+ * fresh connecting attempt.+ */+export function threadRouteOpensDraft(input: {+ readonly connectionState: EnvironmentConnectionPhase;+ readonly detailStatus: EnvironmentThreadStatus;+}): boolean {+ if (input.detailStatus === "deleted") {+ return true;+ }+ return input.connectionState !== "reconnecting" && input.connectionState !== "connecting";+}++/**+ * A thread keeps its sync error until the new connection resubscribes, so the first renders after+ * a reconnect still show the old connection's error. Only an error the live connection reported+ * counts.+ */+export function threadDetailErrorIsCurrent<E>(input: {+ readonly connectionState: EnvironmentConnectionPhase;+ readonly error: Option.Option<E>;+ readonly errorBeforeConnected: Option.Option<E>;+}): boolean {+ return (+ Option.isSome(input.error) &&+ !(input.connectionState === "connected" && input.error === input.errorBeforeConnected)+ );+}