Open History to see the last commit
1import { ConnectionTraceId } from "./ConnectionTraceId";
2import {
3 type EnvironmentConnectionPhase,
4 type EnvironmentConnectionPresentation,
5} from "@t3tools/client-runtime/connection";
6import { SymbolView } from "../../components/AppSymbol";
7import { ActivityIndicator, Pressable, View } from "react-native";
8
9import { AppText as Text } from "../../components/AppText";
10
11function noticeTitle(phase: EnvironmentConnectionPhase, environmentLabel: string): string {
12 switch (phase) {
13 case "offline":
14 return "You are offline";
15 case "connecting":
16 return `Connecting to ${environmentLabel}...`;
17 case "reconnecting":
18 return `Reconnecting to ${environmentLabel}...`;
19 case "unsupported":
20 return "Client not supported";
21 case "error":
22 return `${environmentLabel} is unavailable`;
23 case "available":
24 return `${environmentLabel} is disconnected`;
25 case "connected":
26 return "";
27 }
28}
29
30function noticeDetail(
31 phase: EnvironmentConnectionPhase,
32 resourceName: string,
33 error: string | null,
34): string {
35 if (error) {
36 return phase === "reconnecting" ? `The app will keep retrying automatically. ${error}` : error;
37 }
38
39 switch (phase) {
40 case "offline":
41 return `Cached data remains available. The ${resourceName} will load when your connection returns.`;
42 case "connecting":
43 case "reconnecting":
44 return `The ${resourceName} will load as soon as the environment is ready.`;
45 case "unsupported":
46 return "Use compatible versions of the app and server to connect.";
47 case "available":
48 case "error":
49 return `Reconnect the environment to load the ${resourceName}.`;
50 case "connected":
51 return "";
52 }
53}
54
55export function EnvironmentConnectionNotice(props: {
56 readonly environmentLabel: string;
57 readonly connection: EnvironmentConnectionPresentation;
58 readonly resourceName: string;
59 readonly onRetry: () => void;
60}) {
61 const isRetrying =
62 props.connection.phase === "connecting" || props.connection.phase === "reconnecting";
63
64 return (
65 <View className="flex-1 items-center justify-center px-8">
66 <View className="max-w-[320px] items-center gap-3">
67 {isRetrying ? (
68 <ActivityIndicator size="small" colorClassName={"accent-icon-muted"} />
69 ) : (
70 <SymbolView
71 name={props.connection.phase === "offline" ? "wifi.slash" : "bolt.horizontal.circle"}
72 size={24}
73 tintColorClassName={"accent-icon-muted"}
74 type="monochrome"
75 />
76 )}
77
78 <Text className="text-center text-lg font-t3-bold text-foreground">
79 {noticeTitle(props.connection.phase, props.environmentLabel)}
80 </Text>
81 <Text className="text-center text-sm leading-normal text-foreground-muted">
82 {noticeDetail(props.connection.phase, props.resourceName, props.connection.error)}
83 {props.connection.traceId ? (
84 <ConnectionTraceId traceId={props.connection.traceId} />
85 ) : null}
86 </Text>
87
88 {props.connection.phase !== "offline" && props.connection.phase !== "unsupported" ? (
89 <Pressable
90 accessibilityRole="button"
91 className="mt-1 rounded-full bg-subtle px-4 py-2.5 active:opacity-70"
92 onPress={props.onRetry}
93 >
94 <Text className="text-sm font-t3-bold text-foreground">Retry now</Text>
95 </Pressable>
96 ) : null}
97 </View>
98 </View>
99 );
100}