Open History to see the last commit
1import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView";
2import { NativeHeaderToolbar } from "../../native/StackHeader";
3import { useAuth } from "@clerk/expo";
4import { StackActions, useNavigation } from "@react-navigation/native";
5import { useCallback, useEffect, useState } from "react";
6import { Platform, Pressable, RefreshControl, View } from "react-native";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8
9import { reportAtomCommandResult, settlePromise } from "@t3tools/client-runtime/state/runtime";
10import { AndroidSheetHeader } from "../../components/AndroidScreenHeader";
11import { AppText as Text } from "../../components/AppText";
12import { useRemoteConnections } from "../../state/use-remote-environment-registry";
13import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
14import { splitEnvironmentSections } from "../connection/environmentSections";
15import { useConnectionController } from "../connection/useConnectionController";
16import { optOutOfConnectOnboarding } from "./connectOnboardingOptOut";
17import { hasCloudPublicConfig } from "./publicConfig";
18
19/**
20 * Post-sign-in onboarding sheet for T3 Connect. Mobile never publishes
21 * environments itself — it consumes ones published elsewhere — so this simply
22 * surfaces the account's T3 Connect environments right after sign-in so every
23 * device can be connected in one go. It shows on every sign-in: sign-out
24 * clears the connected environments, so each new session starts from zero.
25 */
26export function ConnectOnboardingRouteScreen() {
27 const navigation = useNavigation();
28
29 // The route is deep-linkable; without cloud config the sheet would present
30 // empty with no chrome to dismiss it, so bail back out instead.
31 useEffect(() => {
32 if (hasCloudPublicConfig()) {
33 return;
34 }
35 if (navigation.canGoBack()) {
36 navigation.goBack();
37 } else {
38 navigation.dispatch(StackActions.replace("Home"));
39 }
40 }, [navigation]);
41
42 return hasCloudPublicConfig() ? <ConfiguredConnectOnboardingRouteScreen /> : null;
43}
44
45function ConfiguredConnectOnboardingRouteScreen() {
46 const navigation = useNavigation();
47 const insets = useSafeAreaInsets();
48 const { isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false });
49 const { connectedEnvironments, onSetEnvironmentEnabled, onRemoveEnvironmentPress } =
50 useRemoteConnections();
51 const { refreshRelayEnvironments } = useConnectionController();
52 const { connectedCloudEnvironments } = splitEnvironmentSections({
53 connectedEnvironments,
54 cloudEnvironments: null,
55 });
56
57 // Pull-to-refresh tracks its own spinner instead of discovery's refreshing
58 // flag, so background refreshes (e.g. the sign-in one) don't yank the
59 // content down.
60 const [isPullRefreshing, setIsPullRefreshing] = useState(false);
61 const handlePullRefresh = useCallback(() => {
62 void (async () => {
63 setIsPullRefreshing(true);
64 await refreshRelayEnvironments();
65 setIsPullRefreshing(false);
66 })();
67 }, [refreshRelayEnvironments]);
68
69 const handleClose = useCallback(() => {
70 navigation.goBack();
71 }, [navigation]);
72
73 // Persist before dismissing so a quick sign-out/sign-in cannot race ahead
74 // of the preference write; the write is a local secure-store update.
75 const handleDontShowAgain = useCallback(() => {
76 void (async () => {
77 if (userId) {
78 const result = await settlePromise(() => optOutOfConnectOnboarding(userId));
79 reportAtomCommandResult(result, { label: "connect onboarding opt-out" });
80 }
81 navigation.goBack();
82 })();
83 }, [navigation, userId]);
84
85 return (
86 <View collapsable={false} className="flex-1 bg-sheet">
87 {Platform.OS === "android" ? (
88 <AndroidSheetHeader
89 title="Set up T3 Connect"
90 actions={[{ accessibilityLabel: "Close", icon: "xmark", onPress: handleClose }]}
91 />
92 ) : (
93 <NativeHeaderToolbar placement="right">
94 <NativeHeaderToolbar.Button icon="xmark" onPress={handleClose} separateBackground />
95 </NativeHeaderToolbar>
96 )}
97 <ScrollView
98 alwaysBounceVertical
99 contentInsetAdjustmentBehavior="automatic"
100 showsVerticalScrollIndicator={false}
101 className="flex-1"
102 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
103 contentContainerStyle={{
104 gap: 16,
105 paddingHorizontal: 20,
106 paddingTop: 16,
107 }}
108 refreshControl={
109 <RefreshControl refreshing={isPullRefreshing} onRefresh={handlePullRefresh} />
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-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}