Open History to see the last commit
1import { SymbolView } from "../../components/AppSymbol";
2import { connectionStatusText } from "@t3tools/client-runtime/connection";
3import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
4import { type EnvironmentId, resolveEnvironmentMachineKind } from "@t3tools/contracts";
5import { useAtomValue } from "@effect/atom-react";
6import * as Cause from "effect/Cause";
7import { AsyncResult } from "effect/unstable/reactivity";
8import { useCallback, useState } from "react";
9import { Platform, Alert, Pressable, View } from "react-native";
10import Animated, { FadeIn, FadeOut, LinearTransition } from "react-native-reanimated";
11
12import { AppText as Text, AppTextInput as TextInput } from "../../components/AppText";
13import { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSymbol";
14import { MaterialButton } from "../../components/MaterialButton";
15import { MaterialIconButton } from "../../components/MaterialIconButton";
16import { ThemedSwitch } from "../../components/ThemedSwitch";
17import { cn } from "../../lib/cn";
18import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic";
19import type { ConnectedEnvironmentSummary } from "../../state/remote-runtime-types";
20import { serverEnvironment } from "../../state/server";
21import { ConnectionStatusDot } from "./ConnectionStatusDot";
22
23function connectionStatusLabel(environment: ConnectedEnvironmentSummary): string | null {
24 if (!environment.isEnabled && environment.connectionState !== "unsupported") {
25 return "Off";
26 }
27 return connectionStatusText({
28 phase: environment.connectionState,
29 error: environment.connectionError,
30 traceId: environment.connectionErrorTraceId,
31 });
32}
33
34export function ConnectionEnvironmentRow(props: {
35 readonly environment: ConnectedEnvironmentSummary;
36 readonly expanded: boolean;
37 readonly onToggle: () => void;
38 readonly onReconnect: (environmentId: EnvironmentId) => void;
39 readonly onRemove: (environmentId: EnvironmentId) => void;
40 readonly onSetEnabled: (environmentId: EnvironmentId, enabled: boolean) => void;
41 readonly onUpdate: (
42 environmentId: EnvironmentId,
43 updates: { readonly label: string; readonly displayUrl: string },
44 ) => Promise<AtomCommandResult<unknown, unknown>>;
45}) {
46 const [label, setLabel] = useState(props.environment.environmentLabel);
47 const [url, setUrl] = useState(props.environment.displayUrl);
48 const serverConfig = useAtomValue(
49 serverEnvironment.configValueAtom(props.environment.environmentId),
50 );
51 const unsupported = props.environment.connectionState === "unsupported";
52 const enabled = props.environment.isEnabled && !unsupported;
53 const statusLabel = connectionStatusLabel(props.environment);
54 const statusTraceId = enabled ? props.environment.connectionErrorTraceId : null;
55 // Unsupported is a compatibility note, not a failure, so it stays muted.
56 const hasConnectionFailure = enabled && props.environment.connectionError !== null;
57 const isRetrying =
58 enabled &&
59 (props.environment.connectionState === "connecting" ||
60 props.environment.connectionState === "reconnecting");
61 const handleSave = useCallback(async () => {
62 const result = await props.onUpdate(props.environment.environmentId, {
63 label: label.trim(),
64 displayUrl: url.trim(),
65 });
66 if (AsyncResult.isSuccess(result)) {
67 props.onToggle();
68 return;
69 }
70 const error = Cause.squash(result.cause);
71 Alert.alert(
72 "Could not update environment",
73 error instanceof Error ? error.message : "The environment could not be updated.",
74 );
75 }, [label, url, props]);
76
77 return (
78 <Animated.View layout={LinearTransition.duration(250)} className="bg-card">
79 <Pressable
80 className="flex-row items-center gap-3 px-4 py-3.5 active:opacity-70"
81 onPress={props.onToggle}
82 >
83 <ConnectionStatusDot
84 state={enabled || unsupported ? props.environment.connectionState : "available"}
85 pulse={isRetrying}
86 size={8}
87 />
88
89 <View className="flex-1 gap-0.5">
90 <View className="flex-row items-center gap-1.5">
91 <EnvironmentMachineSymbol
92 kind={resolveEnvironmentMachineKind(serverConfig)}
93 size={14}
94 tintColorClassName="accent-foreground-muted"
95 />
96 <Text
97 className="min-w-0 flex-shrink text-base font-t3-bold leading-snug text-foreground"
98 numberOfLines={1}
99 >
100 {props.environment.environmentLabel}
101 </Text>
102 </View>
103 <Text className="text-xs text-foreground-muted" numberOfLines={1}>
104 {props.environment.displayUrl}
105 </Text>
106 {statusLabel ? (
107 <Text
108 className={cn(
109 "text-xs",
110 hasConnectionFailure ? "text-danger-foreground" : "text-foreground-muted",
111 )}
112 numberOfLines={props.expanded ? undefined : 1}
113 selectable={props.expanded}
114 >
115 {statusLabel}
116 {statusTraceId ? (
117 <>
118 {" Trace ID: "}
119 <Text
120 accessibilityHint="Copies the trace ID"
121 accessibilityRole="button"
122 className="underline decoration-dotted"
123 onLongPress={(event) => {
124 event.stopPropagation();
125 copyTextWithHaptic(statusTraceId, { target: "connection-trace-id" });
126 }}
127 onPress={(event) => {
128 event.stopPropagation();
129 }}
130 >
131 {statusTraceId}
132 </Text>
133 </>
134 ) : null}
135 </Text>
136 ) : null}
137 </View>
138
139 <ThemedSwitch
140 disabled={unsupported}
141 onValueChange={(next) => props.onSetEnabled(props.environment.environmentId, next)}
142 value={enabled}
143 />
144 <SymbolView
145 name="chevron.down"
146 size={12}
147 tintColorClassName="accent-icon-subtle"
148 type="monochrome"
149 style={{
150 transform: [{ rotate: props.expanded ? "180deg" : "0deg" }],
151 }}
152 />
153 </Pressable>
154
155 {props.expanded ? (
156 <Animated.View
157 entering={FadeIn.duration(200)}
158 exiting={FadeOut.duration(150)}
159 className="gap-3 px-4 pb-4"
160 >
161 {props.environment.isRelayManaged ? (
162 <Text className="text-sm text-foreground-muted">
163 Managed by T3 Connect. Tunnel details update automatically.
164 </Text>
165 ) : (
166 <>
167 <View className="gap-1.5">
168 <Text className="text-2xs font-t3-bold tracking-[0.8px] uppercase text-foreground-muted">
169 Label
170 </Text>
171 <TextInput
172 autoCapitalize="words"
173 autoCorrect={false}
174 placeholder="My MacBook"
175 value={label}
176 onChangeText={setLabel}
177 className="rounded-[14px] border border-input-border bg-input px-4 py-3 text-base text-foreground"
178 />
179 </View>
180
181 <View className="gap-1.5">
182 <Text className="text-2xs font-t3-bold tracking-[0.8px] uppercase text-foreground-muted">
183 URL
184 </Text>
185 <TextInput
186 autoCapitalize="none"
187 autoCorrect={false}
188 keyboardType="url"
189 placeholder="192.168.1.100:8080"
190 value={url}
191 onChangeText={setUrl}
192 className="rounded-[14px] border border-input-border bg-input px-4 py-3 text-base text-foreground"
193 />
194 </View>
195 </>
196 )}
197
198 {Platform.OS === "android" ? (
199 <View className="flex-row items-center justify-end gap-2">
200 {props.environment.isRelayManaged ? null : (
201 <View className="flex-1">
202 <MaterialButton
203 label="Save"
204 tone="primary"
205 fullWidth
206 onPress={() => {
207 void handleSave();
208 }}
209 />
210 </View>
211 )}
212 <MaterialIconButton
213 accessibilityLabel="Reconnect environment"
214 icon="arrow.clockwise"
215 variant="tonal"
216 disabled={!enabled}
217 onPress={() => props.onReconnect(props.environment.environmentId)}
218 />
219 <MaterialIconButton
220 accessibilityLabel="Remove environment"
221 icon="trash"
222 variant="danger"
223 onPress={() => props.onRemove(props.environment.environmentId)}
224 />
225 </View>
226 ) : (
227 <View className="flex-row justify-end gap-2">
228 {props.environment.isRelayManaged ? null : (
229 <Pressable
230 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"
231 onPress={handleSave}
232 >
233 <SymbolView
234 name="checkmark"
235 size={13}
236 tintColorClassName="accent-primary-foreground"
237 type="monochrome"
238 />
239 <Text className="text-xs font-t3-bold tracking-[0.8px] uppercase text-primary-foreground">
240 Save
241 </Text>
242 </Pressable>
243 )}
244
245 <Pressable
246 className="h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-input-border bg-input active:opacity-70 disabled:opacity-40"
247 disabled={!enabled}
248 onPress={() => props.onReconnect(props.environment.environmentId)}
249 >
250 <SymbolView
251 name="arrow.clockwise"
252 size={14}
253 tintColorClassName="accent-icon-subtle"
254 type="monochrome"
255 />
256 </Pressable>
257
258 <Pressable
259 className="h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-danger-border bg-danger active:opacity-70"
260 onPress={() => props.onRemove(props.environment.environmentId)}
261 >
262 <SymbolView
263 name="trash"
264 size={14}
265 tintColorClassName="accent-danger-foreground"
266 type="monochrome"
267 />
268 </Pressable>
269 </View>
270 )}
271 </Animated.View>
272 ) : null}
273 </Animated.View>
274 );
275}