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