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

ConnectionEnvironmentRow.tsx

254 lines9.7 KB

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