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

ConnectionEnvironmentRow.tsx

200 lines7.1 KB

1import { SymbolView } from "expo-symbols";
2import type { EnvironmentId } from "@t3tools/contracts";
3import { useCallback, useState } from "react";
4import { Pressable, View } from "react-native";
5import Animated, { FadeIn, FadeOut, LinearTransition } from "react-native-reanimated";
6import { useThemeColor } from "../../lib/useThemeColor";
7​
8import { AppText as Text, AppTextInput as TextInput } from "../../components/AppText";
9import type { ConnectedEnvironmentSummary } from "../../state/remote-runtime-types";
10import { ConnectionStatusDot } from "./ConnectionStatusDot";
11​
12function connectionStatusLabel(environment: ConnectedEnvironmentSummary): string | null {
13 if (environment.connectionError) {
14 return null;
15 }
16​
17 switch (environment.connectionState) {
18 case "ready":
19 return "Connected";
20 case "connecting":
21 return "Connecting";
22 case "reconnecting":
23 return "Reconnecting";
24 case "disconnected":
25 return null;
26 case "idle":
27 return null;
28 }
30​
31export function ConnectionEnvironmentRow(props: {
32 readonly environment: ConnectedEnvironmentSummary;
33 readonly expanded: boolean;
34 readonly onToggle: () => void;
35 readonly onReconnect: (environmentId: EnvironmentId) => void;
36 readonly onRemove: (environmentId: EnvironmentId) => void;
37 readonly onUpdate: (
38 environmentId: EnvironmentId,
39 updates: { readonly label: string; readonly displayUrl: string },
40 ) => void;
41}) {
42 const [label, setLabel] = useState(props.environment.environmentLabel);
43 const [url, setUrl] = useState(props.environment.displayUrl);
44​
45 const mutedColor = useThemeColor("--color-icon-subtle");
46 const placeholderColor = useThemeColor("--color-placeholder");
47 const primaryFg = useThemeColor("--color-primary-foreground");
48 const dangerFg = useThemeColor("--color-danger-foreground");
49 const statusLabel = connectionStatusLabel(props.environment);
50​
51 const handleSave = useCallback(() => {
52 props.onUpdate(props.environment.environmentId, {
53 label: label.trim(),
54 displayUrl: url.trim(),
55 });
56 props.onToggle();
57 }, [label, url, props]);
58​
59 return (
60 <Animated.View layout={LinearTransition.duration(250)} className="bg-card">
61 <Pressable
62 className="flex-row items-center gap-3 px-4 py-3.5 active:opacity-70"
63 onPress={props.onToggle}
64 >
65 <ConnectionStatusDot
66 state={props.environment.connectionState}
67 pulse={
68 props.environment.connectionState === "connecting" ||
69 props.environment.connectionState === "reconnecting"
70 }
71 size={8}
72 />
73​
74 <View className="flex-1 gap-0.5">
75 <Text
76 className="text-[16px] font-t3-bold leading-[21px] text-foreground"
77 numberOfLines={1}
78 >
79 {props.environment.environmentLabel}
80 </Text>
81 <Text className="text-[12px] leading-[16px] text-foreground-muted" numberOfLines={1}>
82 {props.environment.displayUrl}
83 </Text>
84 {statusLabel ? (
85 <Text className="text-[12px] leading-[16px] text-foreground-muted" numberOfLines={1}>
86 {statusLabel}
87 </Text>
88 ) : null}
89 {props.environment.connectionError ? (
90 <Text
91 className="text-[12px] leading-[16px] text-rose-500 dark:text-rose-400"
92 numberOfLines={2}
93 >
94 {props.environment.connectionError}
95 </Text>
96 ) : null}
97 </View>
98​
99 <SymbolView
100 name="chevron.down"
101 size={12}
102 tintColor={mutedColor}
103 type="monochrome"
104 style={{
105 transform: [{ rotate: props.expanded ? "180deg" : "0deg" }],
106 }}
107 />
108 </Pressable>
109​
110 {props.expanded ? (
111 <Animated.View
112 entering={FadeIn.duration(200)}
113 exiting={FadeOut.duration(150)}
114 className="gap-3 px-4 pb-4"
115 >
116 {props.environment.isRelayManaged ? (
117 <Text className="text-[13px] leading-[18px] text-foreground-muted">
118 Managed by T3 Cloud. Tunnel details update automatically.
119 </Text>
120 ) : (
121 <>
122 <View className="gap-1.5">
123 <Text
124 className="text-[11px] font-t3-bold uppercase text-foreground-muted"
125 style={{ letterSpacing: 0.8 }}
126 >
127 Label
128 </Text>
129 <TextInput
130 autoCapitalize="words"
131 autoCorrect={false}
132 placeholder="My MacBook"
133 placeholderTextColor={placeholderColor}
134 value={label}
135 onChangeText={setLabel}
136 className="rounded-[14px] border border-input-border bg-input px-4 py-3 text-[15px] text-foreground"
137 />
138 </View>
139​
140 <View className="gap-1.5">
141 <Text
142 className="text-[11px] font-t3-bold uppercase text-foreground-muted"
143 style={{ letterSpacing: 0.8 }}
144 >
145 URL
146 </Text>
147 <TextInput
148 autoCapitalize="none"
149 autoCorrect={false}
150 keyboardType="url"
151 placeholder="192.168.1.100:8080"
152 placeholderTextColor={placeholderColor}
153 value={url}
154 onChangeText={setUrl}
155 className="rounded-[14px] border border-input-border bg-input px-4 py-3 text-[15px] text-foreground"
156 />
157 </View>
158 </>
159 )}
160​
161 <View className="flex-row justify-end gap-2">
162 {props.environment.isRelayManaged ? null : (
163 <Pressable
164 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"
165 onPress={handleSave}
166 >
167 <SymbolView name="checkmark" size={13} tintColor={primaryFg} type="monochrome" />
168 <Text
169 className="text-[12px] font-t3-bold uppercase text-primary-foreground"
170 style={{ letterSpacing: 0.8 }}
171 >
172 Save
173 </Text>
174 </Pressable>
175 )}
176​
177 <Pressable
178 className="h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-input-border bg-input active:opacity-70"
179 onPress={() => props.onReconnect(props.environment.environmentId)}
180 >
181 <SymbolView
182 name="arrow.clockwise"
183 size={14}
184 tintColor={mutedColor}
185 type="monochrome"
186 />
187 </Pressable>
188​
189 <Pressable
190 className="h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-danger-border bg-danger active:opacity-70"
191 onPress={() => props.onRemove(props.environment.environmentId)}
192 >
193 <SymbolView name="trash" size={14} tintColor={dangerFg} type="monochrome" />
194 </Pressable>
195 </View>
196 </Animated.View>
197 ) : null}
198 </Animated.View>
199 );