Skip to content

Commit a300584

Browse files
refactor(mobile): extract the floating pill's connection status mapping
Moves the phase-to-pill mapping into a pure module so it can be tested without standing up the native view stack, and covers each connection phase and the reconnect handler. Co-Authored-By: Claude Code <[email protected]>
1 parent 916382d commit a300584

4 files changed

Lines changed: 130 additions & 60 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,10 @@ import { ComposerFeedback } from "./ComposerFeedback";
8282
import { ComposerUsageLimits } from "./ComposerUsageLimits";
8383
import { PendingUserInputCard } from "./PendingUserInputCard";
8484
import {
85-
connectionFloatingStatus,
8685
FLOATING_WORKING_CONTROL_COVERAGE,
8786
FloatingWorkingControl,
88-
type FloatingWorkingStatus,
8987
} from "./floating-working-control";
88+
import { connectionFloatingStatus, type FloatingWorkingStatus } from "./floating-working-status";
9089
import {
9190
derivePendingUserInputMaxHeight,
9291
ESTIMATED_KEYBOARD_HEIGHT,

‎apps/mobile/src/features/threads/floating-working-control.tsx‎

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
21
import { formatDuration } from "@t3tools/shared/orchestrationTiming";
32
import { GlassContainer, GlassView } from "expo-glass-effect";
43
import { type ReactNode, useEffect, useRef, useState } from "react";
@@ -24,6 +23,7 @@ import { AppText as Text } from "../../components/AppText";
2423
import { SymbolView } from "../../components/AppSymbol";
2524
import { ControlPill } from "../../components/ControlPill";
2625
import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
26+
import type { FloatingWorkingStatus } from "./floating-working-status";
2727

2828
const CONTROL_HEIGHT = 38.5; // h-11 with the mobile 14px rem
2929
// The collapsed composer capsule starts 6 below its overlay's top edge, so
@@ -58,63 +58,6 @@ const AnimatedGlassView = Animated.createAnimatedComponent(UniwindGlassView);
5858
const CONTROL_OVERLAY_OFFSET = CONTROL_HEIGHT + CONTROL_GAP - COMPOSER_CAPSULE_INSET;
5959
export const FLOATING_WORKING_CONTROL_COVERAGE = CONTROL_OVERLAY_OFFSET + CONTROL_GAP;
6060

61-
/**
62-
* What the floating pill says. Connection, syncing, and working share one
63-
* element so the label swaps in place instead of one pill fading out for
64-
* another. The connection variant is tappable and triggers a reconnect.
65-
*/
66-
export type FloatingWorkingStatus =
67-
| { readonly kind: "working"; readonly startedAt: string }
68-
| { readonly kind: "syncing"; readonly label: string }
69-
| { readonly kind: "compacting" }
70-
| {
71-
readonly kind: "connection";
72-
readonly tone: "reconnecting" | "unavailable";
73-
readonly label: string;
74-
readonly onPress: () => void;
75-
};
76-
77-
export function connectionFloatingStatus(input: {
78-
readonly connectionError: string | null;
79-
readonly connectionState: EnvironmentConnectionPhase;
80-
readonly environmentLabel: string | null;
81-
readonly onReconnect: () => void;
82-
}): FloatingWorkingStatus | null {
83-
const environmentLabel = input.environmentLabel ?? "Environment";
84-
const unavailable = (label: string): FloatingWorkingStatus => ({
85-
kind: "connection",
86-
tone: "unavailable",
87-
label,
88-
onPress: input.onReconnect,
89-
});
90-
91-
switch (input.connectionState) {
92-
case "connecting":
93-
case "reconnecting":
94-
return {
95-
kind: "connection",
96-
tone: "reconnecting",
97-
label:
98-
input.connectionError === null
99-
? `Reconnecting to ${environmentLabel}...`
100-
: `Failed to connect. Retrying ${environmentLabel}...`,
101-
onPress: input.onReconnect,
102-
};
103-
case "offline":
104-
return unavailable("You are offline");
105-
case "error":
106-
return unavailable(
107-
input.connectionError
108-
? `Failed to connect to ${environmentLabel}: ${input.connectionError}`
109-
: `Failed to connect to ${environmentLabel}`,
110-
);
111-
case "available":
112-
return unavailable(`${environmentLabel} is not connected`);
113-
case "connected":
114-
return null;
115-
}
116-
}
117-
11861
export function FloatingWorkingControl(props: {
11962
readonly colorScheme: "light" | "dark";
12063
readonly status: FloatingWorkingStatus | null;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { describe, expect, it, vi } from "vite-plus/test";
2+
3+
import { connectionFloatingStatus } from "./floating-working-status";
4+
5+
const status = (
6+
connectionState: Parameters<typeof connectionFloatingStatus>[0]["connectionState"],
7+
overrides: { connectionError?: string | null; environmentLabel?: string | null } = {},
8+
) =>
9+
connectionFloatingStatus({
10+
connectionError: overrides.connectionError ?? null,
11+
connectionState,
12+
environmentLabel:
13+
overrides.environmentLabel === undefined ? "Mac mini" : overrides.environmentLabel,
14+
onReconnect: () => {},
15+
});
16+
17+
describe("connectionFloatingStatus", () => {
18+
it("yields the pill to sync and working state once connected", () => {
19+
expect(status("connected")).toBeNull();
20+
});
21+
22+
it("names the environment it is retrying, and says so only after a failure", () => {
23+
expect(status("connecting")).toMatchObject({
24+
tone: "reconnecting",
25+
label: "Reconnecting to Mac mini...",
26+
});
27+
expect(status("reconnecting", { connectionError: "ECONNREFUSED" })).toMatchObject({
28+
tone: "reconnecting",
29+
label: "Failed to connect. Retrying Mac mini...",
30+
});
31+
});
32+
33+
it("reports why the environment is unreachable", () => {
34+
expect(status("offline")).toMatchObject({
35+
tone: "unavailable",
36+
label: "You are offline",
37+
});
38+
expect(status("available")).toMatchObject({
39+
tone: "unavailable",
40+
label: "Mac mini is not connected",
41+
});
42+
expect(status("error", { connectionError: "handshake timed out" })).toMatchObject({
43+
label: "Failed to connect to Mac mini: handshake timed out",
44+
});
45+
expect(status("error")).toMatchObject({ label: "Failed to connect to Mac mini" });
46+
});
47+
48+
it("falls back to a generic name when the environment has no label", () => {
49+
expect(status("error", { environmentLabel: null })).toMatchObject({
50+
label: "Failed to connect to Environment",
51+
});
52+
});
53+
54+
it("carries the reconnect handler so the pill can trigger it", () => {
55+
const onReconnect = vi.fn();
56+
const pill = connectionFloatingStatus({
57+
connectionError: null,
58+
connectionState: "offline",
59+
environmentLabel: "Mac mini",
60+
onReconnect,
61+
});
62+
if (pill?.kind !== "connection") throw new Error("expected a connection pill");
63+
pill.onPress();
64+
expect(onReconnect).toHaveBeenCalledOnce();
65+
});
66+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2+
3+
/**
4+
* What the floating pill says. Connection, syncing, and working share one
5+
* element so the label swaps in place instead of one pill fading out for
6+
* another. The connection variant is tappable and triggers a reconnect.
7+
*/
8+
export type FloatingWorkingStatus =
9+
| { readonly kind: "working"; readonly startedAt: string }
10+
| { readonly kind: "syncing"; readonly label: string }
11+
| { readonly kind: "compacting" }
12+
| {
13+
readonly kind: "connection";
14+
readonly tone: "reconnecting" | "unavailable";
15+
readonly label: string;
16+
readonly onPress: () => void;
17+
};
18+
19+
/**
20+
* The pill's connection variant, or null once the environment is connected and
21+
* the pill is free to report sync and working state instead.
22+
*/
23+
export function connectionFloatingStatus(input: {
24+
readonly connectionError: string | null;
25+
readonly connectionState: EnvironmentConnectionPhase;
26+
readonly environmentLabel: string | null;
27+
readonly onReconnect: () => void;
28+
}): FloatingWorkingStatus | null {
29+
const environmentLabel = input.environmentLabel ?? "Environment";
30+
const unavailable = (label: string): FloatingWorkingStatus => ({
31+
kind: "connection",
32+
tone: "unavailable",
33+
label,
34+
onPress: input.onReconnect,
35+
});
36+
37+
switch (input.connectionState) {
38+
case "connecting":
39+
case "reconnecting":
40+
return {
41+
kind: "connection",
42+
tone: "reconnecting",
43+
label:
44+
input.connectionError === null
45+
? `Reconnecting to ${environmentLabel}...`
46+
: `Failed to connect. Retrying ${environmentLabel}...`,
47+
onPress: input.onReconnect,
48+
};
49+
case "offline":
50+
return unavailable("You are offline");
51+
case "error":
52+
return unavailable(
53+
input.connectionError
54+
? `Failed to connect to ${environmentLabel}: ${input.connectionError}`
55+
: `Failed to connect to ${environmentLabel}`,
56+
);
57+
case "available":
58+
return unavailable(`${environmentLabel} is not connected`);
59+
case "connected":
60+
return null;
61+
}
62+
}

0 commit comments

Comments
 (0)