import { Redirect } from "expo-router"; import { useEffect, useState } from "react"; import { ActivityIndicator, View } from "react-native"; import { fetchAPI } from "@/lib/fetch"; import { useSession } from "@/lib/session"; const App = () => { const { isLoaded, isSignedIn, user } = useSession(); const [role, setRole] = useState(undefined); useEffect(() => { if (!isSignedIn) return; // Prefer the role cached at sign-in; fall back to a fresh fetch. if (user?.role !== undefined && user?.role !== null) { setRole(user.role); return; } fetchAPI("/(api)/user") .then((res) => setRole(res?.data?.role ?? null)) .catch(() => setRole(null)); }, [isSignedIn, user]); if (!isLoaded) { return ( ); } if (!isSignedIn) return ; // Still loading the user's role from the database. if (role === undefined) { return ( ); } if (role === "driver") return ; if (role === "rider") return ; // Signed in but no role chosen yet. return ; }; export default App;