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 { RefreshControl } from "react-native";
8import { useSafeAreaInsets } from "react-native-safe-area-context";
9
10import { SettingsScreen } from "./components/SettingsScreen";
11import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
12import { LocalEnvironmentList } from "../connection/LocalEnvironmentList";
13import { PullRequestRoutingSettings } from "../connection/PullRequestRoutingSettings";
14import { splitEnvironmentSections } from "../connection/environmentSections";
15import { useUniwindTheme } from "../../lib/useUniwindTheme";
16import { useRemoteConnections } from "../../state/use-remote-environment-registry";
17import { relayEnvironmentDiscovery } from "../../state/relay";
18import { useAtomCommand } from "../../state/use-atom-command";
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 headerIconColor = useUniwindTheme()["--color-icon"];
49 const relaySession = useAtomValue(managedRelaySessionAtom);
50 const refreshRelayEnvironments = useAtomCommand(
51 relayEnvironmentDiscovery.refresh,
52 "relay environment refresh",
53 );
54 const [isRefreshingCloud, setIsRefreshingCloud] = useState(false);
55 const cloudRefreshPendingRef = useRef(false);
56 async function refreshCloudEnvironments() {
57 if (!relaySession || cloudRefreshPendingRef.current) return;
58 cloudRefreshPendingRef.current = true;
59 setIsRefreshingCloud(true);
60 await refreshRelayEnvironments();
61 cloudRefreshPendingRef.current = false;
62 setIsRefreshingCloud(false);
63 }
64
65 const openEnvironment = (environmentId: EnvironmentId) => {
66 navigation.navigate("SettingsEnvironmentDetail", { environmentId });
67 };
68 const handleUpdateEnvironment = (
69 environmentId: EnvironmentId,
70 updates: { readonly label: string; readonly displayUrl: string },
71 ) => {
72 if (!SHOWCASE_ENABLED) return onUpdateEnvironment(environmentId, updates);
73 const actualEnvironment = environmentSections.localEnvironments.find(
74 (environment) => environment.environmentId === environmentId,
75 );
76 const presentedEnvironment = localEnvironments.find(
77 (environment) => environment.environmentId === environmentId,
78 );
79 return onUpdateEnvironment(environmentId, {
80 ...updates,
81 displayUrl:
82 actualEnvironment && presentedEnvironment
83 ? resolveShowcaseEnvironmentUpdateDisplayUrl({
84 actualDisplayUrl: actualEnvironment.displayUrl,
85 presentedDisplayUrl: presentedEnvironment.displayUrl,
86 submittedDisplayUrl: updates.displayUrl,
87 })
88 : updates.displayUrl,
89 });
90 };
91
92 return (
93 <SettingsScreen
94 title="Environments"
95 refresh={
96 relaySession
97 ? {
98 accessibilityLabel: "Refresh cloud environments",
99 disabled: isRefreshingCloud,
100 onPress: () => void refreshCloudEnvironments(),
101 }
102 : undefined
103 }
104 actions={[
105 {
106 accessibilityLabel: "Add environment",
107 icon: "plus",
108 tintColor: headerIconColor,
109 onPress: () => navigation.navigate("SettingsEnvironmentNew"),
110 },
111 ]}
112 >
113 <ScrollView
114 alwaysBounceVertical
115 contentInsetAdjustmentBehavior="automatic"
116 showsVerticalScrollIndicator={false}
117 className="flex-1"
118 contentContainerClassName="px-5 pt-4"
119 contentContainerStyle={{
120 paddingBottom: Math.max(insets.bottom, 18) + 18,
121 }}
122 refreshControl={
123 relaySession ? (
124 <RefreshControl
125 refreshing={isRefreshingCloud}
126 onRefresh={() => void refreshCloudEnvironments()}
127 />
128 ) : undefined
129 }
130 >
131 <LocalEnvironmentList
132 environments={localEnvironments}
133 expandedId={null}
134 onToggle={openEnvironment}
135 opensDetails
136 onReconnect={onReconnectEnvironment}
137 onRemove={onRemoveEnvironmentPress}
138 onSetEnabled={onSetEnvironmentEnabled}
139 onUpdate={handleUpdateEnvironment}
140 />
141
142 {/* Keep connected relays visible and removable without cloud config or sign-in; this component gates discovery. */}
143 <CloudEnvironmentRows
144 connectedCloudEnvironments={connectedCloudEnvironments}
145 onOpenEnvironment={openEnvironment}
146 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
147 onRemoveEnvironment={onRemoveEnvironmentPress}
148 {...(SHOWCASE_ENABLED
149 ? {
150 showcaseAvailableEnvironments: SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
151 showcaseSignedIn: true,
152 }
153 : {})}
154 />
155 <PullRequestRoutingSettings />
156 </ScrollView>
157 </SettingsScreen>
158 );
159}