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 const [isRefreshing, setIsRefreshing] = useState(false);
58 const handleRefresh = useCallback(() => {
59 void (async () => {
60 setIsRefreshing(true);
61 try {
62 await refreshRelayEnvironments();
63 } finally {
64 setIsRefreshing(false);
65 }
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={[
91 {
92 accessibilityLabel: "Refresh environments",
93 disabled: isRefreshing,
94 icon: "arrow.clockwise",
95 loading: isRefreshing,
96 onPress: handleRefresh,
97 },
98 { accessibilityLabel: "Close", icon: "xmark", onPress: handleClose },
99 ]}
100 />
101 ) : (
102 <NativeHeaderToolbar placement="right">
103 <NativeHeaderToolbar.Button icon="xmark" onPress={handleClose} separateBackground />
104 </NativeHeaderToolbar>
105 )}
106 <ScrollView
107 nestedScrollEnabled
108 alwaysBounceVertical
109 contentInsetAdjustmentBehavior="automatic"
110 showsVerticalScrollIndicator={false}
111 className="flex-1"
112 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
113 contentContainerStyle={{
114 gap: 16,
115 paddingHorizontal: 20,
116 paddingTop: 16,
117 }}
118 refreshControl={
119 Platform.OS === "ios" ? (
120 <RefreshControl refreshing={isRefreshing} onRefresh={handleRefresh} />
121 ) : undefined
122 }
123 >
124 {isSignedIn ? (
125 <CloudEnvironmentRows
126 connectedCloudEnvironments={connectedCloudEnvironments}
127 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
128 onRemoveEnvironment={onRemoveEnvironmentPress}
129 showHeader={false}
130 />
131 ) : (
132 <View collapsable={false} className="rounded-[24px] bg-grouped-card p-5">
133 <Text className="text-sm leading-normal text-foreground-muted">
134 Sign in to your T3 account to set up T3 Connect.
135 </Text>
136 </View>
137 )}
138
139 {userId ? (
140 <Pressable
141 accessibilityRole="button"
142 hitSlop={8}
143 onPress={handleDontShowAgain}
144 className="items-center py-1 active:opacity-70"
145 >
146 <Text className="text-xs text-foreground-muted">{"Don't show this again"}</Text>
147 </Pressable>
148 ) : null}
149 </ScrollView>
150 </View>
151 );
152}