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