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

ConnectionEnvironmentRow.tsx

220 lines8.4 KB

1import { SymbolView } from "expo-symbols";
2import { connectionStatusText } from "@t3tools/client-runtime/connection";
3import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
4import type { EnvironmentId } from "@t3tools/contracts";
5import * as Cause from "effect/Cause";
6import { AsyncResult } from "effect/unstable/reactivity";
7import { useCallback, useState } from "react";
8import { Alert, Pressable, View } from "react-native";
9import Animated, { FadeIn, FadeOut, LinearTransition } from "react-native-reanimated";
10import { useThemeColor } from "../../lib/useThemeColor";
11​
12import { AppText as Text, AppTextInput as TextInput } from "../../components/AppText";
13import { cn } from "../../lib/cn";
14import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic";
15import type { ConnectedEnvironmentSummary } from "../../state/remote-runtime-types";
16import { ConnectionStatusDot } from "./ConnectionStatusDot";
17​
18function connectionStatusLabel(environment: ConnectedEnvironmentSummary): string | null {
19 return connectionStatusText({
20 phase: environment.connectionState,
21 error: environment.connectionError,
22 traceId: environment.connectionErrorTraceId,
23 });
25​
26export function ConnectionEnvironmentRow(props: {
27 readonly environment: ConnectedEnvironmentSummary;
28 readonly expanded: boolean;
29 readonly onToggle: () => void;
30 readonly onReconnect: (environmentId: EnvironmentId) => void;
31 readonly onRemove: (environmentId: EnvironmentId) => void;
32 readonly onUpdate: (
33 environmentId: EnvironmentId,
34 updates: { readonly label: string; readonly displayUrl: string },
35 ) => Promise<AtomCommandResult<unknown, unknown>>;
36}) {
37 const [label, setLabel] = useState(props.environment.environmentLabel);
38 const [url, setUrl] = useState(props.environment.displayUrl);
39​
40 const mutedColor = useThemeColor("--color-icon-subtle");
41 const placeholderColor = useThemeColor("--color-placeholder");
42 const primaryFg = useThemeColor("--color-primary-foreground");
43 const dangerFg = useThemeColor("--color-danger-foreground");
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 <Text className="text-base font-t3-bold leading-snug text-foreground" numberOfLines={1}>
80 {props.environment.environmentLabel}
81 </Text>
82 <Text className="text-xs text-foreground-muted" numberOfLines={1}>
83 {props.environment.displayUrl}
84 </Text>
85 {statusLabel ? (
86 <Text
87 className={cn(
88 "text-xs",
89 hasConnectionFailure ? "text-rose-500 dark:text-rose-400" : "text-foreground-muted",
90 )}
91 numberOfLines={props.expanded ? undefined : 1}
92 selectable={props.expanded}
93 >
94 {statusLabel}
95 {statusTraceId ? (
96 <>
97 {" Trace ID: "}
98 <Text
99 accessibilityHint="Copies the trace ID"
100 accessibilityRole="button"
101 className="underline"
102 onLongPress={(event) => {
103 event.stopPropagation();
104 copyTextWithHaptic(statusTraceId, { target: "connection-trace-id" });
105 }}
106 onPress={(event) => {
107 event.stopPropagation();
108 }}
109 style={{ textDecorationStyle: "dotted" }}
110 >
111 {statusTraceId}
112 </Text>
113 </>
114 ) : null}
115 </Text>
116 ) : null}
117 </View>
118​
119 <SymbolView
120 name="chevron.down"
121 size={12}
122 tintColor={mutedColor}
123 type="monochrome"
124 style={{
125 transform: [{ rotate: props.expanded ? "180deg" : "0deg" }],
126 }}
127 />
128 </Pressable>
129​
130 {props.expanded ? (
131 <Animated.View
132 entering={FadeIn.duration(200)}
133 exiting={FadeOut.duration(150)}
134 className="gap-3 px-4 pb-4"
135 >
136 {props.environment.isRelayManaged ? (
137 <Text className="text-sm text-foreground-muted">
138 Managed by T3 Connect. Tunnel details update automatically.
139 </Text>
140 ) : (
141 <>
142 <View className="gap-1.5">
143 <Text
144 className="text-2xs font-t3-bold uppercase text-foreground-muted"
145 style={{ letterSpacing: 0.8 }}
146 >
147 Label
148 </Text>
149 <TextInput
150 autoCapitalize="words"
151 autoCorrect={false}
152 placeholder="My MacBook"
153 placeholderTextColor={placeholderColor}
154 value={label}
155 onChangeText={setLabel}
156 className="rounded-[14px] border border-input-border bg-input px-4 py-3 text-base text-foreground"
157 />
158 </View>
159​
160 <View className="gap-1.5">
161 <Text
162 className="text-2xs font-t3-bold uppercase text-foreground-muted"
163 style={{ letterSpacing: 0.8 }}
164 >
165 URL
166 </Text>
167 <TextInput
168 autoCapitalize="none"
169 autoCorrect={false}
170 keyboardType="url"
171 placeholder="192.168.1.100:8080"
172 placeholderTextColor={placeholderColor}
173 value={url}
174 onChangeText={setUrl}
175 className="rounded-[14px] border border-input-border bg-input px-4 py-3 text-base text-foreground"
176 />
177 </View>
178 </>
179 )}
180​
181 <View className="flex-row justify-end gap-2">
182 {props.environment.isRelayManaged ? null : (
183 <Pressable
184 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"
185 onPress={handleSave}
186 >
187 <SymbolView name="checkmark" size={13} tintColor={primaryFg} type="monochrome" />
188 <Text
189 className="text-xs font-t3-bold uppercase text-primary-foreground"
190 style={{ letterSpacing: 0.8 }}
191 >
192 Save
193 </Text>
194 </Pressable>
195 )}
196​
197 <Pressable
198 className="h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-input-border bg-input active:opacity-70"
199 onPress={() => props.onReconnect(props.environment.environmentId)}
200 >
201 <SymbolView
202 name="arrow.clockwise"
203 size={14}
204 tintColor={mutedColor}
205 type="monochrome"
206 />
207 </Pressable>
208​
209 <Pressable
210 className="h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-danger-border bg-danger active:opacity-70"
211 onPress={() => props.onRemove(props.environment.environmentId)}
212 >
213 <SymbolView name="trash" size={14} tintColor={dangerFg} type="monochrome" />
214 </Pressable>
215 </View>
216 </Animated.View>
217 ) : null}
218 </Animated.View>
219 );