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