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

ConnectionEnvironmentRow.tsx

262 lines10.1 KB

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