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