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