Open History to see the last commit
1import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
2import { SheetHeader } from "../../components/ui/SheetHeader";
3import { IconButton } from "../../components/ui/IconButton";
4import { Button } from "../../components/ui/Button";
5import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
6
7import { useAuth } from "@clerk/expo";
8import { StackActions, useNavigation } from "@react-navigation/native";
9import { useEffect, useState } from "react";
10import { Platform, RefreshControl, View } from "react-native";
11import { useSafeAreaInsets } from "react-native-safe-area-context";
12
13import { reportAtomCommandResult, settlePromise } from "@t3tools/client-runtime/state/runtime";
14import { AppText as Text } from "../../components/ui/AppText";
15import { useRemoteConnections } from "../../state/use-remote-environment-registry";
16import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
17import { splitEnvironmentSections } from "../connection/environmentSections";
18import { useConnectionController } from "../connection/useConnectionController";
19import { optOutOfConnectOnboarding } from "./connectOnboardingOptOut";
20import { hasCloudPublicConfig } from "./publicConfig";
21
22export function ConnectOnboardingRouteScreen() {
23 const navigation = useNavigation();
24
25 // The route is deep-linkable; without cloud config the sheet would present
26 // empty with no chrome to dismiss it, so bail back out instead.
27 useEffect(() => {
28 if (hasCloudPublicConfig()) {
29 return;
30 }
31 if (navigation.canGoBack()) {
32 navigation.goBack();
33 } else {
34 navigation.dispatch(StackActions.replace("Home"));
35 }
36 }, [navigation]);
37
38 return hasCloudPublicConfig() ? <ConfiguredConnectOnboardingRouteScreen /> : null;
39}
40
41function ConfiguredConnectOnboardingRouteScreen() {
42 const navigation = useNavigation();
43 const insets = useSafeAreaInsets();
44 const { isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false });
45 const { connectedEnvironments, onSetEnvironmentEnabled, onRemoveEnvironmentPress } =
46 useRemoteConnections();
47 const { refreshRelayEnvironments } = useConnectionController();
48 const { connectedCloudEnvironments } = splitEnvironmentSections({
49 connectedEnvironments,
50 cloudEnvironments: null,
51 });
52
53 const [isRefreshing, setIsRefreshing] = useState(false);
54 const handleRefresh = () => {
55 setIsRefreshing(true);
56 void refreshRelayEnvironments().finally(() => setIsRefreshing(false));
57 };
58
59 const handleClose = () => {
60 navigation.goBack();
61 };
62
63 // Persist before dismissing so a quick sign-out/sign-in cannot race ahead
64 // of the preference write; the write is a local secure-store update.
65 const handleDontShowAgain = () => {
66 void (async () => {
67 if (userId) {
68 const result = await settlePromise(() => optOutOfConnectOnboarding(userId));
69 reportAtomCommandResult(result, { label: "connect onboarding opt-out" });
70 }
71 navigation.goBack();
72 })();
73 };
74
75 return (
76 <View collapsable={false} className="flex-1 bg-sheet">
77 <SheetHeader
78 presentation={Platform.OS === "android" ? "inline" : "native"}
79 title="Set up T3 Connect"
80 onClose={handleClose}
81 trailing={
82 Platform.OS === "android" ? (
83 isRefreshing ? (
84 <View
85 className="size-12 items-center justify-center"
86 accessible
87 accessibilityLabel="Refreshing environments"
88 accessibilityState={{ busy: true }}
89 >
90 <AppActivityIndicator />
91 </View>
92 ) : (
93 <IconButton
94 accessibilityLabel="Refresh environments"
95 icon="arrow.clockwise"
96 onPress={handleRefresh}
97 />
98 )
99 ) : undefined
100 }
101 />
102 <ScrollView
103 nestedScrollEnabled
104 alwaysBounceVertical
105 contentInsetAdjustmentBehavior="automatic"
106 showsVerticalScrollIndicator={false}
107 className="flex-1"
108 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
109 contentContainerStyle={{
110 gap: 16,
111 paddingHorizontal: 20,
112 paddingTop: 16,
113 }}
114 refreshControl={
115 Platform.OS === "ios" ? (
116 <RefreshControl refreshing={isRefreshing} onRefresh={handleRefresh} />
117 ) : undefined
118 }
119 >
120 {isSignedIn ? (
121 <CloudEnvironmentRows
122 connectedCloudEnvironments={connectedCloudEnvironments}
123 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
124 onRemoveEnvironment={onRemoveEnvironmentPress}
125 showHeader={false}
126 />
127 ) : (
128 <View collapsable={false} className="rounded-[24px] bg-grouped-card p-5">
129 <Text className="text-sm leading-normal text-foreground-muted">
130 Sign in to your T3 account to set up T3 Connect.
131 </Text>
132 </View>
133 )}
134
135 {userId ? (
136 <Button label={"Don't show this again"} tone="text" onPress={handleDontShowAgain} />
137 ) : null}
138 </ScrollView>
139 </View>
140 );
141}