1import {
2  getBuildInfoAsync,
3  getCrashReport,
4  installationID,
5  isDevice,
6  updatesConfig,
7  loadFontsAsync,
8  consumeNavigationStateAsync,
9} from '../native-modules/DevLauncherInternal';
10import { getMenuPreferencesAsync } from '../native-modules/DevMenuPreferences';
11import { AppProvidersProps } from '../providers/AppProviders';
12import { defaultQueryOptions } from '../providers/QueryProvider';
13import { prefetchBranchesForApp } from '../queries/useBranchesForApp';
14import { getDevSessionsAsync } from './getDevSessionsAsync';
15import { restoreUserAsync } from './restoreUserAsync';
16
17export async function getInitialData(): Promise<Partial<AppProvidersProps>> {
18  const [initialBuildInfo, initialDevMenuPreferences, initialCrashReport, initialNavigationState] =
19    await Promise.all([
20      getBuildInfoAsync(),
21      getMenuPreferencesAsync(),
22      getCrashReport(),
23      consumeNavigationStateAsync(),
24    ]);
25
26  // todo - move this to native entirely? no need to run on app mount
27  await loadFontsAsync();
28
29  const initialUserData = await restoreUserAsync().catch((error) => {
30    // likely network request failure -- no need to do anything
31    console.log({ error });
32    return Promise.resolve(null);
33  });
34
35  const isAuthenticated = initialUserData != null;
36
37  const initialDevSessions = await getDevSessionsAsync({
38    isAuthenticated,
39    installationID,
40    isDevice,
41    timeout: 1500,
42  }).catch((error) => {
43    // likely network request failure -- no need to do anything
44    console.log({ error });
45    return Promise.resolve([]);
46  });
47
48  if (isAuthenticated && updatesConfig.usesEASUpdates) {
49    prefetchBranchesForApp(
50      updatesConfig.appId,
51      updatesConfig.runtimeVersion,
52      defaultQueryOptions.pageSize
53    ).catch((error) => {
54      // this is an optimistic fetch - not necessary to show the user anything if this fails
55      console.log({ error });
56    });
57  }
58
59  return {
60    initialDevSessions,
61    initialUserData,
62    initialBuildInfo,
63    initialDevMenuPreferences,
64    initialCrashReport,
65    initialNavigationState,
66  };
67}
68