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

ConnectOnboardingRouteScreen.tsx

127 lines4.7 KB

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 { useNavigation } from "@react-navigation/native";
9import { 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​
22// Only reachable with cloud config: onboarding is requested after a T3 Connect
23// sign-in, and appLinking drops the deep link otherwise.
24export function ConnectOnboardingRouteScreen() {
25 return hasCloudPublicConfig() ? <ConfiguredConnectOnboardingRouteScreen /> : null;
27​
28function ConfiguredConnectOnboardingRouteScreen() {
29 const navigation = useNavigation();
30 const insets = useSafeAreaInsets();
31 const { isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false });
32 const { connectedEnvironments, onSetEnvironmentEnabled, onRemoveEnvironmentPress } =
33 useRemoteConnections();
34 const { refreshRelayEnvironments } = useConnectionController();
35 const { connectedCloudEnvironments } = splitEnvironmentSections({
36 connectedEnvironments,
37 cloudEnvironments: null,
38 });
39​
40 const [isRefreshing, setIsRefreshing] = useState(false);
41 const handleRefresh = () => {
42 setIsRefreshing(true);
43 void refreshRelayEnvironments().finally(() => setIsRefreshing(false));
44 };
45​
46 const handleClose = () => {
47 navigation.goBack();
48 };
49​
50 // Persist before dismissing so quick sign-out/sign-in cannot outrun the write.
51 const handleDontShowAgain = () => {
52 void (async () => {
53 if (userId) {
54 const result = await settlePromise(() => optOutOfConnectOnboarding(userId));
55 reportAtomCommandResult(result, { label: "connect onboarding opt-out" });
56 }
57 navigation.goBack();
58 })();
59 };
60​
61 return (
62 <View collapsable={false} className="flex-1 bg-sheet">
63 <SheetHeader
64 presentation={Platform.OS === "android" ? "inline" : "native"}
65 title="Set up T3 Connect"
66 onClose={handleClose}
67 trailing={
68 Platform.OS === "android" ? (
69 isRefreshing ? (
70 <View
71 className="size-12 items-center justify-center"
72 accessible
73 accessibilityLabel="Refreshing environments"
74 accessibilityState={{ busy: true }}
75 >
76 <AppActivityIndicator />
77 </View>
78 ) : (
79 <IconButton
80 accessibilityLabel="Refresh environments"
81 icon="arrow.clockwise"
82 onPress={handleRefresh}
83 />
84 )
85 ) : undefined
86 }
87 />
88 <ScrollView
89 nestedScrollEnabled
90 alwaysBounceVertical
91 contentInsetAdjustmentBehavior="automatic"
92 showsVerticalScrollIndicator={false}
93 className="flex-1"
94 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
95 contentContainerStyle={{
96 gap: 16,
97 paddingHorizontal: 20,
98 paddingTop: 16,
99 }}
100 refreshControl={
101 Platform.OS === "ios" ? (
102 <RefreshControl refreshing={isRefreshing} onRefresh={handleRefresh} />
103 ) : undefined
104 }
105 >
106 {isSignedIn ? (
107 <CloudEnvironmentRows
108 connectedCloudEnvironments={connectedCloudEnvironments}
109 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
110 onRemoveEnvironment={onRemoveEnvironmentPress}
111 showHeader={false}
112 />
113 ) : (
114 <View collapsable={false} className="rounded-[24px] bg-grouped-card p-5">
115 <Text className="text-sm leading-normal text-foreground-muted">
116 Sign in to your T3 account to set up T3 Connect.
117 </Text>
118 </View>
119 )}
120​
121 {userId ? (
122 <Button label={"Don't show this again"} tone="text" onPress={handleDontShowAgain} />
123 ) : null}
124 </ScrollView>
125 </View>
126 );