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

connectOnboarding.ts

46 lines1.6 KB

1import { rootNavigationRef, whenRootNavigationReady } from "../../lib/rootNavigation";
2import { isConnectOnboardingOptedOut } from "./connectOnboardingOptOut";
3​
4// Sign-in happens in its own modal; give its session-state transition a beat to
5// settle before presenting another formSheet on top.
6const PRESENT_ONBOARDING_DELAY_MS = 600;
7​
8interface PendingRequest {
9 readonly accountId: string;
10 readonly timer: ReturnType<typeof setTimeout>;
12​
13// Retain the account id so sign-out cannot present onboarding for the wrong account.
14let pending: PendingRequest | null = null;
15​
16export function requestConnectOnboarding(accountId: string): void {
17 clearConnectOnboardingRequest();
18 const request: PendingRequest = {
19 accountId,
20 timer: setTimeout(() => {
21 void (async () => {
22 const optedOut = await isConnectOnboardingOptedOut(accountId).catch(() => false);
23 // A sign-out or a newer sign-in may have replaced this request while
24 // the opt-out read was in flight.
25 if (pending !== request) return;
26 if (optedOut) {
27 pending = null;
28 return;
29 }
30 // Stays pending until navigation is ready, so a sign-out meanwhile still cancels it.
31 whenRootNavigationReady(() => {
32 if (pending !== request) return;
33 pending = null;
34 rootNavigationRef.navigate("ConnectOnboarding");
35 });
36 })();
37 }, PRESENT_ONBOARDING_DELAY_MS),
38 };
39 pending = request;
41​
42export function clearConnectOnboardingRequest(): void {
43 if (pending === null) return;
44 clearTimeout(pending.timer);
45 pending = null;