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", {
102 screen: "SettingsContent",
103 params: { screen: "SettingsEnvironmentNew" },
104 }),
105 },
106 ]}
107 />
108 </>
109 ) : (
110 <NativeHeaderToolbar placement="right">
111 <NativeHeaderToolbar.Button
112 icon="plus"
113 onPress={() =>
114 navigation.navigate("SettingsSheet", {
115 screen: "SettingsContent",
116 params: { screen: "SettingsEnvironmentNew" },
117 })
118 }
119 separateBackground
120 tintColor={headerIconColor}
121 />
122 </NativeHeaderToolbar>
123 )}
124 <ScrollView
125 contentInsetAdjustmentBehavior="automatic"
126 showsVerticalScrollIndicator={false}
127 className="flex-1"
128 contentContainerClassName="px-5 pt-4"
129 contentContainerStyle={{
130 paddingBottom: Math.max(insets.bottom, 18) + 18,
131 }}
132 >
133 {hasLocalEnvironments ? (
134 <View collapsable={false} className="overflow-hidden rounded-[24px] bg-card">
135 {localEnvironments.map((environment, index) => (
136 <View
137 key={environment.environmentId}
138 collapsable={false}
139 className={cn(index !== 0 && "border-t border-border")}
140 >
141 <ConnectionEnvironmentRow
142 environment={environment}
143 expanded={expandedId === environment.environmentId}
144 onToggle={() => handleToggle(environment.environmentId)}
145 onReconnect={onReconnectEnvironment}
146 onRemove={onRemoveEnvironmentPress}
147 onUpdate={handleUpdateEnvironment}
148 />
149 </View>
150 ))}
151 </View>
152 ) : (
153 <View collapsable={false} className="items-center gap-3 rounded-[24px] bg-card px-6 py-8">
154 <View className="h-12 w-12 items-center justify-center rounded-[16px] bg-subtle">
155 <SymbolView
156 name="point.3.connected.trianglepath.dotted"
157 size={20}
158 tintColor={accentColor}
159 type="monochrome"
160 />
161 </View>
162 <Text className="text-center text-sm leading-normal text-foreground-muted">
163 No environments connected yet.{"\n"}Tap{" "}
164 <Text className="font-t3-bold text-foreground">+</Text> to add one.
165 </Text>
166 </View>
167 )}
168
169 {/* Always mounted: already-connected relay environments must stay
170 visible (and removable) even when cloud config is missing or the
171 user is signed out — the component gates discovery itself. */}
172 <CloudEnvironmentRows
173 connectedCloudEnvironments={connectedCloudEnvironments}
174 onReconnectEnvironment={onReconnectEnvironment}
175 {...(SHOWCASE_ENABLED
176 ? {
177 showcaseAvailableEnvironments: SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
178 showcaseSignedIn: true,
179 }
180 : {})}
181 />
182 </ScrollView>
183 </View>
184 );
185}