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 // Without cloud config, deep links must exit to avoid an empty, undismissable sheet.
26 useEffect(() => {
27 if (hasCloudPublicConfig()) {
28 return;
29 }
30 if (navigation.canGoBack()) {
31 navigation.goBack();
32 } else {
33 navigation.dispatch(StackActions.replace("Home"));
34 }
35 }, [navigation]);
36
37 return hasCloudPublicConfig() ? <ConfiguredConnectOnboardingRouteScreen /> : null;
38}
39
40function ConfiguredConnectOnboardingRouteScreen() {
41 const navigation = useNavigation();
42 const insets = useSafeAreaInsets();
43 const { isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false });
44 const { connectedEnvironments, onSetEnvironmentEnabled, onRemoveEnvironmentPress } =
45 useRemoteConnections();
46 const { refreshRelayEnvironments } = useConnectionController();
47 const { connectedCloudEnvironments } = splitEnvironmentSections({
48 connectedEnvironments,
49 cloudEnvironments: null,
50 });
51
52 const [isRefreshing, setIsRefreshing] = useState(false);
53 const handleRefresh = () => {
54 setIsRefreshing(true);
55 void refreshRelayEnvironments().finally(() => setIsRefreshing(false));
56 };
57
58 const handleClose = () => {
59 navigation.goBack();
60 };
61
62 // Persist before dismissing so quick sign-out/sign-in cannot outrun the write.
63 const handleDontShowAgain = () => {
64 void (async () => {
65 if (userId) {
66 const result = await settlePromise(() => optOutOfConnectOnboarding(userId));
67 reportAtomCommandResult(result, { label: "connect onboarding opt-out" });
68 }
69 navigation.goBack();
70 })();
71 };
72
73 return (
74 <View collapsable={false} className="flex-1 bg-sheet">
75 <SheetHeader
76 presentation={Platform.OS === "android" ? "inline" : "native"}
77 title="Set up T3 Connect"
78 onClose={handleClose}
79 trailing={
80 Platform.OS === "android" ? (
81 isRefreshing ? (
82 <View
83 className="size-12 items-center justify-center"
84 accessible
85 accessibilityLabel="Refreshing environments"
86 accessibilityState={{ busy: true }}
87 >
88 <AppActivityIndicator />
89 </View>
90 ) : (
91 <IconButton
92 accessibilityLabel="Refresh environments"
93 icon="arrow.clockwise"
94 onPress={handleRefresh}
95 />
96 )
97 ) : undefined
98 }
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 <Button label={"Don't show this again"} tone="text" onPress={handleDontShowAgain} />
135 ) : null}
136 </ScrollView>
137 </View>
138 );
139}