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

ConnectionEnvironmentRow.tsx

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