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

ConnectOnboardingRouteScreen.tsx

146 lines5.2 KB

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 { useCallback, 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;
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 = useCallback(() => {
53 void (async () => {
54 setIsRefreshing(true);
55 try {
56 await refreshRelayEnvironments();
57 } finally {
58 setIsRefreshing(false);
59 }
60 })();
61 }, [refreshRelayEnvironments]);
62​
63 const handleClose = useCallback(() => {
64 navigation.goBack();
65 }, [navigation]);
66​
67 // Persist before dismissing so a quick sign-out/sign-in cannot race ahead
68 // of the preference write; the write is a local secure-store update.
69 const handleDontShowAgain = useCallback(() => {
70 void (async () => {
71 if (userId) {
72 const result = await settlePromise(() => optOutOfConnectOnboarding(userId));
73 reportAtomCommandResult(result, { label: "connect onboarding opt-out" });
74 }
75 navigation.goBack();
76 })();
77 }, [navigation, userId]);
78​
79 return (
80 <View collapsable={false} className="flex-1 bg-sheet">
81 {Platform.OS === "android" ? (
82 <AndroidSheetHeader
83 title="Set up T3 Connect"
84 actions={[
85 {
86 accessibilityLabel: "Refresh environments",
87 disabled: isRefreshing,
88 icon: "arrow.clockwise",
89 loading: isRefreshing,
90 onPress: handleRefresh,
91 },
92 { accessibilityLabel: "Close", icon: "xmark", onPress: handleClose },
93 ]}
94 />
95 ) : (
96 <Toolbar placement="right">
97 <Toolbar.Button icon="xmark" onPress={handleClose} separateBackground />
98 </Toolbar>
99 )}
100 <ScrollView
101 nestedScrollEnabled
102 alwaysBounceVertical
103 contentInsetAdjustmentBehavior="automatic"
104 showsVerticalScrollIndicator={false}
105 className="flex-1"
106 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
107 contentContainerStyle={{
108 gap: 16,
109 paddingHorizontal: 20,
110 paddingTop: 16,
111 }}
112 refreshControl={
113 Platform.OS === "ios" ? (
114 <RefreshControl refreshing={isRefreshing} onRefresh={handleRefresh} />
115 ) : undefined
116 }
117 >
118 {isSignedIn ? (
119 <CloudEnvironmentRows
120 connectedCloudEnvironments={connectedCloudEnvironments}
121 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
122 onRemoveEnvironment={onRemoveEnvironmentPress}
123 showHeader={false}
124 />
125 ) : (
126 <View collapsable={false} className="rounded-[24px] bg-grouped-card p-5">
127 <Text className="text-sm leading-normal text-foreground-muted">
128 Sign in to your T3 account to set up T3 Connect.
129 </Text>
130 </View>
131 )}
132​
133 {userId ? (
134 <Pressable
135 accessibilityRole="button"
136 hitSlop={8}
137 onPress={handleDontShowAgain}
138 className="items-center py-1 active:opacity-70"
139 >
140 <Text className="text-xs text-foreground-muted">{"Don't show this again"}</Text>
141 </Pressable>
142 ) : null}
143 </ScrollView>
144 </View>
145 );