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

ConnectOnboardingRouteScreen.tsx

105 lines3.9 KB

1import { AppBar } from "../../components/ui/AppBar";
2import { Button } from "../../components/ui/Button";
3import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
4​
5import { useAuth } from "@clerk/expo";
6import { useNavigation } from "@react-navigation/native";
7import { useState } from "react";
8import { RefreshControl, View } from "react-native";
9import { useSafeAreaInsets } from "react-native-safe-area-context";
10​
11import { reportAtomCommandResult, settlePromise } from "@t3tools/client-runtime/state/runtime";
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​
20// Only reachable with cloud config: onboarding is requested after a T3 Connect
21// sign-in, and appLinking drops the deep link otherwise.
22export function ConnectOnboardingRouteScreen() {
23 return hasCloudPublicConfig() ? <ConfiguredConnectOnboardingRouteScreen /> : null;
25​
26function ConfiguredConnectOnboardingRouteScreen() {
27 const navigation = useNavigation();
28 const insets = useSafeAreaInsets();
29 const { isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false });
30 const { connectedEnvironments, onSetEnvironmentEnabled, onRemoveEnvironmentPress } =
31 useRemoteConnections();
32 const { refreshRelayEnvironments } = useConnectionController();
33 const { connectedCloudEnvironments } = splitEnvironmentSections({
34 connectedEnvironments,
35 cloudEnvironments: null,
36 });
37​
38 const [isRefreshing, setIsRefreshing] = useState(false);
39 const handleRefresh = () => {
40 setIsRefreshing(true);
41 void refreshRelayEnvironments().finally(() => setIsRefreshing(false));
42 };
43​
44 const handleClose = () => {
45 navigation.goBack();
46 };
47​
48 // Persist before dismissing so quick sign-out/sign-in cannot outrun the write.
49 const handleDontShowAgain = () => {
50 void (async () => {
51 if (userId) {
52 const result = await settlePromise(() => optOutOfConnectOnboarding(userId));
53 reportAtomCommandResult(result, { label: "connect onboarding opt-out" });
54 }
55 navigation.goBack();
56 })();
57 };
58​
59 return (
60 <View collapsable={false} className="flex-1 bg-sheet">
61 <AppBar
62 title="Set up T3 Connect"
63 onClose={handleClose}
64 refresh={{
65 accessibilityLabel: "Refresh environments",
66 disabled: isRefreshing,
67 onPress: handleRefresh,
68 }}
69 />
70 <ScrollView
71 nestedScrollEnabled
72 alwaysBounceVertical
73 contentInsetAdjustmentBehavior="automatic"
74 showsVerticalScrollIndicator={false}
75 className="flex-1"
76 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
77 contentContainerStyle={{
78 gap: 16,
79 paddingHorizontal: 20,
80 paddingTop: 16,
81 }}
82 refreshControl={<RefreshControl refreshing={isRefreshing} onRefresh={handleRefresh} />}
83 >
84 {isSignedIn ? (
85 <CloudEnvironmentRows
86 connectedCloudEnvironments={connectedCloudEnvironments}
87 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
88 onRemoveEnvironment={onRemoveEnvironmentPress}
89 showHeader={false}
90 />
91 ) : (
92 <View collapsable={false} className="rounded-[24px] bg-grouped-card p-5">
93 <Text className="text-sm leading-normal text-foreground-muted">
94 Sign in to your T3 account to set up T3 Connect.
95 </Text>
96 </View>
97 )}
98​
99 {userId ? (
100 <Button label={"Don't show this again"} tone="text" onPress={handleDontShowAgain} />
101 ) : null}
102 </ScrollView>
103 </View>
104 );