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

SettingsAuthRouteScreen.tsx

65 lines2.1 KB

1import { useAuth } from "@clerk/expo";
2import {
3 AuthView,
4 type UserProfileCustomPage,
5 UserProfileView,
6} from "@clerk/expo/native-components";
7import { StackActions, useNavigation } from "@react-navigation/native";
8import { useEffect, useLayoutEffect, useState } from "react";
9import { View } from "react-native";
10​
11import { hasCloudPublicConfig } from "../cloud/publicConfig";
12import { T3ConnectProfilePage } from "../cloud/T3ConnectProfilePage";
13​
14const USER_PROFILE_CUSTOM_PAGES = [
15 {
16 path: "t3-connect",
17 label: "T3 Connect",
18 icon: "globe",
19 content: <T3ConnectProfilePage />,
20 },
21] satisfies UserProfileCustomPage[];
22​
23export function SettingsAuthRouteScreen() {
24 const navigation = useNavigation();
25​
26 useLayoutEffect(() => {
27 if (!hasCloudPublicConfig()) {
28 navigation.dispatch(StackActions.replace("SettingsContent"));
29 }
30 }, [navigation]);
31​
32 return hasCloudPublicConfig() ? <ConfiguredSettingsAuthRouteScreen /> : null;
34​
35function ConfiguredSettingsAuthRouteScreen() {
36 const { isLoaded, isSignedIn } = useAuth({ treatPendingAsSignedOut: false });
37 const navigation = useNavigation();
38 const handleHostBack = () => navigation.dispatch(StackActions.popTo("SettingsContent"));
39 // Once signed in, stay on the profile view so signing out pops back instead of
40 // flashing the sign-in form.
41 const [hasBeenSignedIn, setHasBeenSignedIn] = useState(isSignedIn === true);
42 if (isSignedIn && !hasBeenSignedIn) setHasBeenSignedIn(true);
43​
44 useEffect(() => {
45 if (hasBeenSignedIn && isLoaded && isSignedIn === false) {
46 navigation.dispatch(StackActions.popTo("SettingsContent"));
47 }
48 }, [hasBeenSignedIn, isLoaded, isSignedIn, navigation]);
49​
50 return (
51 <View collapsable={false} className="flex-1 overflow-hidden bg-sheet">
52 {isLoaded ? (
53 hasBeenSignedIn ? (
54 <UserProfileView
55 customPages={USER_PROFILE_CUSTOM_PAGES}
56 isDismissible={false}
57 onHostBack={handleHostBack}
58 />
59 ) : (
60 <AuthView isDismissible={false} onHostBack={handleHostBack} />
61 )
62 ) : null}
63 </View>
64 );