Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SettingsEnvironmentsRouteScreen.tsx

177 lines6.7 KB

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 { PullRequestRoutingSettings } from "../connection/PullRequestRoutingSettings";
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("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="Environments"
97 trailing={
98 Platform.OS === "android" && relaySession ? (
99 <AndroidAnchoredMenu
100 title="Environment options"
101 actions={[
102 {
103 id: "refresh",
104 title: "Refresh cloud environments",
105 attributes: { disabled: isRefreshingCloud },
106 },
107 ]}
108 onPressAction={({ nativeEvent }) => {
109 if (nativeEvent.event === "refresh") void refreshCloudEnvironments();
110 }}
111 >
112 {(open) => (
113 <AndroidHeaderIconButton
114 accessibilityLabel="Environment options"
115 icon="ellipsis"
116 onPress={open}
117 />
118 )}
119 </AndroidAnchoredMenu>
120 ) : undefined
121 }
122 actions={[
123 {
124 accessibilityLabel: "Add environment",
125 icon: "plus",
126 tintColor: headerIconColor,
127 onPress: () => navigation.navigate("SettingsEnvironmentNew"),
128 },
129 ]}
130 >
131 <ScrollView
132 alwaysBounceVertical
133 contentInsetAdjustmentBehavior="automatic"
134 showsVerticalScrollIndicator={false}
135 className="flex-1"
136 contentContainerClassName="px-5 pt-4"
137 contentContainerStyle={{
138 paddingBottom: Math.max(insets.bottom, 18) + 18,
139 }}
140 refreshControl={
141 relaySession ? (
142 <RefreshControl
143 refreshing={isRefreshingCloud}
144 onRefresh={() => void refreshCloudEnvironments()}
145 />
146 ) : undefined
147 }
148 >
149 <LocalEnvironmentList
150 environments={localEnvironments}
151 expandedId={null}
152 onToggle={openEnvironment}
153 opensDetails
154 onReconnect={onReconnectEnvironment}
155 onRemove={onRemoveEnvironmentPress}
156 onSetEnabled={onSetEnvironmentEnabled}
157 onUpdate={handleUpdateEnvironment}
158 />
159​
160 {/* Keep connected relays visible and removable without cloud config or sign-in; this component gates discovery. */}
161 <CloudEnvironmentRows
162 connectedCloudEnvironments={connectedCloudEnvironments}
163 onOpenEnvironment={openEnvironment}
164 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
165 onRemoveEnvironment={onRemoveEnvironmentPress}
166 {...(SHOWCASE_ENABLED
167 ? {
168 showcaseAvailableEnvironments: SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
169 showcaseSignedIn: true,
170 }
171 : {})}
172 />
173 <PullRequestRoutingSettings />
174 </ScrollView>
175 </SettingsScreen>
176 );