import { useEffect, useState } from "react";
import { Redirect } from "expo-router";
import { useAuthStore } from "../src/store";
import { View, ActivityIndicator, StyleSheet } from "react-native";
import { needsOnboarding } from "../src/constants/onboarding";
import { getPendingInviteCode } from "../src/lib/pendingInviteCode";
export default function Index() {
const { isAuthenticated, isLoading, user } = useAuthStore();
const [pendingInviteCheck, setPendingInviteCheck] = useState<{ userId: string; hasInvite: boolean } | null>(null);
// Check once, after auth resolves, whether an invite code is waiting to be
// applied (e.g. the user tapped a join link before signing in).
useEffect(() => {
if (isLoading || !isAuthenticated || user?.couple_id) {
return;
}
let cancelled = false;
if (!user?.id) return;
setPendingInviteCheck(null);
getPendingInviteCode(user.id).then((code) => {
if (cancelled) return;
setPendingInviteCheck({ userId: user.id, hasInvite: !!code });
});
return () => {
cancelled = true;
};
}, [isLoading, isAuthenticated, user?.couple_id, user?.id]);
// Show loading state while auth is being determined
if (isLoading) {
return (
);
}
// Redirect based on auth state
if (!isAuthenticated) {
return ;
}
// If user hasn't completed onboarding or needs to re-onboard, go to onboarding
if (needsOnboarding(user?.onboarding_completed, user?.onboarding_version)) {
return ;
}
// A couple already exists for this user; no invite hand-off is relevant.
if (user?.couple_id) {
return ;
}
// Wait for the one-time pending invite code check before deciding the
// route, so a stashed code from a join link is applied without an extra
// trip through the home screen.
if (pendingInviteCheck?.userId !== user?.id) {
return (
);
}
if (pendingInviteCheck?.hasInvite) {
return ;
}
// Go to home/dashboard - users can pair from within the app
// No forced redirect to pairing for users without a couple
return ;
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#1a1a2e",
},
});