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