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

ConnectionEnvironmentRow.tsx

243 lines9.4 KB

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