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

ConnectionEnvironmentRow.tsx

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