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

ConnectOnboardingRouteScreen.tsx

139 lines5.4 KB

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;
43​
44function ConfiguredConnectOnboardingRouteScreen() {
45 const navigation = useNavigation();
46 const insets = useSafeAreaInsets();
47 const { isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false });
48 const { connectedEnvironments, onSetEnvironmentEnabled, onRemoveEnvironmentPress } =
49 useRemoteConnections();
50 const { refreshRelayEnvironments } = useConnectionController();
51 const { connectedCloudEnvironments } = splitEnvironmentSections({
52 connectedEnvironments,
53 cloudEnvironments: null,
54 });
55​
56 // Pull-to-refresh tracks its own spinner instead of discovery's refreshing
57 // flag, so background refreshes (e.g. the sign-in one) don't yank the
58 // content down.
59 const [isPullRefreshing, setIsPullRefreshing] = useState(false);
60 const handlePullRefresh = useCallback(() => {
61 void (async () => {
62 setIsPullRefreshing(true);
63 await refreshRelayEnvironments();
64 setIsPullRefreshing(false);
65 })();
66 }, [refreshRelayEnvironments]);
67​
68 const handleClose = useCallback(() => {
69 navigation.goBack();
70 }, [navigation]);
71​
72 // Persist before dismissing so a quick sign-out/sign-in cannot race ahead
73 // of the preference write; the write is a local secure-store update.
74 const handleDontShowAgain = useCallback(() => {
75 void (async () => {
76 if (userId) {
77 const result = await settlePromise(() => optOutOfConnectOnboarding(userId));
78 reportAtomCommandResult(result, { label: "connect onboarding opt-out" });
79 }
80 navigation.goBack();
81 })();
82 }, [navigation, userId]);
83​
84 return (
85 <View collapsable={false} className="flex-1 bg-sheet">
86 {Platform.OS === "android" ? (
87 <AndroidSheetHeader
88 title="Set up T3 Connect"
89 actions={[{ accessibilityLabel: "Close", icon: "xmark", onPress: handleClose }]}
90 />
91 ) : (
92 <NativeHeaderToolbar placement="right">
93 <NativeHeaderToolbar.Button icon="xmark" onPress={handleClose} separateBackground />
94 </NativeHeaderToolbar>
95 )}
96 <ScrollView
97 alwaysBounceVertical
98 contentInsetAdjustmentBehavior="automatic"
99 showsVerticalScrollIndicator={false}
100 className="flex-1"
101 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
102 contentContainerStyle={{
103 gap: 16,
104 paddingHorizontal: 20,
105 paddingTop: 16,
106 }}
107 refreshControl={
108 <RefreshControl refreshing={isPullRefreshing} onRefresh={handlePullRefresh} />
109 }
110 >
111 {isSignedIn ? (
112 <CloudEnvironmentRows
113 connectedCloudEnvironments={connectedCloudEnvironments}
114 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
115 onRemoveEnvironment={onRemoveEnvironmentPress}
116 showHeader={false}
117 />
118 ) : (
119 <View collapsable={false} className="rounded-[24px] bg-card p-5">
120 <Text className="text-sm leading-normal text-foreground-muted">
121 Sign in to your T3 account to set up T3 Connect.
122 </Text>
123 </View>
124 )}
125​
126 {userId ? (
127 <Pressable
128 accessibilityRole="button"
129 hitSlop={8}
130 onPress={handleDontShowAgain}
131 className="items-center py-1 active:opacity-70"
132 >
133 <Text className="text-xs text-foreground-muted">{"Don't show this again"}</Text>
134 </Pressable>
135 ) : null}
136 </ScrollView>
137 </View>
138 );