Open History to see the last commit
1import { NativeHeaderToolbar } from "../../native/StackHeader";
2import { useAuth } from "@clerk/expo";
3import { StackActions, useNavigation } from "@react-navigation/native";
4import { useCallback, useEffect, useState } from "react";
5import { Platform, Pressable, RefreshControl, ScrollView, View } from "react-native";
6import { useSafeAreaInsets } from "react-native-safe-area-context";
7
8import { reportAtomCommandResult, settlePromise } from "@t3tools/client-runtime/state/runtime";
9import { AndroidSheetHeader } from "../../components/AndroidScreenHeader";
10import { AppText as Text } from "../../components/AppText";
11import { useRemoteConnections } from "../../state/use-remote-environment-registry";
12import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
13import { splitEnvironmentSections } from "../connection/environmentSections";
14import { useConnectionController } from "../connection/useConnectionController";
15import { optOutOfConnectOnboarding } from "./connectOnboardingOptOut";
16import { hasCloudPublicConfig } from "./publicConfig";
17
18/**
19 * Post-sign-in onboarding sheet for T3 Connect. Mobile never publishes
20 * environments itself — it consumes ones published elsewhere — so this simply
21 * surfaces the account's T3 Connect environments right after sign-in so every
22 * device can be connected in one go. It shows on every sign-in: sign-out
23 * clears the connected environments, so each new session starts from zero.
24 */
25export function ConnectOnboardingRouteScreen() {
26 const navigation = useNavigation();
27
28 // The route is deep-linkable; without cloud config the sheet would present
29 // empty with no chrome to dismiss it, so bail back out instead.
30 useEffect(() => {
31 if (hasCloudPublicConfig()) {
32 return;
33 }
34 if (navigation.canGoBack()) {
35 navigation.goBack();
36 } else {
37 navigation.dispatch(StackActions.replace("Home"));
38 }
39 }, [navigation]);
40
41 return hasCloudPublicConfig() ? <ConfiguredConnectOnboardingRouteScreen /> : null;
42}
43
44function ConfiguredConnectOnboardingRouteScreen() {
45 const navigation = useNavigation();
46 const insets = useSafeAreaInsets();
47 const { isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false });
48 const { connectedEnvironments, onReconnectEnvironment } = useRemoteConnections();
49 const { refreshRelayEnvironments } = useConnectionController();
50 const { connectedCloudEnvironments } = splitEnvironmentSections({
51 connectedEnvironments,
52 cloudEnvironments: null,
53 });
54
55 // Pull-to-refresh tracks its own spinner instead of discovery's refreshing
56 // flag, so background refreshes (e.g. the sign-in one) don't yank the
57 // content down.
58 const [isPullRefreshing, setIsPullRefreshing] = useState(false);
59 const handlePullRefresh = useCallback(() => {
60 void (async () => {
61 setIsPullRefreshing(true);
62 await refreshRelayEnvironments();
63 setIsPullRefreshing(false);
64 })();
65 }, [refreshRelayEnvironments]);
66
67 const handleClose = useCallback(() => {
68 navigation.goBack();
69 }, [navigation]);
70
71 // Persist before dismissing so a quick sign-out/sign-in cannot race ahead
72 // of the preference write; the write is a local secure-store update.
73 const handleDontShowAgain = useCallback(() => {
74 void (async () => {
75 if (userId) {
76 const result = await settlePromise(() => optOutOfConnectOnboarding(userId));
77 reportAtomCommandResult(result, { label: "connect onboarding opt-out" });
78 }
79 navigation.goBack();
80 })();
81 }, [navigation, userId]);
82
83 return (
84 <View collapsable={false} className="flex-1 bg-sheet">
85 {Platform.OS === "android" ? (
86 <AndroidSheetHeader
87 title="Set up T3 Connect"
88 actions={[{ accessibilityLabel: "Close", icon: "xmark", onPress: handleClose }]}
89 />
90 ) : (
91 <NativeHeaderToolbar placement="right">
92 <NativeHeaderToolbar.Button icon="xmark" onPress={handleClose} separateBackground />
93 </NativeHeaderToolbar>
94 )}
95 <ScrollView
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 <RefreshControl refreshing={isPullRefreshing} onRefresh={handlePullRefresh} />
108 }
109 >
110 {isSignedIn ? (
111 <CloudEnvironmentRows
112 connectedCloudEnvironments={connectedCloudEnvironments}
113 onReconnectEnvironment={onReconnectEnvironment}
114 showHeader={false}
115 />
116 ) : (
117 <View collapsable={false} className="rounded-[24px] bg-card p-5">
118 <Text className="text-sm leading-normal text-foreground-muted">
119 Sign in to your T3 account to set up T3 Connect.
120 </Text>
121 </View>
122 )}
123
124 {userId ? (
125 <Pressable
126 accessibilityRole="button"
127 hitSlop={8}
128 onPress={handleDontShowAgain}
129 className="items-center py-1 active:opacity-70"
130 >
131 <Text className="text-xs text-foreground-muted">{"Don't show this again"}</Text>
132 </Pressable>
133 ) : null}
134 </ScrollView>
135 </View>
136 );
137}