Skip to content
51 changes: 20 additions & 31 deletions src/libs/Navigation/AppNavigator/AuthScreens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import React, {memo, useEffect, useRef, useState} from 'react';
import {NativeModules, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import Onyx, {withOnyx} from 'react-native-onyx';
import Onyx, {useOnyx} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import ActiveGuidesEventListener from '@components/ActiveGuidesEventListener';
import ComposeProviders from '@components/ComposeProviders';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import OptionsListContextProvider from '@components/OptionListContextProvider';
import {SearchContextProvider} from '@components/Search/SearchContext';
import {useSearchRouterContext} from '@components/Search/SearchRouter/SearchRouterContext';
Expand Down Expand Up @@ -58,6 +59,7 @@
import type * as OnyxTypes from '@src/types/onyx';
import type {SelectedTimezone, Timezone} from '@src/types/onyx/PersonalDetails';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';
import type ReactComponentModule from '@src/types/utils/ReactComponentModule';
import beforeRemoveReportOpenedFromSearchRHP from './beforeRemoveReportOpenedFromSearchRHP';
import CENTRAL_PANE_SCREENS from './CENTRAL_PANE_SCREENS';
Expand All @@ -75,17 +77,6 @@
import WelcomeVideoModalNavigator from './Navigators/WelcomeVideoModalNavigator';
import useRootNavigatorOptions from './useRootNavigatorOptions';

type AuthScreensProps = {
/** Session of currently logged in user */
session: OnyxEntry<OnyxTypes.Session>;

/** The report ID of the last opened public room as anonymous user */
lastOpenedPublicRoomID: OnyxEntry<string>;

/** The last Onyx update ID was applied to the client */
initialLastUpdateIDAppliedToClient: OnyxEntry<number>;
};

const loadReportAttachments = () => require<ReactComponentModule>('../../../pages/home/report/ReportAttachments').default;
const loadValidateLoginPage = () => require<ReactComponentModule>('../../../pages/ValidateLoginPage').default;
const loadLogOutPreviousUserPage = () => require<ReactComponentModule>('../../../pages/LogOutPreviousUserPage').default;
Expand Down Expand Up @@ -147,7 +138,7 @@
return;
}

currentAccountID = value.accountID ?? -1;

Check failure on line 141 in src/libs/Navigation/AppNavigator/AuthScreens.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check


if (Navigation.isActiveRoute(ROUTES.SIGN_IN_MODAL)) {
// This means sign in in RHP was successful, so we can subscribe to user events
Expand Down Expand Up @@ -230,14 +221,19 @@
},
};

function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDAppliedToClient}: AuthScreensProps) {
function AuthScreens() {
const [session, sessionStatus] = useOnyx(ONYXKEYS.SESSION);
const [lastOpenedPublicRoomID, lastOpenedPublicRoomIDStatus] = useOnyx(ONYXKEYS.LAST_OPENED_PUBLIC_ROOM_ID);
const [initialLastUpdateIDAppliedToClient, initialLastUpdateIDAppliedToClientStatus] = useOnyx(ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT);
const areAllOnyxValuesLoadedFirstTime = useRef(true);
const theme = useTheme();
const styles = useThemeStyles();
const {shouldUseNarrowLayout} = useResponsiveLayout();
const rootNavigatorOptions = useRootNavigatorOptions();
const {canUseDefaultRooms} = usePermissions();
const {activeWorkspaceID} = useActiveWorkspace();
const {toggleSearch} = useSearchRouterContext();
const isAnyOnyxValueStillLoading = isLoadingOnyxValue(sessionStatus, lastOpenedPublicRoomIDStatus, initialLastUpdateIDAppliedToClientStatus);

const modal = useRef<OnyxTypes.Modal>({});
const {isOnboardingCompleted} = useOnboardingFlowRouter();
Expand All @@ -249,7 +245,7 @@
}

const initialReport = ReportUtils.findLastAccessedReport(!canUseDefaultRooms, shouldOpenOnAdminRoom(), activeWorkspaceID);
return initialReport?.reportID ?? '';

Check failure on line 248 in src/libs/Navigation/AppNavigator/AuthScreens.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

});

useEffect(() => {
Expand All @@ -261,6 +257,11 @@
}, [theme]);

useEffect(() => {
if (!areAllOnyxValuesLoadedFirstTime.current || !isAnyOnyxValueStillLoading) {
return;
}
areAllOnyxValuesLoadedFirstTime.current = false;

const shortcutsOverviewShortcutConfig = CONST.KEYBOARD_SHORTCUTS.SHORTCUTS;
const searchShortcutConfig = CONST.KEYBOARD_SHORTCUTS.SEARCH;
const chatShortcutConfig = CONST.KEYBOARD_SHORTCUTS.NEW_CHAT;
Expand Down Expand Up @@ -400,7 +401,7 @@

// Rule disabled because this effect is only for component did mount & will component unmount lifecycle event
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);
}, [isAnyOnyxValueStillLoading]);

const CentralPaneScreenOptions: PlatformStackNavigationOptions = {
...hideKeyboardOnSwipe,
Expand All @@ -412,6 +413,10 @@
},
};

if (isAnyOnyxValueStillLoading) {
return <FullScreenLoadingIndicator />;
}

return (
<ComposeProviders components={[OptionsListContextProvider, SearchContextProvider]}>
<View style={styles.rootNavigatorContainerStyles(shouldUseNarrowLayout)}>
Expand Down Expand Up @@ -594,20 +599,4 @@

AuthScreens.displayName = 'AuthScreens';

const AuthScreensMemoized = memo(AuthScreens, () => true);

// Migration to useOnyx cause re-login if logout from deeplinked report in desktop app
// Further analysis required and more details can be seen here:
// https://github.com/Expensify/App/issues/50560
// eslint-disable-next-line
export default withOnyx<AuthScreensProps, AuthScreensProps>({
session: {
key: ONYXKEYS.SESSION,
},
lastOpenedPublicRoomID: {
key: ONYXKEYS.LAST_OPENED_PUBLIC_ROOM_ID,
},
initialLastUpdateIDAppliedToClient: {
key: ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT,
},
})(AuthScreensMemoized);
export default memo(AuthScreens, () => true);
Loading