Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

floating-working-status.ts

94 lines3.1 KB

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