Skip to content
TypeScript

fix(mobile): keep home brand visible during secondary retries (#21388)

maria327f3fc72cee
diff --git a/apps/mobile/src/features/home/workspace-connection-status.test.ts b/apps/mobile/src/features/home/workspace-connection-status.test.ts
index 1d7239fc018..3b50d7f78f7 100644
--- a/apps/mobile/src/features/home/workspace-connection-status.test.ts
+++ b/apps/mobile/src/features/home/workspace-connection-status.test.ts
@@ -9,6 +9,7 @@ function workspaceState(overrides: Partial<WorkspaceState> = {}): WorkspaceState
hasConnections: true,
hasLoadedShellSnapshot: true,
hasPendingShellSnapshot: false,
+ hasSyncingConnectedEnvironment: false,
hasReadyEnvironment: true,
hasConnectingEnvironment: false,
connectingEnvironments: [],
@@ -25,6 +26,66 @@ describe("workspace connection status", () => {
expect(workspaceConnectionStatusPresentation(workspaceState())).toBeNull();
});
+ it.each([
+ ["connecting", null],
+ ["reconnecting", null],
+ ["connecting", "Could not reach sleeping laptop"],
+ ["reconnecting", "Could not reach sleeping laptop"],
+ ["error", "Could not reach sleeping laptop"],
+ ] as const)(
+ "keeps the brand while a secondary environment is %s (%s)",
+ (connectionState, connectionError) => {
+ const state = workspaceState({
+ hasConnectingEnvironment: connectionState !== "error",
+ hasPendingShellSnapshot: true,
+ connectionError,
+ connectingEnvironments:
+ connectionState === "error"
+ ? []
+ : [
+ {
+ environmentId: "environment-2" as never,
+ environmentLabel: "Sleeping laptop",
+ displayUrl: "",
+ isRelayManaged: false,
+ isEnabled: true,
+ connectionState,
+ connectionError,
+ connectionErrorTraceId: null,
+ },
+ ],
+ });
+
+ expect(workspaceConnectionStatusPresentation(state)).toBeNull();
+ },
+ );
+
+ it("shows connected shell catch-up even while a secondary environment is unreachable", () => {
+ const state = workspaceState({
+ hasSyncingConnectedEnvironment: true,
+ hasPendingShellSnapshot: true,
+ hasConnectingEnvironment: true,
+ connectionError: "Could not reach sleeping laptop",
+ connectingEnvironments: [
+ {
+ environmentId: "environment-2" as never,
+ environmentLabel: "Sleeping laptop",
+ displayUrl: "",
+ isRelayManaged: false,
+ isEnabled: true,
+ connectionState: "reconnecting",
+ connectionError: "Could not reach sleeping laptop",
+ connectionErrorTraceId: null,
+ },
+ ],
+ });
+
+ expect(workspaceConnectionStatusPresentation(state)).toEqual({
+ label: "Syncing threads...",
+ showsProgress: true,
+ });
+ });
+
it("surfaces offline snapshots", () => {
const state = workspaceState({ networkStatus: "offline", hasReadyEnvironment: false });
@@ -72,7 +133,10 @@ describe("workspace connection status", () => {
});
it("shows shell catch-up while cached threads remain visible", () => {
- const state = workspaceState({ hasPendingShellSnapshot: true });
+ const state = workspaceState({
+ hasPendingShellSnapshot: true,
+ hasSyncingConnectedEnvironment: true,
+ });
expect(workspaceConnectionStatusPresentation(state)).toEqual({
label: "Syncing threads...",
@@ -84,6 +148,7 @@ describe("workspace connection status", () => {
const state = workspaceState({
hasLoadedShellSnapshot: false,
hasPendingShellSnapshot: true,
+ hasSyncingConnectedEnvironment: true,
});
expect(workspaceConnectionStatusPresentation(state)).toEqual({
diff --git a/apps/mobile/src/features/home/workspace-connection-status.ts b/apps/mobile/src/features/home/workspace-connection-status.ts
index 88ce6a3bde1..7bdb53b9b28 100644
--- a/apps/mobile/src/features/home/workspace-connection-status.ts
+++ b/apps/mobile/src/features/home/workspace-connection-status.ts
@@ -6,8 +6,9 @@ export interface WorkspaceConnectionStatusPresentation {
}
function shouldShowWorkspaceConnectionStatus(state: WorkspaceState): boolean {
+ if (state.networkStatus === "offline") return true;
+ if (state.hasReadyEnvironment) return state.hasSyncingConnectedEnvironment;
return (
- state.networkStatus === "offline" ||
state.connectionError !== null ||
state.hasConnectingEnvironment ||
state.hasPendingShellSnapshot ||
@@ -17,6 +18,9 @@ function shouldShowWorkspaceConnectionStatus(state: WorkspaceState): boolean {
function workspaceConnectionStatusLabel(state: WorkspaceState): string {
if (state.networkStatus === "offline") return "You are offline";
+ if (state.hasReadyEnvironment && state.hasSyncingConnectedEnvironment) {
+ return state.hasLoadedShellSnapshot ? "Syncing threads..." : "Loading threads...";
+ }
if (state.connectingEnvironments.length === 1) {
return `Reconnecting to ${state.connectingEnvironments[0]!.environmentLabel}`;
}
@@ -38,7 +42,7 @@ export function workspaceConnectionStatusPresentation(
label: workspaceConnectionStatusLabel(state),
showsProgress:
state.networkStatus !== "offline" &&
- state.connectionError === null &&
+ (state.hasReadyEnvironment || state.connectionError === null) &&
(state.connectingEnvironments.length > 0 || state.hasPendingShellSnapshot),
};
}
diff --git a/apps/mobile/src/state/workspace-connection-atoms.test.ts b/apps/mobile/src/state/workspace-connection-atoms.test.ts
index 96e0b4ef934..803d34bc448 100644
--- a/apps/mobile/src/state/workspace-connection-atoms.test.ts
+++ b/apps/mobile/src/state/workspace-connection-atoms.test.ts
@@ -62,6 +62,7 @@ describe("workspace connection subscriptions", () => {
networkStatus: get(h.network),
environments: [projectWorkspaceEnvironment(ID, get(h.source)!)],
shellSummary: get(shell),
+ hasSyncingConnectedEnvironment: false,
}),
);
const initial = h.registry.get(h.stateAtom);
diff --git a/apps/mobile/src/state/workspace.ts b/apps/mobile/src/state/workspace.ts
index edc253e243a..46fafdf265d 100644
--- a/apps/mobile/src/state/workspace.ts
+++ b/apps/mobile/src/state/workspace.ts
@@ -1,7 +1,8 @@
import { useAtomValue } from "@effect/atom-react";
import { useMemo } from "react";
+import { Atom } from "effect/reactivity";
-import { environmentShellSummaryAtom } from "./shell";
+import { environmentShell, environmentShellSummaryAtom } from "./shell";
import { projectWorkspaceState } from "./workspaceModel";
import { environmentCatalog } from "../connection/catalog";
import { environmentPresentations } from "./presentation";
@@ -13,6 +14,16 @@ export const workspaceConnections = createWorkspaceConnectionAtoms({
presentationAtom: environmentPresentations.presentationAtom,
});
+// A disconnected environment's retries also leave its shell synchronizing.
+const hasSyncingConnectedEnvironmentAtom = Atom.make((get) =>
+ get(workspaceConnections.environmentsAtom).some(
+ (environment) =>
+ environment.isEnabled &&
+ environment.connectionState === "connected" &&
+ get(environmentShell.stateValueAtom(environment.environmentId)).status === "synchronizing",
+ ),
+).pipe(Atom.withLabel("mobile:has-syncing-connected-environment"));
+
export function useWorkspaceEnvironments() {
return useAtomValue(workspaceConnections.environmentsAtom);
}
@@ -30,6 +41,7 @@ export function useWorkspaceState() {
const networkStatus = useAtomValue(environmentCatalog.networkStatusValueAtom);
const projectedEnvironments = useWorkspaceEnvironments();
const shellSummary = useAtomValue(environmentShellSummaryAtom);
+ const hasSyncingConnectedEnvironment = useAtomValue(hasSyncingConnectedEnvironmentAtom);
const state = useMemo(
() =>
projectWorkspaceState({
@@ -37,8 +49,9 @@ export function useWorkspaceState() {
networkStatus,
environments: projectedEnvironments,
shellSummary,
+ hasSyncingConnectedEnvironment,
}),
- [isReady, networkStatus, projectedEnvironments, shellSummary],
+ [isReady, networkStatus, projectedEnvironments, shellSummary, hasSyncingConnectedEnvironment],
);
return {
diff --git a/apps/mobile/src/state/workspaceModel.test.ts b/apps/mobile/src/state/workspaceModel.test.ts
index 60bc6830321..8a4f4451c64 100644
--- a/apps/mobile/src/state/workspaceModel.test.ts
+++ b/apps/mobile/src/state/workspaceModel.test.ts
@@ -77,6 +77,7 @@ describe("mobile workspace projection", () => {
networkStatus: "offline",
environments,
shellSummary: EMPTY_SHELL_SUMMARY,
+ hasSyncingConnectedEnvironment: false,
});
expect(state.connectionState).toBe("offline");
@@ -94,12 +95,14 @@ describe("mobile workspace projection", () => {
networkStatus: "online",
environments,
shellSummary: EMPTY_SHELL_SUMMARY,
+ hasSyncingConnectedEnvironment: true,
});
expect(state.connectingEnvironments).toHaveLength(1);
expect(state.connectingEnvironments[0]?.connectionState).toBe("reconnecting");
expect(state.hasConnectingEnvironment).toBe(true);
expect(state.hasReadyEnvironment).toBe(true);
+ expect(state.hasSyncingConnectedEnvironment).toBe(true);
});
it("keeps retained snapshots visible while reconnecting without claiming readiness", () => {
@@ -109,10 +112,12 @@ describe("mobile workspace projection", () => {
networkStatus: "online",
environments,
shellSummary: CACHED_SHELL_SUMMARY,
+ hasSyncingConnectedEnvironment: false,
});
expect(state.hasLoadedShellSnapshot).toBe(true);
expect(state.hasPendingShellSnapshot).toBe(true);
+ expect(state.hasSyncingConnectedEnvironment).toBe(false);
expect(state.hasReadyEnvironment).toBe(false);
expect(state.connectionState).toBe("reconnecting");
});
diff --git a/apps/mobile/src/state/workspaceModel.ts b/apps/mobile/src/state/workspaceModel.ts
index 5584a06a8c7..efd9071927f 100644
--- a/apps/mobile/src/state/workspaceModel.ts
+++ b/apps/mobile/src/state/workspaceModel.ts
@@ -25,6 +25,7 @@ export interface WorkspaceConnectionState {
export interface WorkspaceState extends WorkspaceConnectionState {
readonly hasLoadedShellSnapshot: boolean;
readonly hasPendingShellSnapshot: boolean;
+ readonly hasSyncingConnectedEnvironment: boolean;
readonly shellSnapshotError: string | null;
}
@@ -92,11 +93,13 @@ export function projectWorkspaceState(input: {
readonly networkStatus: NetworkStatus;
readonly environments: ReadonlyArray<WorkspaceEnvironment>;
readonly shellSummary: EnvironmentShellSummary;
+ readonly hasSyncingConnectedEnvironment: boolean;
}): WorkspaceState {
return {
...projectWorkspaceConnectionState(input),
hasLoadedShellSnapshot: input.shellSummary.hasSnapshot,
hasPendingShellSnapshot: input.shellSummary.hasSynchronizingShell,
+ hasSyncingConnectedEnvironment: input.hasSyncingConnectedEnvironment,
shellSnapshotError: input.shellSummary.firstError,
};
}