@@ -2,11 +2,15 @@ import { ConnectionTraceId } from "./ConnectionTraceId";
22import { SymbolView } from "../../components/AppSymbol" ;
33import { connectionStatusText } from "@t3tools/client-runtime/connection" ;
44import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime" ;
5- import { type EnvironmentId , resolveEnvironmentMachineKind } from "@t3tools/contracts" ;
5+ import {
6+ type EnvironmentId ,
7+ type ServerConfig ,
8+ resolveEnvironmentMachineKind ,
9+ } from "@t3tools/contracts" ;
610import { useAtomValue } from "@effect/atom-react" ;
711import * as Cause from "effect/Cause" ;
812import { AsyncResult } from "effect/reactivity" ;
9- import { useCallback , useState } from "react" ;
13+ import { useState } from "react" ;
1014import { Platform , Alert , Pressable , View } from "react-native" ;
1115import Animated , { FadeIn , FadeOut , LinearTransition } from "react-native-reanimated" ;
1216
@@ -20,6 +24,7 @@ import type { ConnectedEnvironmentSummary } from "../../state/remote-runtime-typ
2024import { serverEnvironment } from "../../state/server" ;
2125import { ConnectionFormField } from "./ConnectionFormField" ;
2226import { ConnectionStatusDot } from "./ConnectionStatusDot" ;
27+ import { environmentCatalog } from "../../connection/catalog" ;
2328
2429function connectionStatusLabel ( environment : ConnectedEnvironmentSummary ) : string | null {
2530 if ( ! environment . isEnabled && environment . connectionState !== "unsupported" ) {
@@ -32,7 +37,7 @@ function connectionStatusLabel(environment: ConnectedEnvironmentSummary): string
3237 } ) ;
3338}
3439
35- export function ConnectionEnvironmentRow ( props : {
40+ interface ConnectionEnvironmentRowProps {
3641 readonly environment : ConnectedEnvironmentSummary ;
3742 readonly expanded : boolean ;
3843 readonly opensDetails ?: boolean ;
@@ -44,9 +49,12 @@ export function ConnectionEnvironmentRow(props: {
4449 environmentId : EnvironmentId ,
4550 updates : { readonly label : string ; readonly displayUrl : string } ,
4651 ) => Promise < AtomCommandResult < unknown , unknown > > ;
47- } ) {
48- const [ label , setLabel ] = useState ( props . environment . environmentLabel ) ;
49- const [ url , setUrl ] = useState ( props . environment . displayUrl ) ;
52+ }
53+
54+ export function ConnectionEnvironmentRow ( props : ConnectionEnvironmentRowProps ) {
55+ const catalog = useAtomValue ( environmentCatalog . catalogValueAtom ) ;
56+ const isHostName =
57+ catalog . entries . get ( props . environment . environmentId ) ?. target . _tag === "RelayConnectionTarget" ;
5058 const serverConfig = useAtomValue (
5159 serverEnvironment . configValueAtom ( props . environment . environmentId ) ,
5260 ) ;
@@ -60,21 +68,6 @@ export function ConnectionEnvironmentRow(props: {
6068 enabled &&
6169 ( props . environment . connectionState === "connecting" ||
6270 props . environment . connectionState === "reconnecting" ) ;
63- const handleSave = useCallback ( async ( ) => {
64- const result = await props . onUpdate ( props . environment . environmentId , {
65- label : label . trim ( ) ,
66- displayUrl : url . trim ( ) ,
67- } ) ;
68- if ( AsyncResult . isSuccess ( result ) ) {
69- props . onToggle ( ) ;
70- return ;
71- }
72- const error = Cause . squash ( result . cause ) ;
73- Alert . alert (
74- "Could not update environment" ,
75- error instanceof Error ? error . message : "The environment could not be updated." ,
76- ) ;
77- } , [ label , url , props ] ) ;
7871
7972 return (
8073 < Animated . View layout = { LinearTransition . duration ( 250 ) } className = "bg-grouped-card" >
@@ -105,7 +98,7 @@ export function ConnectionEnvironmentRow(props: {
10598 { props . environment . environmentLabel }
10699 </ Text >
107100 </ View >
108- { ! props . environment . isRelayManaged && props . environment . displayUrl . trim ( ) ? (
101+ { ! isHostName && props . environment . displayUrl . trim ( ) ? (
109102 < Text className = "text-xs text-foreground-muted" numberOfLines = { 1 } >
110103 { props . environment . displayUrl }
111104 </ Text >
@@ -149,113 +142,181 @@ export function ConnectionEnvironmentRow(props: {
149142 </ Pressable >
150143
151144 { props . expanded ? (
152- < Animated . View
153- entering = { FadeIn . duration ( 200 ) }
154- exiting = { FadeOut . duration ( 150 ) }
155- className = "gap-3 px-4 pb-4"
156- >
157- { props . environment . isRelayManaged ? (
158- < Text className = "text-sm text-foreground-muted" >
159- Managed by T3 Connect. Tunnel details update automatically.
160- </ Text >
161- ) : (
162- < >
163- < ConnectionFormField
164- label = "Label"
165- autoCapitalize = "words"
166- autoCorrect = { false }
167- placeholder = "My MacBook"
168- value = { label }
169- onChangeText = { setLabel }
170- />
145+ < ConnectionEnvironmentEditor
146+ key = { isHostName ? "host" : "local" }
147+ { ...props }
148+ isHostName = { isHostName }
149+ enabled = { enabled }
150+ serverConfig = { serverConfig }
151+ />
152+ ) : null }
153+ </ Animated . View >
154+ ) ;
155+ }
171156
172- < ConnectionFormField
173- label = "URL"
174- autoCapitalize = "none"
175- autoCorrect = { false }
176- keyboardType = "url"
177- placeholder = "192.168.1.100:8080"
178- value = { url }
179- onChangeText = { setUrl }
180- />
181- </ >
182- ) }
157+ /** Mounted only while expanded, so each opening starts from the latest saved values. */
158+ function ConnectionEnvironmentEditor (
159+ props : ConnectionEnvironmentRowProps & {
160+ readonly isHostName : boolean ;
161+ readonly enabled : boolean ;
162+ readonly serverConfig : ServerConfig | null ;
163+ } ,
164+ ) {
165+ const { isHostName, enabled, serverConfig } = props ;
166+ const [ draft , setDraft ] = useState < { readonly label ?: string ; readonly url ?: string } > ( { } ) ;
167+ const [ saving , setSaving ] = useState ( false ) ;
168+ const label =
169+ draft . label ??
170+ ( isHostName
171+ ? ( serverConfig ?. settings . environmentLabel ?? "" )
172+ : props . environment . environmentLabel ) ;
173+ const url = draft . url ?? props . environment . displayUrl ;
174+ const canUpdateSettings = useAtomValue (
175+ serverEnvironment . updateSettings . permissionAtom ( props . environment . environmentId , {
176+ patch : { environmentLabel : label . trim ( ) } ,
177+ } ) ,
178+ ) ;
179+ const canEditHost =
180+ props . environment . connectionState === "connected" &&
181+ serverConfig ?. environment . capabilities . environmentLabel === true &&
182+ canUpdateSettings ;
183+ const canSave = ! saving && ( ! isHostName || canEditHost ) ;
184+ async function handleSave ( ) {
185+ if ( ! canSave ) return ;
186+ setSaving ( true ) ;
187+ try {
188+ const result = await props . onUpdate ( props . environment . environmentId , {
189+ label : label . trim ( ) ,
190+ displayUrl : url . trim ( ) ,
191+ } ) ;
192+ if ( AsyncResult . isSuccess ( result ) ) {
193+ props . onToggle ( ) ;
194+ return ;
195+ }
196+ const error = Cause . squash ( result . cause ) ;
197+ Alert . alert (
198+ "Could not update environment" ,
199+ error instanceof Error ? error . message : "The environment could not be updated." ,
200+ ) ;
201+ } finally {
202+ setSaving ( false ) ;
203+ }
204+ }
183205
184- { Platform . OS === "android" ? (
185- < View className = "flex-row items-center justify-end gap-2" >
186- { props . environment . isRelayManaged ? null : (
187- < View className = "flex-1" >
188- < MaterialButton
189- label = "Save"
190- tone = "primary"
191- fullWidth
192- onPress = { ( ) => {
193- void handleSave ( ) ;
194- } }
195- />
196- </ View >
197- ) }
198- < MaterialIconButton
199- accessibilityLabel = "Reconnect environment"
200- icon = "arrow.clockwise"
201- variant = "tonal"
202- disabled = { ! enabled }
203- onPress = { ( ) => props . onReconnect ( props . environment . environmentId ) }
204- />
205- < MaterialIconButton
206- accessibilityLabel = "Remove environment"
207- icon = "trash"
208- variant = "danger"
209- onPress = { ( ) => props . onRemove ( props . environment . environmentId ) }
210- />
211- </ View >
212- ) : (
213- < View className = "flex-row justify-end gap-2" >
214- { props . environment . isRelayManaged ? null : (
215- < Pressable
216- 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"
217- onPress = { handleSave }
218- >
219- < SymbolView
220- name = "checkmark"
221- size = { 13 }
222- tintColorClassName = "accent-primary-foreground"
223- type = "monochrome"
224- />
225- < Text className = "text-xs font-t3-bold tracking-[0.8px] uppercase text-primary-foreground" >
226- Save
227- </ Text >
228- </ Pressable >
229- ) }
206+ return (
207+ < Animated . View
208+ entering = { FadeIn . duration ( 200 ) }
209+ exiting = { FadeOut . duration ( 150 ) }
210+ className = "gap-3 px-4 pb-4"
211+ >
212+ < ConnectionFormField
213+ label = { isHostName ? "Host name" : "Label" }
214+ autoCapitalize = "words"
215+ autoCorrect = { false }
216+ placeholder = "My MacBook"
217+ value = { label }
218+ editable = { ! saving && ( ! isHostName || canEditHost ) }
219+ onChangeText = { ( value ) => {
220+ setDraft ( ( previous ) => ( { ...previous , label : value } ) ) ;
221+ } }
222+ />
230223
231- < Pressable
232- className = "h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-input-border bg-input active:opacity-70 disabled:opacity-40"
233- disabled = { ! enabled }
234- onPress = { ( ) => props . onReconnect ( props . environment . environmentId ) }
235- >
236- < SymbolView
237- name = "arrow.clockwise"
238- size = { 14 }
239- tintColorClassName = "accent-icon-subtle"
240- type = "monochrome"
241- />
242- </ Pressable >
224+ { isHostName ? (
225+ < Text className = "text-sm text-foreground-muted" >
226+ { props . environment . connectionState !== "connected"
227+ ? "Connect to this environment to change its host name."
228+ : serverConfig ?. environment . capabilities . environmentLabel !== true
229+ ? "Update the host server to change its name."
230+ : ! canUpdateSettings
231+ ? "You need permission to update this environment’s settings."
232+ : "Shared with all connected clients. Leave empty to use the machine name." }
233+ </ Text >
234+ ) : (
235+ < ConnectionFormField
236+ label = "URL"
237+ autoCapitalize = "none"
238+ autoCorrect = { false }
239+ keyboardType = "url"
240+ placeholder = "192.168.1.100:8080"
241+ value = { url }
242+ editable = { ! saving }
243+ onChangeText = { ( value ) => {
244+ setDraft ( ( previous ) => ( { ...previous , url : value } ) ) ;
245+ } }
246+ />
247+ ) }
243248
244- < Pressable
245- className = "h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-danger-border bg-danger active:opacity-70"
246- onPress = { ( ) => props . onRemove ( props . environment . environmentId ) }
247- >
248- < SymbolView
249- name = "trash"
250- size = { 14 }
251- tintColorClassName = "accent-danger-foreground"
252- type = "monochrome"
253- />
254- </ Pressable >
255- </ View >
256- ) }
257- </ Animated . View >
258- ) : null }
249+ { Platform . OS === "android" ? (
250+ < View className = "flex-row items-center justify-end gap-2" >
251+ < View className = "flex-1" >
252+ < MaterialButton
253+ label = "Save"
254+ tone = "primary"
255+ fullWidth
256+ disabled = { ! canSave }
257+ onPress = { ( ) => {
258+ void handleSave ( ) ;
259+ } }
260+ />
261+ </ View >
262+ < MaterialIconButton
263+ accessibilityLabel = "Reconnect environment"
264+ icon = "arrow.clockwise"
265+ variant = "tonal"
266+ disabled = { ! enabled }
267+ onPress = { ( ) => props . onReconnect ( props . environment . environmentId ) }
268+ />
269+ < MaterialIconButton
270+ accessibilityLabel = "Remove environment"
271+ icon = "trash"
272+ variant = "danger"
273+ onPress = { ( ) => props . onRemove ( props . environment . environmentId ) }
274+ />
275+ </ View >
276+ ) : (
277+ < View className = "flex-row justify-end gap-2" >
278+ < Pressable
279+ 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 disabled:opacity-40"
280+ disabled = { ! canSave }
281+ onPress = { handleSave }
282+ >
283+ < SymbolView
284+ name = "checkmark"
285+ size = { 13 }
286+ tintColorClassName = "accent-primary-foreground"
287+ type = "monochrome"
288+ />
289+ < Text className = "text-xs font-t3-bold tracking-[0.8px] uppercase text-primary-foreground" >
290+ Save
291+ </ Text >
292+ </ Pressable >
293+
294+ < Pressable
295+ className = "h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-input-border bg-input active:opacity-70 disabled:opacity-40"
296+ disabled = { ! enabled }
297+ onPress = { ( ) => props . onReconnect ( props . environment . environmentId ) }
298+ >
299+ < SymbolView
300+ name = "arrow.clockwise"
301+ size = { 14 }
302+ tintColorClassName = "accent-icon-subtle"
303+ type = "monochrome"
304+ />
305+ </ Pressable >
306+
307+ < Pressable
308+ className = "h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-danger-border bg-danger active:opacity-70"
309+ onPress = { ( ) => props . onRemove ( props . environment . environmentId ) }
310+ >
311+ < SymbolView
312+ name = "trash"
313+ size = { 14 }
314+ tintColorClassName = "accent-danger-foreground"
315+ type = "monochrome"
316+ />
317+ </ Pressable >
318+ </ View >
319+ ) }
259320 </ Animated . View >
260321 ) ;
261322}
0 commit comments