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