Open History to see the last commit
1import { Toolbar } from "../../components/ui/Toolbar";
2import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
3
4import { useAuth } from "@clerk/expo";
5import { StackActions, useNavigation } from "@react-navigation/native";
6import { useEffect, useState } from "react";
7import { Platform, Pressable, RefreshControl, View } from "react-native";
8import { useSafeAreaInsets } from "react-native-safe-area-context";
9
10import { reportAtomCommandResult, settlePromise } from "@t3tools/client-runtime/state/runtime";
11import { AndroidSheetHeader } from "../../components/ui/AndroidScreenHeader";
12import { AppText as Text } from "../../components/ui/AppText";
13import { useRemoteConnections } from "../../state/use-remote-environment-registry";
14import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
15import { splitEnvironmentSections } from "../connection/environmentSections";
16import { useConnectionController } from "../connection/useConnectionController";
17import { optOutOfConnectOnboarding } from "./connectOnboardingOptOut";
18import { hasCloudPublicConfig } from "./publicConfig";
19
20export function ConnectOnboardingRouteScreen() {
21 const navigation = useNavigation();
22
23 // The route is deep-linkable; without cloud config the sheet would present
24 // empty with no chrome to dismiss it, so bail back out instead.
25 useEffect(() => {
26 if (hasCloudPublicConfig()) {
27 return;
28 }
29 if (navigation.canGoBack()) {
30 navigation.goBack();
31 } else {
32 navigation.dispatch(StackActions.replace("Home"));
33 }
34 }, [navigation]);
35
36 return hasCloudPublicConfig() ? <ConfiguredConnectOnboardingRouteScreen /> : null;
37}
38
39function ConfiguredConnectOnboardingRouteScreen() {
40 const navigation = useNavigation();
41 const insets = useSafeAreaInsets();
42 const { isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false });
43 const { connectedEnvironments, onSetEnvironmentEnabled, onRemoveEnvironmentPress } =
44 useRemoteConnections();
45 const { refreshRelayEnvironments } = useConnectionController();
46 const { connectedCloudEnvironments } = splitEnvironmentSections({
47 connectedEnvironments,
48 cloudEnvironments: null,
49 });
50
51 const [isRefreshing, setIsRefreshing] = useState(false);
52 const handleRefresh = () => {
53 setIsRefreshing(true);
54 void refreshRelayEnvironments().finally(() => setIsRefreshing(false));
55 };
56
57 const handleClose = () => {
58 navigation.goBack();
59 };
60
61 // Persist before dismissing so a quick sign-out/sign-in cannot race ahead
62 // of the preference write; the write is a local secure-store update.
63 const handleDontShowAgain = () => {
64 void (async () => {
65 if (userId) {
66 const result = await settlePromise(() => optOutOfConnectOnboarding(userId));
67 reportAtomCommandResult(result, { label: "connect onboarding opt-out" });
68 }
69 navigation.goBack();
70 })();
71 };
72
73 return (
74 <View collapsable={false} className="flex-1 bg-sheet">
75 {Platform.OS === "android" ? (
76 <AndroidSheetHeader
77 title="Set up T3 Connect"
78 actions={[
79 {
80 accessibilityLabel: "Refresh environments",
81 disabled: isRefreshing,
82 icon: "arrow.clockwise",
83 loading: isRefreshing,
84 onPress: handleRefresh,
85 },
86 { accessibilityLabel: "Close", icon: "xmark", onPress: handleClose },
87 ]}
88 />
89 ) : (
90 <Toolbar placement="right">
91 <Toolbar.Button icon="xmark" onPress={handleClose} separateBackground />
92 </Toolbar>
93 )}
94 <ScrollView
95 nestedScrollEnabled
96 alwaysBounceVertical
97 contentInsetAdjustmentBehavior="automatic"
98 showsVerticalScrollIndicator={false}
99 className="flex-1"
100 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
101 contentContainerStyle={{
102 gap: 16,
103 paddingHorizontal: 20,
104 paddingTop: 16,
105 }}
106 refreshControl={
107 Platform.OS === "ios" ? (
108 <RefreshControl refreshing={isRefreshing} onRefresh={handleRefresh} />
109 ) : undefined
110 }
111 >
112 {isSignedIn ? (
113 <CloudEnvironmentRows
114 connectedCloudEnvironments={connectedCloudEnvironments}
115 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
116 onRemoveEnvironment={onRemoveEnvironmentPress}
117 showHeader={false}
118 />
119 ) : (
120 <View collapsable={false} className="rounded-[24px] bg-grouped-card p-5">
121 <Text className="text-sm leading-normal text-foreground-muted">
122 Sign in to your T3 account to set up T3 Connect.
123 </Text>
124 </View>
125 )}
126
127 {userId ? (
128 <Pressable
129 accessibilityRole="button"
130 hitSlop={8}
131 onPress={handleDontShowAgain}
132 className="items-center py-1 active:opacity-70"
133 >
134 <Text className="text-xs text-foreground-muted">{"Don't show this again"}</Text>
135 </Pressable>
136 ) : null}
137 </ScrollView>
138 </View>
139 );
140}