Open History to see the last commit
1import { ScreenScrollView as ScrollView } from "../../components/ui/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 { 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/ui/AndroidAnchoredMenu";
12import { AndroidHeaderIconButton } from "../../components/ui/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 await refreshRelayEnvironments();
63 cloudRefreshPendingRef.current = false;
64 setIsRefreshingCloud(false);
65 }
66
67 const openEnvironment = (environmentId: EnvironmentId) => {
68 navigation.navigate("SettingsSheet", {
69 screen: "SettingsContent",
70 params: { screen: "SettingsEnvironmentDetail", params: { environmentId } },
71 });
72 };
73 const handleUpdateEnvironment = (
74 environmentId: EnvironmentId,
75 updates: { readonly label: string; readonly displayUrl: string },
76 ) => {
77 if (!SHOWCASE_ENABLED) return onUpdateEnvironment(environmentId, updates);
78 const actualEnvironment = environmentSections.localEnvironments.find(
79 (environment) => environment.environmentId === environmentId,
80 );
81 const presentedEnvironment = localEnvironments.find(
82 (environment) => environment.environmentId === environmentId,
83 );
84 return onUpdateEnvironment(environmentId, {
85 ...updates,
86 displayUrl:
87 actualEnvironment && presentedEnvironment
88 ? resolveShowcaseEnvironmentUpdateDisplayUrl({
89 actualDisplayUrl: actualEnvironment.displayUrl,
90 presentedDisplayUrl: presentedEnvironment.displayUrl,
91 submittedDisplayUrl: updates.displayUrl,
92 })
93 : updates.displayUrl,
94 });
95 };
96
97 return (
98 <SettingsScreen
99 title="Environments"
100 trailing={
101 Platform.OS === "android" && relaySession ? (
102 <AndroidAnchoredMenu
103 title="Environment options"
104 actions={[
105 {
106 id: "refresh",
107 title: "Refresh cloud environments",
108 attributes: { disabled: isRefreshingCloud },
109 },
110 ]}
111 onPressAction={({ nativeEvent }) => {
112 if (nativeEvent.event === "refresh") void refreshCloudEnvironments();
113 }}
114 >
115 {(open) => (
116 <AndroidHeaderIconButton
117 accessibilityLabel="Environment options"
118 icon="ellipsis"
119 onPress={open}
120 />
121 )}
122 </AndroidAnchoredMenu>
123 ) : undefined
124 }
125 actions={[
126 {
127 accessibilityLabel: "Add environment",
128 icon: "plus",
129 tintColor: headerIconColor,
130 onPress: () =>
131 navigation.navigate("SettingsSheet", {
132 screen: "SettingsContent",
133 params: { screen: "SettingsEnvironmentNew" },
134 }),
135 },
136 ]}
137 >
138 <ScrollView
139 alwaysBounceVertical
140 contentInsetAdjustmentBehavior="automatic"
141 showsVerticalScrollIndicator={false}
142 className="flex-1"
143 contentContainerClassName="px-5 pt-4"
144 contentContainerStyle={{
145 paddingBottom: Math.max(insets.bottom, 18) + 18,
146 }}
147 refreshControl={
148 relaySession ? (
149 <RefreshControl
150 refreshing={isRefreshingCloud}
151 onRefresh={() => void refreshCloudEnvironments()}
152 />
153 ) : undefined
154 }
155 >
156 <LocalEnvironmentList
157 environments={localEnvironments}
158 expandedId={null}
159 onToggle={openEnvironment}
160 opensDetails
161 onReconnect={onReconnectEnvironment}
162 onRemove={onRemoveEnvironmentPress}
163 onSetEnabled={onSetEnvironmentEnabled}
164 onUpdate={handleUpdateEnvironment}
165 />
166
167 {/* Always mounted: already-connected relay environments must stay
168 visible (and removable) even when cloud config is missing or the
169 user is signed out — the component gates discovery itself. */}
170 <CloudEnvironmentRows
171 connectedCloudEnvironments={connectedCloudEnvironments}
172 onOpenEnvironment={openEnvironment}
173 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
174 onRemoveEnvironment={onRemoveEnvironmentPress}
175 {...(SHOWCASE_ENABLED
176 ? {
177 showcaseAvailableEnvironments: SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
178 showcaseSignedIn: true,
179 }
180 : {})}
181 />
182 <GitHubRoutingSettings />
183 </ScrollView>
184 </SettingsScreen>
185 );
186}