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

EnvironmentConnectionNotice.tsx

150 lines5.1 KB

1import { msg, useGT, useMessages } from "gt-react-native";
2import { Button } from "../../components/ui/Button";
3import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
4import { ConnectionTraceId } from "./ConnectionTraceId";
5import {
6 connectionErrorText,
7 type EnvironmentConnectionPhase,
8 type EnvironmentConnectionPresentation,
9} from "@t3tools/client-runtime/connection";
10import { SymbolView } from "../../components/ui/AppSymbol";
11import { View } from "react-native";
12​
13import { AppText as Text } from "../../components/ui/AppText";
14import type { MessageResolver } from "../../i18n/messages";
15​
16function noticeTitle(
17 phase: EnvironmentConnectionPhase,
18 environmentLabel: string,
19 awaitingApproval: boolean,
20 resolve: MessageResolver,
21): string {
22 const environment = environmentLabel;
23 if (awaitingApproval) return resolve(msg`Waiting for ${{ environment }} to approve this device`);
24 switch (phase) {
25 case "offline":
26 return resolve(msg("You are offline"));
27 case "connecting":
28 return resolve(msg`Connecting to ${{ environment }}...`);
29 case "reconnecting":
30 return resolve(msg`Reconnecting to ${{ environment }}...`);
31 case "unsupported":
32 return resolve(msg("Client not supported"));
33 case "error":
34 return resolve(msg`${{ environment }} is unavailable`);
35 case "available":
36 return resolve(msg`${{ environment }} is disconnected`);
37 case "connected":
38 return "";
39 }
41​
42type ConnectionNoticeResource = "preview" | "review" | "terminal";
43​
44const OFFLINE_DETAIL = {
45 preview: msg(
46 "Cached data remains available. The preview will load when your connection returns.",
47 ),
48 review: msg("Cached data remains available. The review will load when your connection returns."),
49 terminal: msg(
50 "Cached data remains available. The terminal will load when your connection returns.",
51 ),
52} satisfies Record<ConnectionNoticeResource, string>;
53​
54const CONNECTING_DETAIL = {
55 preview: msg("The preview will load as soon as the environment is ready."),
56 review: msg("The review will load as soon as the environment is ready."),
57 terminal: msg("The terminal will load as soon as the environment is ready."),
58} satisfies Record<ConnectionNoticeResource, string>;
59​
60const RECONNECT_DETAIL = {
61 preview: msg("Reconnect the environment to load the preview."),
62 review: msg("Reconnect the environment to load the review."),
63 terminal: msg("Reconnect the environment to load the terminal."),
64} satisfies Record<ConnectionNoticeResource, string>;
65​
66function noticeDetail(
67 phase: EnvironmentConnectionPhase,
68 resource: ConnectionNoticeResource,
69 error: string | null,
70 awaitingApproval: boolean,
71 resolve: MessageResolver,
72): string {
73 if (error && awaitingApproval) return error;
74 if (error) {
75 return phase === "reconnecting"
76 ? resolve(msg`The app will keep retrying automatically. ${{ error }}`)
77 : error;
78 }
79​
80 switch (phase) {
81 case "offline":
82 return resolve(OFFLINE_DETAIL[resource]);
83 case "connecting":
84 case "reconnecting":
85 return resolve(CONNECTING_DETAIL[resource]);
86 case "unsupported":
87 return resolve(msg("Use compatible versions of the app and server to connect."));
88 case "available":
89 case "error":
90 return resolve(RECONNECT_DETAIL[resource]);
91 case "connected":
92 return "";
93 }
95​
96export function EnvironmentConnectionNotice(props: {
97 readonly environmentLabel: string;
98 readonly connection: EnvironmentConnectionPresentation;
99 readonly resource: ConnectionNoticeResource;
100 readonly onRetry: () => void;
101}) {
102 const gt = useGT();
103 const m = useMessages();
104 const isRetrying =
105 props.connection.phase === "connecting" || props.connection.phase === "reconnecting";
106​
107 return (
108 <View className="flex-1 items-center justify-center px-8">
109 <View className="max-w-[320px] items-center gap-3">
110 {isRetrying ? (
111 <AppActivityIndicator size="small" colorClassName={"accent-icon-muted"} />
112 ) : (
113 <SymbolView
114 name={props.connection.phase === "offline" ? "wifi.slash" : "bolt.horizontal.circle"}
115 size={24}
116 tintColorClassName={"accent-icon-muted"}
117 type="monochrome"
118 />
119 )}
120​
121 <Text className="text-center text-lg font-t3-bold text-foreground">
122 {noticeTitle(
123 props.connection.phase,
124 props.environmentLabel,
125 props.connection.awaitingApproval === true,
126 m,
127 )}
128 </Text>
129 <Text className="text-center text-sm leading-normal text-foreground-muted">
130 {noticeDetail(
131 props.connection.phase,
132 props.resource,
133 connectionErrorText(props.connection, m),
134 props.connection.awaitingApproval === true,
135 m,
136 )}
137 {props.connection.traceId ? (
138 <ConnectionTraceId traceId={props.connection.traceId} />
139 ) : null}
140 </Text>
141​
142 {props.connection.phase !== "offline" && props.connection.phase !== "unsupported" ? (
143 <View className="mt-1">
144 <Button label={gt`Retry now`} tone="secondary" onPress={props.onRetry} />
145 </View>
146 ) : null}
147 </View>
148 </View>
149 );