Open History to see the last commit
1import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView";
2import { NativeHeaderToolbar } from "../../native/StackHeader";
3import { useNavigation } from "@react-navigation/native";
4import type { EnvironmentId } from "@t3tools/contracts";
5import { useCallback, useState } from "react";
6import { Platform } from "react-native";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8
9import { SettingsScreen } from "./components/SettingsScreen";
10import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
11import { LocalEnvironmentList } from "../connection/LocalEnvironmentList";
12import { GitHubRoutingSettings } from "../connection/GitHubRoutingSettings";
13import { splitEnvironmentSections } from "../connection/environmentSections";
14import { useUniwindTheme } from "../../lib/useUniwindTheme";
15import { useRemoteConnections } from "../../state/use-remote-environment-registry";
16import {
17 applyShowcaseLocalEnvironmentDisplayUrls,
18 resolveShowcaseEnvironmentUpdateDisplayUrl,
19 SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
20 SHOWCASE_CONNECTED_CLOUD_ENVIRONMENTS,
21} from "../showcase/showcaseEnvironmentRows";
22
23const SHOWCASE_ENABLED = process.env.EXPO_PUBLIC_SHOWCASE === "1";
24
25export function SettingsEnvironmentsRouteScreen() {
26 const {
27 connectedEnvironments,
28 onReconnectEnvironment,
29 onRemoveEnvironmentPress,
30 onSetEnvironmentEnabled,
31 onUpdateEnvironment,
32 } = useRemoteConnections();
33 const navigation = useNavigation();
34 const insets = useSafeAreaInsets();
35 const environmentSections = splitEnvironmentSections({
36 connectedEnvironments,
37 cloudEnvironments: null,
38 });
39 const localEnvironments = SHOWCASE_ENABLED
40 ? applyShowcaseLocalEnvironmentDisplayUrls(environmentSections.localEnvironments)
41 : environmentSections.localEnvironments;
42 const connectedCloudEnvironments = SHOWCASE_ENABLED
43 ? SHOWCASE_CONNECTED_CLOUD_ENVIRONMENTS
44 : environmentSections.connectedCloudEnvironments;
45 const [expandedId, setExpandedId] = useState<EnvironmentId | null>(null);
46 const headerIconColor = useUniwindTheme()["--color-icon"];
47
48 const handleToggle = useCallback((environmentId: EnvironmentId) => {
49 setExpandedId((prev) => (prev === environmentId ? null : environmentId));
50 }, []);
51 const handleUpdateEnvironment = useCallback(
52 (
53 environmentId: EnvironmentId,
54 updates: { readonly label: string; readonly displayUrl: string },
55 ) => {
56 if (!SHOWCASE_ENABLED) return onUpdateEnvironment(environmentId, updates);
57 const actualEnvironment = environmentSections.localEnvironments.find(
58 (environment) => environment.environmentId === environmentId,
59 );
60 const presentedEnvironment = localEnvironments.find(
61 (environment) => environment.environmentId === environmentId,
62 );
63 return onUpdateEnvironment(environmentId, {
64 ...updates,
65 displayUrl:
66 actualEnvironment && presentedEnvironment
67 ? resolveShowcaseEnvironmentUpdateDisplayUrl({
68 actualDisplayUrl: actualEnvironment.displayUrl,
69 presentedDisplayUrl: presentedEnvironment.displayUrl,
70 submittedDisplayUrl: updates.displayUrl,
71 })
72 : updates.displayUrl,
73 });
74 },
75 [environmentSections.localEnvironments, localEnvironments, onUpdateEnvironment],
76 );
77
78 return (
79 <SettingsScreen
80 title="Environments"
81 actions={[
82 {
83 accessibilityLabel: "Add environment",
84 icon: "plus",
85 onPress: () =>
86 navigation.navigate("SettingsSheet", {
87 screen: "SettingsContent",
88 params: { screen: "SettingsEnvironmentNew" },
89 }),
90 },
91 ]}
92 >
93 {Platform.OS !== "android" ? (
94 <NativeHeaderToolbar placement="right">
95 <NativeHeaderToolbar.Button
96 icon="plus"
97 onPress={() =>
98 navigation.navigate("SettingsSheet", {
99 screen: "SettingsContent",
100 params: { screen: "SettingsEnvironmentNew" },
101 })
102 }
103 separateBackground
104 tintColor={headerIconColor}
105 />
106 </NativeHeaderToolbar>
107 ) : null}
108 <ScrollView
109 contentInsetAdjustmentBehavior="automatic"
110 showsVerticalScrollIndicator={false}
111 className="flex-1"
112 contentContainerClassName="px-5 pt-4"
113 contentContainerStyle={{
114 paddingBottom: Math.max(insets.bottom, 18) + 18,
115 }}
116 >
117 <LocalEnvironmentList
118 environments={localEnvironments}
119 expandedId={expandedId}
120 onToggle={handleToggle}
121 onReconnect={onReconnectEnvironment}
122 onRemove={onRemoveEnvironmentPress}
123 onSetEnabled={onSetEnvironmentEnabled}
124 onUpdate={handleUpdateEnvironment}
125 />
126
127 {/* Always mounted: already-connected relay environments must stay
128 visible (and removable) even when cloud config is missing or the
129 user is signed out — the component gates discovery itself. */}
130 <CloudEnvironmentRows
131 connectedCloudEnvironments={connectedCloudEnvironments}
132 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
133 onRemoveEnvironment={onRemoveEnvironmentPress}
134 {...(SHOWCASE_ENABLED
135 ? {
136 showcaseAvailableEnvironments: SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
137 showcaseSignedIn: true,
138 }
139 : {})}
140 />
141 <GitHubRoutingSettings />
142 </ScrollView>
143 </SettingsScreen>
144 );
145}