Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { useNavigation } from "@react-navigation/native";
3import { useEffect } from "react";
4
5import { appAtomRegistry } from "../../state/atom-registry";
6import { clearConnectOnboardingRequest, connectOnboardingRequestAtom } from "./connectOnboarding";
7import { isConnectOnboardingOptedOut } from "./connectOnboardingOptOut";
8
9// Sign-in happens inside the Settings sheet; give its session-state transition
10// a beat to settle before presenting another formSheet on top.
11const PRESENT_ONBOARDING_DELAY_MS = 600;
12
13/**
14 * Lives apart from connectOnboarding.ts so non-navigation consumers
15 * (CloudAuthProvider) do not pull
16 * @react-navigation into their module graph.
17 */
18export function useConnectOnboardingNavigation(): void {
19 const navigation = useNavigation();
20 const requestedAccountId = useAtomValue(connectOnboardingRequestAtom);
21
22 useEffect(() => {
23 if (requestedAccountId === null) {
24 return;
25 }
26 let cancelled = false;
27 const timer = setTimeout(() => {
28 void (async () => {
29 const optedOut = await isConnectOnboardingOptedOut(requestedAccountId).catch(() => false);
30 // The cancelled flag covers effect re-runs, but a sign-out can clear
31 // the request atom moments before this render commits — re-check the
32 // atom so a stale request never presents the sheet.
33 if (cancelled || appAtomRegistry.get(connectOnboardingRequestAtom) !== requestedAccountId) {
34 return;
35 }
36 clearConnectOnboardingRequest();
37 if (!optedOut) {
38 navigation.navigate("ConnectOnboarding");
39 }
40 })();
41 }, PRESENT_ONBOARDING_DELAY_MS);
42 return () => {
43 cancelled = true;
44 clearTimeout(timer);
45 };
46 }, [navigation, requestedAccountId]);
47}