export function initializeApp()

in src/actions/app-actions.ts [957:1067]


export function initializeApp(
  config: AppConfig,
  issueId?: string,
  articleId?: string,
  navigateToActivity?: string,
): ReduxAction {
  return async (dispatch: ReduxThunkDispatch, getState: ReduxStateGetter) => {
    const currentUser = storage.getStorageState()?.currentUser;
    const cachedCurrentUser: User | undefined = currentUser?.ytCurrentUser;
    if (cachedCurrentUser) {
      await dispatch(setYTCurrentUser(cachedCurrentUser));
    }
    const cachedPermissions: PermissionCacheItem[] | null = getCachedPermissions();
    if (cachedPermissions) {
      await dispatch(setUserPermissions(cachedPermissions));
    }

    const profiles = cachedCurrentUser?.profiles;
    const userProfileLocale: UserGeneralProfileLocale | undefined = profiles?.general?.locale;
    if (userProfileLocale?.language) {
      loadTranslation(userProfileLocale.locale, userProfileLocale.language);
    }

    await dispatch(migrateToIssuesFilterSearch());
    await createAPIInstance();

    let isRedirected: boolean = false;
    if (cachedPermissions) {
      isRedirected = navigateToRouteById(issueId, articleId, navigateToActivity, !!profiles?.helpdesk?.isReporter);
    }

    let configCurrent = config;
    const currentAppVersion: string | null = storage.getStorageState().currentAppVersion;
    const newVersion = packageJson.version;
    const versionHasChanged: boolean = currentAppVersion != null && newVersion !== currentAppVersion;
    storage.flushStoragePart({currentAppVersion: newVersion});

    try {
      if (versionHasChanged) {
        await _refactoringAndroidResubscribeToPN(); //should be first in a row

        log.info(`App Actions: App upgraded to "${newVersion}"; reloading config`);
        configCurrent = await refreshConfig(config.backendUrl);
      }

      await dispatch(initializeAuth(configCurrent));
      await dispatch(loadCurrentHubUserAndSetAPI());
    } catch (error) {
      log.log('App Actions: App failed to initialize auth. Reloading config...', error);

      try {
        configCurrent = await refreshConfig(config.backendUrl);
      } catch (err) {
        return Router.Home({backendUrl: config.backendUrl, err});
      }

      try {
        await dispatch(initializeAuth(configCurrent));
        await dispatch(loadCurrentHubUserAndSetAPI());
      } catch (e) {
        return Router.LogIn({config, errorMessage: getErrorMessage(e as AnyError)});
      }
    }

    await dispatch(checkUserAgreement());

    if (!getState().app.showUserAgreement) {
      const url = await Linking.getInitialURL();
      await dispatch(
        completeInitialization(
          issueId,
          articleId,
          navigateToActivity,
          extractIssuesQuery(url) ?? undefined,
          isRedirected,
        ),
      );
    }

    dispatch(subscribeToURL());

    if (!versionHasChanged) {
      refreshConfig(configCurrent.backendUrl);
    }

    async function createAPIInstance() {
      if (config) {
        try {
          const auth: OAuth2 = await initAuthInstance(config);
          dispatch(setAuthInstance(auth));
          setApi(new Api(auth));
        } catch (e) {
          redirectToHome(config.backendUrl);
        }
      }
    }

    async function _refactoringAndroidResubscribeToPN() {
      if (
        isAndroidPlatform() &&
        !!getStorageState().deviceToken &&
        currentAppVersion &&
        newVersion.startsWith('2024.3.1')
      ) {
        log.info('REFACTORING:ANDROID:(YTM-21855): re-subscribe to push notifications');
        await pushNotificationsHelper.storeDeviceToken('_');
      }

    }
  };
}