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

ConnectionEnvironmentRow.tsx

212 lines8.0 KB

1import { SymbolView } from "../../components/AppSymbol";
2import { connectionStatusText } from "@t3tools/client-runtime/connection";
3import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
4import type { EnvironmentId } from "@t3tools/contracts";
5import * as Cause from "effect/Cause";
6import { AsyncResult } from "effect/unstable/reactivity";
7import { useCallback, useState } from "react";
8import { Alert, Pressable, View } from "react-native";
9import Animated, { FadeIn, FadeOut, LinearTransition } from "react-native-reanimated";
10​
11import { AppText as Text, AppTextInput as TextInput } from "../../components/AppText";
12import { cn } from "../../lib/cn";
13import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic";
14import type { ConnectedEnvironmentSummary } from "../../state/remote-runtime-types";
15import { ConnectionStatusDot } from "./ConnectionStatusDot";
16​
17function connectionStatusLabel(environment: ConnectedEnvironmentSummary): string | null {
18 return connectionStatusText({
19 phase: environment.connectionState,
20 error: environment.connectionError,
21 traceId: environment.connectionErrorTraceId,
22 });
24​
25export function ConnectionEnvironmentRow(props: {
26 readonly environment: ConnectedEnvironmentSummary;
27 readonly expanded: boolean;
28 readonly onToggle: () => void;
29 readonly onReconnect: (environmentId: EnvironmentId) => void;
30 readonly onRemove: (environmentId: EnvironmentId) => void;
31 readonly onUpdate: (
32 environmentId: EnvironmentId,
33 updates: { readonly label: string; readonly displayUrl: string },
34 ) => Promise<AtomCommandResult<unknown, unknown>>;
35}) {
36 const [label, setLabel] = useState(props.environment.environmentLabel);
37 const [url, setUrl] = useState(props.environment.displayUrl);
38 const statusLabel = connectionStatusLabel(props.environment);
39 const statusTraceId = props.environment.connectionErrorTraceId;
40 const hasConnectionFailure = props.environment.connectionError !== null;
41 const isRetrying =
42 props.environment.connectionState === "connecting" ||
43 props.environment.connectionState === "reconnecting";
44 const handleSave = useCallback(async () => {
45 const result = await props.onUpdate(props.environment.environmentId, {
46 label: label.trim(),
47 displayUrl: url.trim(),
48 });
49 if (AsyncResult.isSuccess(result)) {
50 props.onToggle();
51 return;
52 }
53 const error = Cause.squash(result.cause);
54 Alert.alert(
55 "Could not update environment",
56 error instanceof Error ? error.message : "The environment could not be updated.",
57 );
58 }, [label, url, props]);
59​
60 return (
61 <Animated.View layout={LinearTransition.duration(250)} className="bg-card">
62 <Pressable
63 className="flex-row items-center gap-3 px-4 py-3.5 active:opacity-70"
64 onPress={props.onToggle}
65 >
66 <ConnectionStatusDot
67 state={props.environment.connectionState}
68 pulse={isRetrying}
69 size={8}
70 />
71​
72 <View className="flex-1 gap-0.5">
73 <Text className="text-base font-t3-bold leading-snug text-foreground" numberOfLines={1}>
74 {props.environment.environmentLabel}
75 </Text>
76 <Text className="text-xs text-foreground-muted" numberOfLines={1}>
77 {props.environment.displayUrl}
78 </Text>
79 {statusLabel ? (
80 <Text
81 className={cn(
82 "text-xs",
83 hasConnectionFailure ? "text-adaptive-rose-500-400" : "text-foreground-muted",
84 )}
85 numberOfLines={props.expanded ? undefined : 1}
86 selectable={props.expanded}
87 >
88 {statusLabel}
89 {statusTraceId ? (
90 <>
91 {" Trace ID: "}
92 <Text
93 accessibilityHint="Copies the trace ID"
94 accessibilityRole="button"
95 className="underline decoration-dotted"
96 onLongPress={(event) => {
97 event.stopPropagation();
98 copyTextWithHaptic(statusTraceId, { target: "connection-trace-id" });
99 }}
100 onPress={(event) => {
101 event.stopPropagation();
102 }}
103 >
104 {statusTraceId}
105 </Text>
106 </>
107 ) : null}
108 </Text>
109 ) : null}
110 </View>
111​
112 <SymbolView
113 name="chevron.down"
114 size={12}
115 tintColorClassName={"accent-icon-subtle"}
116 type="monochrome"
117 style={{
118 transform: [{ rotate: props.expanded ? "180deg" : "0deg" }],
119 }}
120 />
121 </Pressable>
122​
123 {props.expanded ? (
124 <Animated.View
125 entering={FadeIn.duration(200)}
126 exiting={FadeOut.duration(150)}
127 className="gap-3 px-4 pb-4"
128 >
129 {props.environment.isRelayManaged ? (
130 <Text className="text-sm text-foreground-muted">
131 Managed by T3 Connect. Tunnel details update automatically.
132 </Text>
133 ) : (
134 <>
135 <View className="gap-1.5">
136 <Text className="text-2xs font-t3-bold tracking-[0.8px] uppercase text-foreground-muted">
137 Label
138 </Text>
139 <TextInput
140 autoCapitalize="words"
141 autoCorrect={false}
142 placeholder="My MacBook"
143 value={label}
144 onChangeText={setLabel}
145 className="rounded-[14px] border border-input-border bg-input px-4 py-3 text-base text-foreground"
146 />
147 </View>
148​
149 <View className="gap-1.5">
150 <Text className="text-2xs font-t3-bold tracking-[0.8px] uppercase text-foreground-muted">
151 URL
152 </Text>
153 <TextInput
154 autoCapitalize="none"
155 autoCorrect={false}
156 keyboardType="url"
157 placeholder="192.168.1.100:8080"
158 value={url}
159 onChangeText={setUrl}
160 className="rounded-[14px] border border-input-border bg-input px-4 py-3 text-base text-foreground"
161 />
162 </View>
163 </>
164 )}
165​
166 <View className="flex-row justify-end gap-2">
167 {props.environment.isRelayManaged ? null : (
168 <Pressable
169 className="min-h-[42px] flex-1 flex-row items-center justify-center gap-1.5 rounded-[14px] bg-primary px-3.5 py-2.5 active:opacity-70"
170 onPress={handleSave}
171 >
172 <SymbolView
173 name="checkmark"
174 size={13}
175 tintColorClassName={"accent-primary-foreground"}
176 type="monochrome"
177 />
178 <Text className="text-xs font-t3-bold tracking-[0.8px] uppercase text-primary-foreground">
179 Save
180 </Text>
181 </Pressable>
182 )}
183​
184 <Pressable
185 className="h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-input-border bg-input active:opacity-70"
186 onPress={() => props.onReconnect(props.environment.environmentId)}
187 >
188 <SymbolView
189 name="arrow.clockwise"
190 size={14}
191 tintColorClassName={"accent-icon-subtle"}
192 type="monochrome"
193 />
194 </Pressable>
195​
196 <Pressable
197 className="h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-danger-border bg-danger active:opacity-70"
198 onPress={() => props.onRemove(props.environment.environmentId)}
199 >
200 <SymbolView
201 name="trash"
202 size={14}
203 tintColorClassName={"accent-danger-foreground"}
204 type="monochrome"
205 />
206 </Pressable>
207 </View>
208 </Animated.View>
209 ) : null}
210 </Animated.View>
211 );