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

ConnectionEnvironmentRow.tsx

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