|
| 1 | +import {findFocusedRoute, useFocusEffect} from '@react-navigation/native'; |
| 2 | +import {useCallback} from 'react'; |
| 3 | +import type {ValueOf} from 'type-fest'; |
| 4 | +import NAVIGATION_TABS from '@components/Navigation/NavigationTabBar/NAVIGATION_TABS'; |
| 5 | +import useSubscriptionPlan from '@hooks/useSubscriptionPlan'; |
| 6 | +import {isAnonymousUser} from '@libs/actions/Session'; |
| 7 | +import getIsNarrowLayout from '@libs/getIsNarrowLayout'; |
| 8 | +import {getSettingsTabStateFromSessionStorage} from '@libs/Navigation/helpers/lastVisitedTabPathUtils'; |
| 9 | +import {TAB_TO_FULLSCREEN} from '@libs/Navigation/linkingConfig/RELATIONS'; |
| 10 | +import Navigation from '@libs/Navigation/Navigation'; |
| 11 | +import type {PlatformStackNavigationProp} from '@libs/Navigation/PlatformStackNavigation/types'; |
| 12 | +import type {AuthScreensParamList, FullScreenName, SearchFullscreenNavigatorParamList, SettingsSplitNavigatorParamList, WorkspaceSplitNavigatorParamList} from '@libs/Navigation/types'; |
| 13 | +import {buildCannedSearchQuery, buildSearchQueryJSON, buildSearchQueryString} from '@libs/SearchQueryUtils'; |
| 14 | +import type CONST from '@src/CONST'; |
| 15 | +import NAVIGATORS from '@src/NAVIGATORS'; |
| 16 | +import SCREENS from '@src/SCREENS'; |
| 17 | +import {getPreservedNavigatorState} from './createSplitNavigator/usePreserveNavigatorState'; |
| 18 | + |
| 19 | +function preloadWorkspacesTab(navigation: PlatformStackNavigationProp<AuthScreensParamList>) { |
| 20 | + const lastWorkspacesSplitNavigator = navigation.getState().routes.findLast((route) => route.name === NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR); |
| 21 | + |
| 22 | + if (!lastWorkspacesSplitNavigator) { |
| 23 | + navigation.preload(SCREENS.WORKSPACES_LIST, {}); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + if (!lastWorkspacesSplitNavigator?.state) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const focusedWorkspaceRoute = findFocusedRoute(lastWorkspacesSplitNavigator.state); |
| 32 | + |
| 33 | + if (!focusedWorkspaceRoute || !focusedWorkspaceRoute?.params) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + if (getIsNarrowLayout()) { |
| 38 | + navigation.preload(NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR, { |
| 39 | + screen: SCREENS.WORKSPACE.INITIAL, |
| 40 | + params: focusedWorkspaceRoute.params as WorkspaceSplitNavigatorParamList[typeof SCREENS.WORKSPACE.INITIAL], |
| 41 | + }); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + navigation.preload(NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR, { |
| 46 | + screen: focusedWorkspaceRoute.name, |
| 47 | + params: focusedWorkspaceRoute.params, |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +function preloadReportsTab(navigation: PlatformStackNavigationProp<AuthScreensParamList>) { |
| 52 | + const lastSearchNavigator = navigation.getState().routes.findLast((route) => route.name === NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR); |
| 53 | + const lastSearchNavigatorState = lastSearchNavigator && lastSearchNavigator.key ? getPreservedNavigatorState(lastSearchNavigator?.key) : undefined; |
| 54 | + const lastSearchRoute = lastSearchNavigatorState?.routes.findLast((route) => route.name === SCREENS.SEARCH.ROOT); |
| 55 | + |
| 56 | + if (lastSearchRoute) { |
| 57 | + const {q, ...rest} = lastSearchRoute.params as SearchFullscreenNavigatorParamList[typeof SCREENS.SEARCH.ROOT]; |
| 58 | + const queryJSON = buildSearchQueryJSON(q); |
| 59 | + if (queryJSON) { |
| 60 | + const query = buildSearchQueryString(queryJSON); |
| 61 | + navigation.preload(NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR, {screen: SCREENS.SEARCH.ROOT, params: {q: query, ...rest}}); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + navigation.preload(NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR, {screen: SCREENS.SEARCH.ROOT, params: {q: buildCannedSearchQuery()}}); |
| 66 | +} |
| 67 | + |
| 68 | +function preloadAccountTab(navigation: PlatformStackNavigationProp<AuthScreensParamList>, subscriptionPlan: ValueOf<typeof CONST.POLICY.TYPE> | null) { |
| 69 | + if (!getIsNarrowLayout()) { |
| 70 | + const settingsTabState = getSettingsTabStateFromSessionStorage(); |
| 71 | + |
| 72 | + if (!settingsTabState) { |
| 73 | + navigation.preload(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR, {screen: SCREENS.SETTINGS.PROFILE.ROOT, params: {}}); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + const screenName = findFocusedRoute(settingsTabState)?.name as keyof SettingsSplitNavigatorParamList | undefined; |
| 78 | + |
| 79 | + if ((!subscriptionPlan && screenName === SCREENS.SETTINGS.SUBSCRIPTION.ROOT) || !screenName) { |
| 80 | + navigation.preload(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR, {screen: SCREENS.SETTINGS.PROFILE.ROOT, params: {}}); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + navigation.preload(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR, { |
| 85 | + screen: screenName, |
| 86 | + }); |
| 87 | + return; |
| 88 | + } |
| 89 | + navigation.preload(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR, {screen: SCREENS.SETTINGS.ROOT}); |
| 90 | +} |
| 91 | + |
| 92 | +function preloadInboxTab(navigation: PlatformStackNavigationProp<AuthScreensParamList>) { |
| 93 | + const payload = getIsNarrowLayout() ? {screen: SCREENS.HOME} : {screen: SCREENS.REPORT}; |
| 94 | + navigation.preload(NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, payload); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Hook that preloads all fullscreen navigators except the current one. |
| 99 | + * This helps improve performance by preloading navigators that might be needed soon. |
| 100 | + */ |
| 101 | +function usePreloadFullScreenNavigators(navigation: PlatformStackNavigationProp<AuthScreensParamList>, fullscreenTabName: keyof typeof NAVIGATION_TABS) { |
| 102 | + const state = navigation.getState(); |
| 103 | + const {preloadedRoutes} = state; |
| 104 | + const subscriptionPlan = useSubscriptionPlan(); |
| 105 | + |
| 106 | + useFocusEffect( |
| 107 | + useCallback(() => { |
| 108 | + if (isAnonymousUser()) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + Navigation.setNavigationActionToMicrotaskQueue(() => { |
| 113 | + Object.values(NAVIGATION_TABS) |
| 114 | + .filter((tabName) => { |
| 115 | + const isCurrentTab = tabName === fullscreenTabName; |
| 116 | + const isRouteAlreadyPreloaded = preloadedRoutes.some((preloadedRoute) => TAB_TO_FULLSCREEN[fullscreenTabName].includes(preloadedRoute.name as FullScreenName)); |
| 117 | + return !isCurrentTab && !isRouteAlreadyPreloaded; |
| 118 | + }) |
| 119 | + .forEach((tabName) => { |
| 120 | + switch (tabName) { |
| 121 | + case NAVIGATION_TABS.WORKSPACES: |
| 122 | + preloadWorkspacesTab(navigation); |
| 123 | + return; |
| 124 | + case NAVIGATION_TABS.SEARCH: |
| 125 | + preloadReportsTab(navigation); |
| 126 | + return; |
| 127 | + case NAVIGATION_TABS.SETTINGS: |
| 128 | + preloadAccountTab(navigation, subscriptionPlan); |
| 129 | + return; |
| 130 | + case NAVIGATION_TABS.HOME: |
| 131 | + preloadInboxTab(navigation); |
| 132 | + return; |
| 133 | + default: |
| 134 | + return undefined; |
| 135 | + } |
| 136 | + }); |
| 137 | + }); |
| 138 | + }, [fullscreenTabName, navigation, preloadedRoutes, subscriptionPlan]), |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +export default usePreloadFullScreenNavigators; |
0 commit comments