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