Open History to see the last commit
1import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView";
2import { useNavigation } from "@react-navigation/native";
3import { useAtomValue } from "@effect/atom-react";
4import { managedRelaySessionAtom } from "@t3tools/client-runtime/relay";
5import type { EnvironmentId } from "@t3tools/contracts";
6import { useCallback, useRef, useState } from "react";
7import { Platform, RefreshControl } from "react-native";
8import { useSafeAreaInsets } from "react-native-safe-area-context";
9
10import { SettingsScreen } from "./components/SettingsScreen";
11import { AndroidAnchoredMenu } from "../../components/AndroidAnchoredMenu";
12import { AndroidHeaderIconButton } from "../../components/AndroidScreenHeader";
13import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
14import { LocalEnvironmentList } from "../connection/LocalEnvironmentList";
15import { GitHubRoutingSettings } from "../connection/GitHubRoutingSettings";
16import { splitEnvironmentSections } from "../connection/environmentSections";
17import { useUniwindTheme } from "../../lib/useUniwindTheme";
18import { useRemoteConnections } from "../../state/use-remote-environment-registry";
19import { relayEnvironmentDiscovery } from "../../state/relay";
20import { useAtomCommand } from "../../state/use-atom-command";
21import {
22 applyShowcaseLocalEnvironmentDisplayUrls,
23 resolveShowcaseEnvironmentUpdateDisplayUrl,
24 SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
25 SHOWCASE_CONNECTED_CLOUD_ENVIRONMENTS,
26} from "../showcase/showcaseEnvironmentRows";
27
28const SHOWCASE_ENABLED = process.env.EXPO_PUBLIC_SHOWCASE === "1";
29
30export function SettingsEnvironmentsRouteScreen() {
31 const {
32 connectedEnvironments,
33 onReconnectEnvironment,
34 onRemoveEnvironmentPress,
35 onSetEnvironmentEnabled,
36 onUpdateEnvironment,
37 } = useRemoteConnections();
38 const navigation = useNavigation();
39 const insets = useSafeAreaInsets();
40 const environmentSections = splitEnvironmentSections({
41 connectedEnvironments,
42 cloudEnvironments: null,
43 });
44 const localEnvironments = SHOWCASE_ENABLED
45 ? applyShowcaseLocalEnvironmentDisplayUrls(environmentSections.localEnvironments)
46 : environmentSections.localEnvironments;
47 const connectedCloudEnvironments = SHOWCASE_ENABLED
48 ? SHOWCASE_CONNECTED_CLOUD_ENVIRONMENTS
49 : environmentSections.connectedCloudEnvironments;
50 const headerIconColor = useUniwindTheme()["--color-icon"];
51 const relaySession = useAtomValue(managedRelaySessionAtom);
52 const refreshRelayEnvironments = useAtomCommand(
53 relayEnvironmentDiscovery.refresh,
54 "relay environment refresh",
55 );
56 const [isRefreshingCloud, setIsRefreshingCloud] = useState(false);
57 const cloudRefreshPendingRef = useRef(false);
58 async function refreshCloudEnvironments() {
59 if (!relaySession || cloudRefreshPendingRef.current) return;
60 cloudRefreshPendingRef.current = true;
61 setIsRefreshingCloud(true);
62 try {
63 await refreshRelayEnvironments();
64 } finally {
65 cloudRefreshPendingRef.current = false;
66 setIsRefreshingCloud(false);
67 }
68 }
69
70 const openEnvironment = useCallback(
71 (environmentId: EnvironmentId) => {
72 navigation.navigate("SettingsSheet", {
73 screen: "SettingsContent",
74 params: { screen: "SettingsEnvironmentDetail", params: { environmentId } },
75 });
76 },
77 [navigation],
78 );
79 const handleUpdateEnvironment = useCallback(
80 (
81 environmentId: EnvironmentId,
82 updates: { readonly label: string; readonly displayUrl: string },
83 ) => {
84 if (!SHOWCASE_ENABLED) return onUpdateEnvironment(environmentId, updates);
85 const actualEnvironment = environmentSections.localEnvironments.find(
86 (environment) => environment.environmentId === environmentId,
87 );
88 const presentedEnvironment = localEnvironments.find(
89 (environment) => environment.environmentId === environmentId,
90 );
91 return onUpdateEnvironment(environmentId, {
92 ...updates,
93 displayUrl:
94 actualEnvironment && presentedEnvironment
95 ? resolveShowcaseEnvironmentUpdateDisplayUrl({
96 actualDisplayUrl: actualEnvironment.displayUrl,
97 presentedDisplayUrl: presentedEnvironment.displayUrl,
98 submittedDisplayUrl: updates.displayUrl,
99 })
100 : updates.displayUrl,
101 });
102 },
103 [environmentSections.localEnvironments, localEnvironments, onUpdateEnvironment],
104 );
105
106 return (
107 <SettingsScreen
108 title="Environments"
109 trailing={
110 Platform.OS === "android" && relaySession ? (
111 <AndroidAnchoredMenu
112 title="Environment options"
113 actions={[
114 {
115 id: "refresh",
116 title: "Refresh cloud environments",
117 attributes: { disabled: isRefreshingCloud },
118 },
119 ]}
120 onPressAction={({ nativeEvent }) => {
121 if (nativeEvent.event === "refresh") void refreshCloudEnvironments();
122 }}
123 >
124 {(open) => (
125 <AndroidHeaderIconButton
126 accessibilityLabel="Environment options"
127 icon="ellipsis"
128 onPress={open}
129 />
130 )}
131 </AndroidAnchoredMenu>
132 ) : undefined
133 }
134 actions={[
135 {
136 accessibilityLabel: "Add environment",
137 icon: "plus",
138 tintColor: headerIconColor,
139 onPress: () =>
140 navigation.navigate("SettingsSheet", {
141 screen: "SettingsContent",
142 params: { screen: "SettingsEnvironmentNew" },
143 }),
144 },
145 ]}
146 >
147 <ScrollView
148 alwaysBounceVertical
149 contentInsetAdjustmentBehavior="automatic"
150 showsVerticalScrollIndicator={false}
151 className="flex-1"
152 contentContainerClassName="px-5 pt-4"
153 contentContainerStyle={{
154 paddingBottom: Math.max(insets.bottom, 18) + 18,
155 }}
156 refreshControl={
157 relaySession ? (
158 <RefreshControl
159 refreshing={isRefreshingCloud}
160 onRefresh={() => void refreshCloudEnvironments()}
161 />
162 ) : undefined
163 }
164 >
165 <LocalEnvironmentList
166 environments={localEnvironments}
167 expandedId={null}
168 onToggle={openEnvironment}
169 opensDetails
170 onReconnect={onReconnectEnvironment}
171 onRemove={onRemoveEnvironmentPress}
172 onSetEnabled={onSetEnvironmentEnabled}
173 onUpdate={handleUpdateEnvironment}
174 />
175
176 {/* Always mounted: already-connected relay environments must stay
177 visible (and removable) even when cloud config is missing or the
178 user is signed out — the component gates discovery itself. */}
179 <CloudEnvironmentRows
180 connectedCloudEnvironments={connectedCloudEnvironments}
181 onOpenEnvironment={openEnvironment}
182 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
183 onRemoveEnvironment={onRemoveEnvironmentPress}
184 {...(SHOWCASE_ENABLED
185 ? {
186 showcaseAvailableEnvironments: SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
187 showcaseSignedIn: true,
188 }
189 : {})}
190 />
191 <GitHubRoutingSettings />
192 </ScrollView>
193 </SettingsScreen>
194 );
195}