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