Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/components/Navigation/NavigationTabBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {findFocusedRoute, useNavigationState} from '@react-navigation/native';
import {findFocusedRoute, StackActions, useNavigationState} from '@react-navigation/native';
import React, {memo, useCallback, useEffect, useState} from 'react';
import {View} from 'react-native';
import type {ValueOf} from 'type-fest';
Expand All @@ -18,13 +18,16 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useSearchTypeMenuSections from '@hooks/useSearchTypeMenuSections';
import {useSidebarOrderedReports} from '@hooks/useSidebarOrderedReports';
import useStyleUtils from '@hooks/useStyleUtils';
import useSubscriptionPlan from '@hooks/useSubscriptionPlan';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWorkspacesTabIndicatorStatus from '@hooks/useWorkspacesTabIndicatorStatus';
import clearSelectedText from '@libs/clearSelectedText/clearSelectedText';
import getPlatform from '@libs/getPlatform';
import interceptAnonymousUser from '@libs/interceptAnonymousUser';
import {getPreservedNavigatorState} from '@libs/Navigation/AppNavigator/createSplitNavigator/usePreserveNavigatorState';
import getAccountTabScreenToOpen from '@libs/Navigation/helpers/getAccountTabScreenToOpen';
import isRoutePreloaded from '@libs/Navigation/helpers/isRoutePreloaded';
import navigateToWorkspacesPage, {getWorkspaceNavigationRouteState} from '@libs/Navigation/helpers/navigateToWorkspacesPage';
import Navigation from '@libs/Navigation/Navigation';
import {buildCannedSearchQuery, buildSearchQueryJSON, buildSearchQueryString} from '@libs/SearchQueryUtils';
Expand Down Expand Up @@ -62,6 +65,7 @@ function NavigationTabBar({selectedTab, isTooltipAllowed = false, isTopLevelBar
const [workspacesTabState, setWorkspacesTabState] = useState(initialNavigationRouteState.workspacesTabState);
const params = workspacesTabState?.routes?.at(0)?.params as WorkspaceSplitNavigatorParamList[typeof SCREENS.WORKSPACE.INITIAL];
const {typeMenuSections} = useSearchTypeMenuSections();
const subscriptionPlan = useSubscriptionPlan();

const [lastViewedPolicy] = useOnyx(
ONYXKEYS.COLLECTION.POLICY,
Expand Down Expand Up @@ -154,10 +158,15 @@ function NavigationTabBar({selectedTab, isTooltipAllowed = false, isTopLevelBar
return;
}
interceptAnonymousUser(() => {
// We use dispatch here because the correct screens and params are preloaded and set up in usePreloadFullScreenNavigators.
navigationRef.dispatch({type: CONST.NAVIGATION.ACTION_TYPE.PUSH, payload: {name: NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR}});
if (isRoutePreloaded(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR)) {
// We use dispatch here because the correct screens and params are preloaded and set up in usePreloadFullScreenNavigators.
navigationRef.dispatch({type: CONST.NAVIGATION.ACTION_TYPE.PUSH, payload: {name: NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR}});
return;
}
const accountTabPayload = getAccountTabScreenToOpen(subscriptionPlan);
navigationRef.dispatch(StackActions.push(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR, accountTabPayload));
});
}, [selectedTab]);
}, [selectedTab, subscriptionPlan]);

/**
* The settings tab is related to SettingsSplitNavigator and WorkspaceSplitNavigator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import syncBrowserHistory from './syncBrowserHistory';
import type {
DismissModalActionType,
OpenWorkspaceSplitActionType,
PreloadActionType,
PushActionType,
ReplaceActionType,
RootStackNavigatorAction,
Expand Down Expand Up @@ -46,6 +47,10 @@ function isToggleSidePanelWithHistoryAction(action: RootStackNavigatorAction): a
return action.type === CONST.NAVIGATION.ACTION_TYPE.TOGGLE_SIDE_PANEL_WITH_HISTORY;
}

function isPreloadAction(action: RootStackNavigatorAction): action is PreloadActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.PRELOAD;
}

function shouldPreventReset(state: StackNavigationState<ParamListBase>, action: CommonActions.Action | StackActionType) {
if (action.type !== CONST.NAVIGATION_ACTIONS.RESET || !action?.payload) {
return false;
Expand Down Expand Up @@ -80,6 +85,10 @@ function RootStackRouter(options: RootStackNavigatorRouterOptions) {
return {
...stackRouter,
getStateForAction(state: StackNavigationState<ParamListBase>, action: RootStackNavigatorAction, configOptions: RouterConfigOptions) {
if (isPreloadAction(action) && action.payload.name === state.routes.at(-1)?.name) {
return state;
}

if (isToggleSidePanelWithHistoryAction(action)) {
return handleToggleSidePanelWithHistoryAction(state, action);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ type RootStackNavigatorActionType =
policyID: string;
screenName: WorkspaceScreenName;
};
}
| {
type: typeof CONST.NAVIGATION.ACTION_TYPE.PRELOAD;
payload: {
name: string;
params: {
screen: string;
params: Record<string, unknown>;
};
};
};

type OpenWorkspaceSplitActionType = RootStackNavigatorActionType & {
Expand All @@ -28,6 +38,8 @@ type ToggleSidePanelWithHistoryActionType = RootStackNavigatorActionType & {
type: typeof CONST.NAVIGATION.ACTION_TYPE.TOGGLE_SIDE_PANEL_WITH_HISTORY;
};

type PreloadActionType = RootStackNavigatorAction & {type: typeof CONST.NAVIGATION.ACTION_TYPE.PRELOAD};

type PushActionType = StackActionType & {type: typeof CONST.NAVIGATION.ACTION_TYPE.PUSH};

type ReplaceActionType = StackActionType & {type: typeof CONST.NAVIGATION.ACTION_TYPE.REPLACE};
Expand All @@ -45,6 +57,7 @@ export type {
PushActionType,
ReplaceActionType,
DismissModalActionType,
PreloadActionType,
RootStackNavigatorAction,
RootStackNavigatorRouterOptions,
ToggleSidePanelWithHistoryActionType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type {NavigationRoute} from '@react-navigation/native';
import {findFocusedRoute, useFocusEffect, useNavigation, useRoute} from '@react-navigation/native';
import {useFocusEffect, useNavigation, useRoute} from '@react-navigation/native';
import {useCallback, useMemo, useRef} from 'react';
import type {ValueOf} from 'type-fest';
import NAVIGATION_TABS from '@components/Navigation/NavigationTabBar/NAVIGATION_TABS';
import useIsAuthenticated from '@hooks/useIsAuthenticated';
import useOnyx from '@hooks/useOnyx';
import useSubscriptionPlan from '@hooks/useSubscriptionPlan';
import {isAnonymousUser} from '@libs/actions/Session';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import {getSettingsTabStateFromSessionStorage, getWorkspacesTabStateFromSessionStorage} from '@libs/Navigation/helpers/lastVisitedTabPathUtils';
import getAccountTabScreenToOpen from '@libs/Navigation/helpers/getAccountTabScreenToOpen';
import {getWorkspacesTabStateFromSessionStorage} from '@libs/Navigation/helpers/lastVisitedTabPathUtils';
import {TAB_TO_FULLSCREEN} from '@libs/Navigation/linkingConfig/RELATIONS';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackNavigationProp} from '@libs/Navigation/PlatformStackNavigation/types';
import type {AuthScreensParamList, FullScreenName, SearchFullscreenNavigatorParamList, SettingsSplitNavigatorParamList} from '@libs/Navigation/types';
import type {AuthScreensParamList, FullScreenName, SearchFullscreenNavigatorParamList} from '@libs/Navigation/types';
import {buildCannedSearchQuery, buildSearchQueryJSON, buildSearchQueryString} from '@libs/SearchQueryUtils';
import type CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
Expand Down Expand Up @@ -55,27 +55,8 @@ function preloadReportsTab(navigation: PlatformStackNavigationProp<AuthScreensPa
}

function preloadAccountTab(navigation: PlatformStackNavigationProp<AuthScreensParamList>, subscriptionPlan: ValueOf<typeof CONST.POLICY.TYPE> | null) {
if (!getIsNarrowLayout()) {
const settingsTabState = getSettingsTabStateFromSessionStorage();

if (!settingsTabState) {
navigation.preload(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR, {screen: SCREENS.SETTINGS.PROFILE.ROOT, params: {}});
return;
}

const screenName = findFocusedRoute(settingsTabState)?.name as keyof SettingsSplitNavigatorParamList | undefined;

if ((!subscriptionPlan && screenName === SCREENS.SETTINGS.SUBSCRIPTION.ROOT) || !screenName) {
navigation.preload(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR, {screen: SCREENS.SETTINGS.PROFILE.ROOT, params: {}});
return;
}

navigation.preload(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR, {
screen: screenName,
});
return;
}
navigation.preload(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR, {screen: SCREENS.SETTINGS.ROOT});
const accountTabPayload = getAccountTabScreenToOpen(subscriptionPlan);
navigation.preload(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR, accountTabPayload);
}

function preloadInboxTab(navigation: PlatformStackNavigationProp<AuthScreensParamList>) {
Expand Down
29 changes: 29 additions & 0 deletions src/libs/Navigation/helpers/getAccountTabScreenToOpen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {findFocusedRoute} from '@react-navigation/native';
import type {NavigatorScreenParams} from '@react-navigation/native';
import type {ValueOf} from 'type-fest';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import type {NavigationPartialRoute, SettingsSplitNavigatorParamList} from '@libs/Navigation/types';
import type CONST from '@src/CONST';
import SCREENS from '@src/SCREENS';
import {getSettingsTabStateFromSessionStorage} from './lastVisitedTabPathUtils';

/**
* Returns the Settings screen that should be opened on the Account tab.
*/
export default function getAccountTabScreenToOpen(subscriptionPlan: ValueOf<typeof CONST.POLICY.TYPE> | null): NavigatorScreenParams<SettingsSplitNavigatorParamList> {
if (getIsNarrowLayout()) {
return {screen: SCREENS.SETTINGS.ROOT};
}

const settingsTabState = getSettingsTabStateFromSessionStorage();
if (!settingsTabState) {
return {screen: SCREENS.SETTINGS.PROFILE.ROOT, params: {}};
}

const focusedRoute = findFocusedRoute(settingsTabState) as NavigationPartialRoute<keyof SettingsSplitNavigatorParamList> | undefined;
if ((!subscriptionPlan && focusedRoute?.name === SCREENS.SETTINGS.SUBSCRIPTION.ROOT) || !focusedRoute) {
return {screen: SCREENS.SETTINGS.PROFILE.ROOT, params: {}};
}

return {screen: focusedRoute.name};
}
8 changes: 8 additions & 0 deletions src/libs/Navigation/helpers/isRoutePreloaded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type {NavigationState} from '@react-navigation/native';
import navigationRef from '@libs/Navigation/navigationRef';
import type {AuthScreensParamList, NavigationPartialRoute} from '@libs/Navigation/types';

export default function isRoutePreloaded(routeName: keyof AuthScreensParamList) {
const rootState = navigationRef.getRootState() as NavigationState<AuthScreensParamList> & {preloadedRoutes: Array<NavigationPartialRoute<keyof AuthScreensParamList>>};
return rootState.preloadedRoutes.some((preloadedRoute) => preloadedRoute.name === routeName);
}