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

SettingsEnvironmentsRouteScreen.tsx

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