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

ConnectionEnvironmentRow.tsx

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