fix(mobile): wait for thread deep link hydration (#11502)

This commit is contained in:
Michel Liao authored and GitHub committed 2026-09-15 10:28:18 -07:00
1 parent d1790aa146
commit caf8b5d798
4 files changed
+176 -6

No files matched your search

@@ -25,6 +25,7 @@ import {
import { Alert, Platform, ScrollView, View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { useWorkspaceState } from "../../state/workspace";
import { useEnvironmentShellState } from "../../state/shell";
import { restoredNewTaskDraftKey } from "../../state/new-task-draft-key";
import { clearPendingThreadCreationOutcome } from "../../state/pending-thread-creation";
import { recoverFailedThreadDraft } from "../../state/recover-failed-thread-draft";
@@ -88,6 +89,7 @@ import {
ThreadInspectorContentStack,
type ThreadInspectorMode,
} from "./thread-inspector-content-stack";
import { threadRouteIsHydrating } from "./thread-route-hydration";
interface ThreadInspectorSelection {
readonly routeThreadIdentity: string | null;
@@ -123,7 +125,11 @@ interface ThreadRouteScreenProps extends ThreadRouteScreenRouteProps {
readonly renderInspector?: (headerInset: number) => ReactNode;
}
function ThreadUnavailableScreen() {
/** Shows recovery only after the target route has reached a terminal unavailable state. */
function ThreadUnavailableScreen(props: {
readonly actionLabel: string;
readonly onAction: () => void;
}) {
return (
<ScrollView
contentInsetAdjustmentBehavior="automatic"
@@ -138,6 +144,8 @@ function ThreadUnavailableScreen() {
<EmptyState
title="Thread unavailable"
detail="This thread is not available in the current mobile snapshot."
actionLabel={props.actionLabel}
onAction={props.onAction}
/>
</ScrollView>
);
@@ -152,6 +160,9 @@ export function ThreadRouteScreen(props: ThreadRouteScreenProps) {
const threadIdRaw = firstRouteParam(params.threadId);
const environmentId = environmentIdRaw ? EnvironmentId.make(environmentIdRaw) : null;
const routeEnvironmentRuntime = useRemoteEnvironmentRuntime(environmentId);
const routeEnvironmentShellState = useEnvironmentShellState(environmentId);
const { onReconnectEnvironment } = useRemoteConnections();
const navigation = useNavigation();
const routeConnectionState =
routeEnvironmentRuntime?.connectionState ?? (environmentId ? "available" : connectionState);
const routeThreadKey =
@@ -177,16 +188,36 @@ export function ThreadRouteScreen(props: ThreadRouteScreenProps) {
return <ThreadRouteContent {...props} selectedThreadDetailState={selectedThreadDetailState} />;
}
const stillHydrating =
workspaceState.isLoadingConnections ||
routeConnectionState === "connecting" ||
routeConnectionState === "reconnecting";
const stillHydrating = threadRouteIsHydrating({
isLoadingConnections: workspaceState.isLoadingConnections,
connectionState: routeConnectionState,
shellStatus: routeEnvironmentShellState.status,
shellHasError: Option.isSome(routeEnvironmentShellState.error),
detailStatus: selectedThreadDetailState.status,
detailHasError: Option.isSome(selectedThreadDetailState.error),
});
if (stillHydrating) {
return <OpeningThreadLoadingScreen />;
}
return <ThreadUnavailableScreen />;
return (
<ThreadUnavailableScreen
actionLabel={
routeEnvironmentRuntime === null ? "Manage environments" : "Reconnect environment"
}
onAction={() => {
if (routeEnvironmentRuntime !== null) {
onReconnectEnvironment(environmentId);
return;
}
navigation.navigate("SettingsSheet", {
screen: "SettingsContent",
params: { screen: "SettingsEnvironments" },
});
}}
/>
);
}
function ThreadRouteContent(
@@ -0,0 +1,74 @@
import { describe, expect, it } from "vite-plus/test";
import { threadRouteIsHydrating } from "./thread-route-hydration";
const settled = {
isLoadingConnections: false,
connectionState: "connected" as const,
shellStatus: "live" as const,
shellHasError: false,
detailStatus: "live" as const,
detailHasError: false,
};
describe("threadRouteIsHydrating", () => {
it("waits for shell and thread detail hydration", () => {
expect(threadRouteIsHydrating({ ...settled, shellStatus: "synchronizing" })).toBe(true);
expect(threadRouteIsHydrating({ ...settled, shellStatus: "empty" })).toBe(true);
expect(threadRouteIsHydrating({ ...settled, detailStatus: "synchronizing" })).toBe(true);
expect(threadRouteIsHydrating({ ...settled, detailStatus: "empty" })).toBe(true);
});
it("stops waiting once an empty detail has an actionable outcome", () => {
expect(
threadRouteIsHydrating({
...settled,
shellStatus: "empty",
shellHasError: true,
detailStatus: "deleted",
}),
).toBe(false);
expect(
threadRouteIsHydrating({
...settled,
detailStatus: "empty",
detailHasError: true,
}),
).toBe(false);
expect(
threadRouteIsHydrating({
...settled,
connectionState: "available",
detailStatus: "empty",
}),
).toBe(false);
expect(threadRouteIsHydrating({ ...settled, detailStatus: "deleted" })).toBe(false);
});
it("prioritizes terminal outcomes over unrelated hydration", () => {
expect(
threadRouteIsHydrating({
...settled,
connectionState: "reconnecting",
shellStatus: "synchronizing",
detailStatus: "deleted",
}),
).toBe(false);
expect(
threadRouteIsHydrating({
...settled,
connectionState: "reconnecting",
shellStatus: "synchronizing",
detailHasError: true,
}),
).toBe(false);
expect(
threadRouteIsHydrating({
...settled,
connectionState: "offline",
shellStatus: "synchronizing",
detailStatus: "synchronizing",
}),
).toBe(false);
});
});
@@ -0,0 +1,38 @@
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";
/**
* Reports whether the route-local projections can still yield the requested
* thread. Explicit terminal outcomes win over unrelated synchronization.
*/
export function threadRouteIsHydrating(input: {
readonly isLoadingConnections: boolean;
readonly connectionState: EnvironmentConnectionPhase;
readonly shellStatus: EnvironmentShellStatus;
readonly shellHasError: boolean;
readonly detailStatus: EnvironmentThreadStatus;
readonly detailHasError: boolean;
}): boolean {
if (input.detailStatus === "deleted" || input.shellHasError || input.detailHasError) {
return false;
}
if (input.isLoadingConnections) {
return true;
}
if (
input.connectionState === "available" ||
input.connectionState === "offline" ||
input.connectionState === "error"
) {
return false;
}
return (
input.connectionState === "connecting" ||
input.connectionState === "reconnecting" ||
input.shellStatus === "synchronizing" ||
(input.connectionState === "connected" && input.shellStatus === "empty") ||
input.detailStatus === "synchronizing" ||
(input.connectionState === "connected" && input.detailStatus === "empty")
);
}
+27
View File
@@ -3,7 +3,12 @@ import {
createEnvironmentShellSummaryAtom,
createEnvironmentSnapshotAtom,
createShellEnvironmentAtoms,
type EnvironmentShellState,
} from "@t3tools/client-runtime/state/shell";
import { useAtomValue } from "@effect/atom-react";
import type { EnvironmentId } from "@t3tools/contracts";
import * as Option from "effect/Option";
import { AsyncResult, Atom } from "effect/unstable/reactivity";
import { environmentCatalog } from "../connection/catalog";
import { connectionAtomRuntime } from "../connection/runtime";
@@ -15,3 +20,25 @@ export const environmentShellSummaryAtom = createEnvironmentShellSummaryAtom({
catalogValueAtom: environmentCatalog.catalogValueAtom,
shellStateValueAtom: environmentShell.stateValueAtom,
});
const EMPTY_ENVIRONMENT_SHELL_STATE_ATOM = Atom.make(
AsyncResult.success<EnvironmentShellState>({
snapshot: Option.none(),
status: "empty",
error: Option.none(),
}),
).pipe(Atom.withLabel("mobile-environment-shell:empty"));
/** Reads one environment's shell projection without waiting on other environments. */
export function useEnvironmentShellState(environmentId: EnvironmentId | null) {
const result = useAtomValue(
environmentId === null
? EMPTY_ENVIRONMENT_SHELL_STATE_ATOM
: environmentShell.stateAtom(environmentId),
);
return Option.getOrElse(AsyncResult.value(result), () => ({
snapshot: Option.none(),
status: "empty" as const,
error: Option.none(),
}));
}