Open History to see the last commit
1import { useGT } from "gt-react-native";
2import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
3import { useNavigation } from "@react-navigation/native";
4import { useAtomValue } from "@effect/atom-react";
5import { managedRelaySessionAtom } from "@t3tools/client-runtime/relay";
6import type { EnvironmentId } from "@t3tools/contracts";
7import { useRef, useState } from "react";
8import { RefreshControl } from "../../components/ui/RefreshControl";
9import { useSafeAreaInsets } from "react-native-safe-area-context";
10
11import { SettingsScreen } from "./components/SettingsScreen";
12import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
13import { LocalEnvironmentList } from "../connection/LocalEnvironmentList";
14import { PullRequestRoutingSettings } from "../connection/PullRequestRoutingSettings";
15import { splitEnvironmentSections } from "../connection/environmentSections";
16import { useUniwindTheme } from "../../lib/useUniwindTheme";
17import { useRemoteConnections } from "../../state/use-remote-environment-registry";
18import { relayEnvironmentDiscovery } from "../../state/relay";
19import { useAtomCommand } from "../../state/use-atom-command";
20import {
21 applyShowcaseLocalEnvironmentDisplayUrls,
22 resolveShowcaseEnvironmentUpdateDisplayUrl,
23 SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
24 SHOWCASE_CONNECTED_CLOUD_ENVIRONMENTS,
25} from "../showcase/showcaseEnvironmentRows";
26
27const SHOWCASE_ENABLED = process.env.EXPO_PUBLIC_SHOWCASE === "1";
28
29export function SettingsEnvironmentsRouteScreen() {
30 const gt = useGT();
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("SettingsEnvironmentDetail", { environmentId });
69 };
70 const handleUpdateEnvironment = (
71 environmentId: EnvironmentId,
72 updates: { readonly label: string; readonly displayUrl: string },
73 ) => {
74 if (!SHOWCASE_ENABLED) return onUpdateEnvironment(environmentId, updates);
75 const actualEnvironment = environmentSections.localEnvironments.find(
76 (environment) => environment.environmentId === environmentId,
77 );
78 const presentedEnvironment = localEnvironments.find(
79 (environment) => environment.environmentId === environmentId,
80 );
81 return onUpdateEnvironment(environmentId, {
82 ...updates,
83 displayUrl:
84 actualEnvironment && presentedEnvironment
85 ? resolveShowcaseEnvironmentUpdateDisplayUrl({
86 actualDisplayUrl: actualEnvironment.displayUrl,
87 presentedDisplayUrl: presentedEnvironment.displayUrl,
88 submittedDisplayUrl: updates.displayUrl,
89 })
90 : updates.displayUrl,
91 });
92 };
93
94 return (
95 <SettingsScreen
96 title={gt`Environments`}
97 refresh={
98 relaySession
99 ? {
100 accessibilityLabel: gt`Refresh cloud environments`,
101 disabled: isRefreshingCloud,
102 onPress: () => void refreshCloudEnvironments(),
103 }
104 : undefined
105 }
106 actions={[
107 {
108 accessibilityLabel: gt`Add environment`,
109 icon: "plus",
110 tintColor: headerIconColor,
111 onPress: () => navigation.navigate("SettingsEnvironmentNew"),
112 },
113 ]}
114 >
115 <ScrollView
116 alwaysBounceVertical
117 contentInsetAdjustmentBehavior="automatic"
118 showsVerticalScrollIndicator={false}
119 className="flex-1"
120 contentContainerClassName="px-5 pt-4"
121 contentContainerStyle={{
122 paddingBottom: Math.max(insets.bottom, 18) + 18,
123 }}
124 refreshControl={
125 relaySession ? (
126 <RefreshControl
127 refreshing={isRefreshingCloud}
128 onRefresh={() => void refreshCloudEnvironments()}
129 />
130 ) : undefined
131 }
132 >
133 {/* Its empty state would claim nothing is connected while saved T3 Connect rows are listed below. */}
134 {localEnvironments.length > 0 || connectedCloudEnvironments.length === 0 ? (
135 <LocalEnvironmentList
136 environments={localEnvironments}
137 expandedId={null}
138 onToggle={openEnvironment}
139 opensDetails
140 onReconnect={onReconnectEnvironment}
141 onRemove={onRemoveEnvironmentPress}
142 onSetEnabled={onSetEnvironmentEnabled}
143 onUpdate={handleUpdateEnvironment}
144 />
145 ) : null}
146
147 {/* Keep connected relays visible and removable without cloud config or sign-in; this component gates discovery. */}
148 <CloudEnvironmentRows
149 connectedCloudEnvironments={connectedCloudEnvironments}
150 onOpenEnvironment={openEnvironment}
151 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
152 onRemoveEnvironment={onRemoveEnvironmentPress}
153 {...(SHOWCASE_ENABLED
154 ? {
155 showcaseAvailableEnvironments: SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
156 showcaseSignedIn: true,
157 }
158 : {})}
159 />
160 <PullRequestRoutingSettings />
161 </ScrollView>
162 </SettingsScreen>
163 );
164}