Open History to see the last commit
1import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
2import { useNavigation } from "@react-navigation/native";
3import { SymbolView } from "../../components/AppSymbol";
4import type { EnvironmentId } from "@t3tools/contracts";
5import { useCallback, useEffect, useState } from "react";
6import { Platform, ScrollView, View } from "react-native";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8
9import { AppText as Text } from "../../components/AppText";
10import { AndroidScreenHeader } from "../../components/AndroidScreenHeader";
11import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
12import { ConnectionEnvironmentRow } from "../connection/ConnectionEnvironmentRow";
13import { splitEnvironmentSections } from "../connection/environmentSections";
14import { cn } from "../../lib/cn";
15import { useThemeColor } from "../../lib/useThemeColor";
16import { useRemoteConnections } from "../../state/use-remote-environment-registry";
17import {
18 applyShowcaseLocalEnvironmentDisplayUrls,
19 resolveShowcaseEnvironmentUpdateDisplayUrl,
20 SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
21 SHOWCASE_CONNECTED_CLOUD_ENVIRONMENTS,
22} from "../showcase/showcaseEnvironmentRows";
23import { markNativeShowcaseReady } from "../showcase/nativeShowcaseScene";
24
25const SHOWCASE_ENABLED = process.env.EXPO_PUBLIC_SHOWCASE === "1";
26
27export function SettingsEnvironmentsRouteScreen() {
28 const {
29 connectedEnvironments,
30 onReconnectEnvironment,
31 onRemoveEnvironmentPress,
32 onUpdateEnvironment,
33 } = useRemoteConnections();
34 const navigation = useNavigation();
35 const insets = useSafeAreaInsets();
36 const environmentSections = splitEnvironmentSections({
37 connectedEnvironments,
38 cloudEnvironments: null,
39 });
40 const localEnvironments = SHOWCASE_ENABLED
41 ? applyShowcaseLocalEnvironmentDisplayUrls(environmentSections.localEnvironments)
42 : environmentSections.localEnvironments;
43 const connectedCloudEnvironments = SHOWCASE_ENABLED
44 ? SHOWCASE_CONNECTED_CLOUD_ENVIRONMENTS
45 : environmentSections.connectedCloudEnvironments;
46 const hasLocalEnvironments = localEnvironments.length > 0;
47 const [expandedId, setExpandedId] = useState<EnvironmentId | null>(null);
48 const accentColor = useThemeColor("--color-icon-muted");
49 const headerIconColor = useThemeColor("--color-icon");
50
51 useEffect(() => {
52 if (!SHOWCASE_ENABLED) return;
53 const timer = setTimeout(() => markNativeShowcaseReady("environments"), 500);
54 return () => clearTimeout(timer);
55 }, []);
56
57 const handleToggle = useCallback((environmentId: EnvironmentId) => {
58 setExpandedId((prev) => (prev === environmentId ? null : environmentId));
59 }, []);
60 const handleUpdateEnvironment = useCallback(
61 (
62 environmentId: EnvironmentId,
63 updates: { readonly label: string; readonly displayUrl: string },
64 ) => {
65 if (!SHOWCASE_ENABLED) return onUpdateEnvironment(environmentId, updates);
66 const actualEnvironment = environmentSections.localEnvironments.find(
67 (environment) => environment.environmentId === environmentId,
68 );
69 const presentedEnvironment = localEnvironments.find(
70 (environment) => environment.environmentId === environmentId,
71 );
72 return onUpdateEnvironment(environmentId, {
73 ...updates,
74 displayUrl:
75 actualEnvironment && presentedEnvironment
76 ? resolveShowcaseEnvironmentUpdateDisplayUrl({
77 actualDisplayUrl: actualEnvironment.displayUrl,
78 presentedDisplayUrl: presentedEnvironment.displayUrl,
79 submittedDisplayUrl: updates.displayUrl,
80 })
81 : updates.displayUrl,
82 });
83 },
84 [environmentSections.localEnvironments, localEnvironments, onUpdateEnvironment],
85 );
86
87 return (
88 <View collapsable={false} className="flex-1 bg-sheet">
89 {Platform.OS === "android" ? (
90 <>
91 {/* Android renders its own in-screen header instead of the native bar. */}
92 <NativeStackScreenOptions options={{ headerShown: false }} />
93 <AndroidScreenHeader
94 title="Environments"
95 onBack={() => navigation.goBack()}
96 actions={[
97 {
98 accessibilityLabel: "Add environment",
99 icon: "plus",
100 onPress: () =>
101 navigation.navigate("SettingsSheet", { screen: "SettingsEnvironmentNew" }),
102 },
103 ]}
104 />
105 </>
106 ) : (
107 <NativeHeaderToolbar placement="right">
108 <NativeHeaderToolbar.Button
109 icon="plus"
110 onPress={() =>
111 navigation.navigate("SettingsSheet", { screen: "SettingsEnvironmentNew" })
112 }
113 separateBackground
114 tintColor={headerIconColor}
115 />
116 </NativeHeaderToolbar>
117 )}
118 <ScrollView
119 contentInsetAdjustmentBehavior="automatic"
120 showsVerticalScrollIndicator={false}
121 className="flex-1"
122 contentContainerClassName="px-5 pt-4"
123 contentContainerStyle={{
124 paddingBottom: Math.max(insets.bottom, 18) + 18,
125 }}
126 >
127 {hasLocalEnvironments ? (
128 <View collapsable={false} className="overflow-hidden rounded-[24px] bg-card">
129 {localEnvironments.map((environment, index) => (
130 <View
131 key={environment.environmentId}
132 collapsable={false}
133 className={cn(index !== 0 && "border-t border-border")}
134 >
135 <ConnectionEnvironmentRow
136 environment={environment}
137 expanded={expandedId === environment.environmentId}
138 onToggle={() => handleToggle(environment.environmentId)}
139 onReconnect={onReconnectEnvironment}
140 onRemove={onRemoveEnvironmentPress}
141 onUpdate={handleUpdateEnvironment}
142 />
143 </View>
144 ))}
145 </View>
146 ) : (
147 <View collapsable={false} className="items-center gap-3 rounded-[24px] bg-card px-6 py-8">
148 <View className="h-12 w-12 items-center justify-center rounded-[16px] bg-subtle">
149 <SymbolView
150 name="point.3.connected.trianglepath.dotted"
151 size={20}
152 tintColor={accentColor}
153 type="monochrome"
154 />
155 </View>
156 <Text className="text-center text-sm leading-normal text-foreground-muted">
157 No environments connected yet.{"\n"}Tap{" "}
158 <Text className="font-t3-bold text-foreground">+</Text> to add one.
159 </Text>
160 </View>
161 )}
162
163 {/* Always mounted: already-connected relay environments must stay
164 visible (and removable) even when cloud config is missing or the
165 user is signed out — the component gates discovery itself. */}
166 <CloudEnvironmentRows
167 connectedCloudEnvironments={connectedCloudEnvironments}
168 onReconnectEnvironment={onReconnectEnvironment}
169 {...(SHOWCASE_ENABLED
170 ? {
171 showcaseAvailableEnvironments: SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
172 showcaseSignedIn: true,
173 }
174 : {})}
175 />
176 </ScrollView>
177 </View>
178 );
179}