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

SettingsEnvironmentsRouteScreen.tsx

182 lines7.3 KB

1import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
2import { useNavigation } from "@react-navigation/native";
3import { SymbolView } from "../../components/AppSymbol";
4import type { EnvironmentId } from "@t3tools/contracts";
5import { useCallback, useState } from "react";
6import { Platform, ScrollView, View } from "react-native";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8​
9import { AppText as Text } from "../../components/AppText";
10import { AndroidScreenHeader } from "../../components/AndroidScreenHeader";
11import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
12import { ConnectionEnvironmentRow } from "../connection/ConnectionEnvironmentRow";
13import { GitHubRoutingSettings } from "../connection/GitHubRoutingSettings";
14import { splitEnvironmentSections } from "../connection/environmentSections";
15import { cn } from "../../lib/cn";
16import { useUniwindTheme } from "../../lib/useUniwindTheme";
17import { useRemoteConnections } from "../../state/use-remote-environment-registry";
18import {
19 applyShowcaseLocalEnvironmentDisplayUrls,
20 resolveShowcaseEnvironmentUpdateDisplayUrl,
21 SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
22 SHOWCASE_CONNECTED_CLOUD_ENVIRONMENTS,
23} from "../showcase/showcaseEnvironmentRows";
24​
25const SHOWCASE_ENABLED = process.env.EXPO_PUBLIC_SHOWCASE === "1";
26​
27export function SettingsEnvironmentsRouteScreen() {
28 const {
29 connectedEnvironments,
30 onReconnectEnvironment,
31 onRemoveEnvironmentPress,
32 onSetEnvironmentEnabled,
33 onUpdateEnvironment,
34 } = useRemoteConnections();
35 const navigation = useNavigation();
36 const insets = useSafeAreaInsets();
37 const environmentSections = splitEnvironmentSections({
38 connectedEnvironments,
39 cloudEnvironments: null,
40 });
41 const localEnvironments = SHOWCASE_ENABLED
42 ? applyShowcaseLocalEnvironmentDisplayUrls(environmentSections.localEnvironments)
43 : environmentSections.localEnvironments;
44 const connectedCloudEnvironments = SHOWCASE_ENABLED
45 ? SHOWCASE_CONNECTED_CLOUD_ENVIRONMENTS
46 : environmentSections.connectedCloudEnvironments;
47 const hasLocalEnvironments = localEnvironments.length > 0;
48 const [expandedId, setExpandedId] = useState<EnvironmentId | null>(null);
49 const headerIconColor = useUniwindTheme()["--color-icon"];
50​
51 const handleToggle = useCallback((environmentId: EnvironmentId) => {
52 setExpandedId((prev) => (prev === environmentId ? null : environmentId));
53 }, []);
54 const handleUpdateEnvironment = useCallback(
55 (
56 environmentId: EnvironmentId,
57 updates: { readonly label: string; readonly displayUrl: string },
58 ) => {
59 if (!SHOWCASE_ENABLED) return onUpdateEnvironment(environmentId, updates);
60 const actualEnvironment = environmentSections.localEnvironments.find(
61 (environment) => environment.environmentId === environmentId,
62 );
63 const presentedEnvironment = localEnvironments.find(
64 (environment) => environment.environmentId === environmentId,
65 );
66 return onUpdateEnvironment(environmentId, {
67 ...updates,
68 displayUrl:
69 actualEnvironment && presentedEnvironment
70 ? resolveShowcaseEnvironmentUpdateDisplayUrl({
71 actualDisplayUrl: actualEnvironment.displayUrl,
72 presentedDisplayUrl: presentedEnvironment.displayUrl,
73 submittedDisplayUrl: updates.displayUrl,
74 })
75 : updates.displayUrl,
76 });
77 },
78 [environmentSections.localEnvironments, localEnvironments, onUpdateEnvironment],
79 );
80​
81 return (
82 <View collapsable={false} className="flex-1 bg-sheet">
83 {Platform.OS === "android" ? (
84 <>
85 {/* Android renders its own in-screen header instead of the native bar. */}
86 <NativeStackScreenOptions options={{ headerShown: false }} />
87 <AndroidScreenHeader
88 title="Environments"
89 onBack={() => navigation.goBack()}
90 actions={[
91 {
92 accessibilityLabel: "Add environment",
93 icon: "plus",
94 onPress: () =>
95 navigation.navigate("SettingsSheet", {
96 screen: "SettingsContent",
97 params: { screen: "SettingsEnvironmentNew" },
98 }),
99 },
100 ]}
101 />
102 </>
103 ) : (
104 <NativeHeaderToolbar placement="right">
105 <NativeHeaderToolbar.Button
106 icon="plus"
107 onPress={() =>
108 navigation.navigate("SettingsSheet", {
109 screen: "SettingsContent",
110 params: { screen: "SettingsEnvironmentNew" },
111 })
112 }
113 separateBackground
114 tintColor={headerIconColor}
115 />
116 </NativeHeaderToolbar>
117 )}
118 <ScrollView
119 contentInsetAdjustmentBehavior="automatic"
120 showsVerticalScrollIndicator={false}
121 className="flex-1"
122 contentContainerClassName="px-5 pt-4"
123 contentContainerStyle={{
124 paddingBottom: Math.max(insets.bottom, 18) + 18,
125 }}
126 >
127 {hasLocalEnvironments ? (
128 <View collapsable={false} className="overflow-hidden rounded-[24px] bg-card">
129 {localEnvironments.map((environment, index) => (
130 <View
131 key={environment.environmentId}
132 collapsable={false}
133 className={cn(index !== 0 && "border-t border-border")}
134 >
135 <ConnectionEnvironmentRow
136 environment={environment}
137 expanded={expandedId === environment.environmentId}
138 onToggle={() => handleToggle(environment.environmentId)}
139 onReconnect={onReconnectEnvironment}
140 onRemove={onRemoveEnvironmentPress}
141 onSetEnabled={onSetEnvironmentEnabled}
142 onUpdate={handleUpdateEnvironment}
143 />
144 </View>
145 ))}
146 </View>
147 ) : (
148 <View collapsable={false} className="items-center gap-3 rounded-[24px] bg-card px-6 py-8">
149 <View className="h-12 w-12 items-center justify-center rounded-[16px] bg-subtle">
150 <SymbolView
151 name="point.3.connected.trianglepath.dotted"
152 size={20}
153 tintColorClassName={"accent-icon-muted"}
154 type="monochrome"
155 />
156 </View>
157 <Text className="text-center text-sm leading-normal text-foreground-muted">
158 No environments connected yet.{"\n"}Tap{" "}
159 <Text className="font-t3-bold text-foreground">+</Text> to add one.
160 </Text>
161 </View>
162 )}
163​
164 {/* Always mounted: already-connected relay environments must stay
165 visible (and removable) even when cloud config is missing or the
166 user is signed out — the component gates discovery itself. */}
167 <CloudEnvironmentRows
168 connectedCloudEnvironments={connectedCloudEnvironments}
169 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
170 onRemoveEnvironment={onRemoveEnvironmentPress}
171 {...(SHOWCASE_ENABLED
172 ? {
173 showcaseAvailableEnvironments: SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
174 showcaseSignedIn: true,
175 }
176 : {})}
177 />
178 <GitHubRoutingSettings />
179 </ScrollView>
180 </View>
181 );