The best way to control Claude Code, Codex and more from any computer or phone

main
ts 99 lines 3.16 KB
Raw
1 import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2 import { msg } from "gt-react-native";
3
4 import { m, type MessageResolver } from "../../i18n/messages";
5
6 export interface FloatingStatusAction {
7 readonly label: string;
8 readonly accessibilityLabel: string;
9 readonly disabled: boolean;
10 readonly onPress: () => void;
11 }
12
13 export type FloatingWorkingStatus =
14 | {
15 readonly kind: "child-input";
16 readonly label: string;
17 readonly accessibilityLabel: string;
18 readonly onPress: () => void;
19 }
20 | {
21 readonly kind: "stop";
22 readonly label: string;
23 readonly unresponsive: boolean;
24 readonly action: FloatingStatusAction | null;
25 }
26 | {
27 readonly kind: "goal";
28 readonly label: string;
29 readonly accessibilityLabel: string;
30 readonly actions: ReadonlyArray<FloatingStatusAction>;
31 }
32 | { readonly kind: "extension"; readonly label: string; readonly onPress: () => void }
33 | { readonly kind: "preparing"; readonly label: string }
34 | {
35 readonly kind: "connection";
36 readonly label: string;
37 readonly onPress: () => void;
38 };
39
40 /** A connection still retrying on its own; short enough for the thread header subtitle. */
41 export function connectionRetryLabel(
42 input: {
43 readonly connectionError: string | null;
44 readonly connectionState: EnvironmentConnectionPhase;
45 readonly connectionAwaitingApproval?: boolean;
46 },
47 resolve: MessageResolver = m,
48 ): string | null {
49 if (input.connectionState === "reconnecting" && input.connectionAwaitingApproval) {
50 return resolve(msg("Awaiting device approval"));
51 }
52 if (input.connectionState !== "connecting" && input.connectionState !== "reconnecting") {
53 return null;
54 }
55 return input.connectionError === null
56 ? resolve(msg("Reconnecting…"))
57 : resolve(msg("Retrying connection…"));
58 }
59
60 export function connectionFloatingStatus(
61 input: {
62 readonly connectionError: string | null;
63 readonly connectionState: EnvironmentConnectionPhase;
64 readonly environmentLabel: string | null;
65 readonly onReconnect: () => void;
66 },
67 resolve: MessageResolver = m,
68 ): FloatingWorkingStatus | null {
69 const environmentLabel = input.environmentLabel ?? resolve(msg("Environment"));
70 const unavailable = (label: string): FloatingWorkingStatus => ({
71 kind: "connection",
72 label,
73 onPress: input.onReconnect,
74 });
75
76 switch (input.connectionState) {
77 case "offline":
78 return unavailable(resolve(msg("You are offline")));
79 case "unsupported":
80 return unavailable(resolve(msg("Client not supported")));
81 case "error":
82 return unavailable(
83 input.connectionError
84 ? resolve(
85 msg("Failed to connect to {environmentLabel}: {connectionError}", {
86 environmentLabel,
87 connectionError: input.connectionError,
88 }),
89 )
90 : resolve(msg("Failed to connect to {environmentLabel}", { environmentLabel })),
91 );
92 case "available":
93 return unavailable(resolve(msg("{environmentLabel} is not connected", { environmentLabel })));
94 case "connecting":
95 case "reconnecting":
96 case "connected":
97 return null;
98 }
99 }