Skip to content

Add popToSidebar method #61689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
74 changes: 51 additions & 23 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import originalCloseRHPFlow from './helpers/closeRHPFlow';
import getPolicyIDFromState from './helpers/getPolicyIDFromState';
import getStateFromPath from './helpers/getStateFromPath';
import getTopmostReportParams from './helpers/getTopmostReportParams';
import {isFullScreenName, isOnboardingFlowName} from './helpers/isNavigatorName';
import {isFullScreenName, isOnboardingFlowName, isSplitNavigatorName} from './helpers/isNavigatorName';
import isReportOpenInRHP from './helpers/isReportOpenInRHP';
import isSideModalNavigator from './helpers/isSideModalNavigator';
import linkTo from './helpers/linkTo';
Expand All @@ -34,6 +34,7 @@ import replaceWithSplitNavigator from './helpers/replaceWithSplitNavigator';
import setNavigationActionToMicrotaskQueue from './helpers/setNavigationActionToMicrotaskQueue';
import switchPolicyID from './helpers/switchPolicyID';
import {linkingConfig} from './linkingConfig';
import {SPLIT_TO_SIDEBAR} from './linkingConfig/RELATIONS';
import navigationRef from './navigationRef';
import type {NavigationPartialRoute, NavigationRoute, NavigationStateRoute, RootNavigatorParamList, State} from './types';

Expand Down Expand Up @@ -73,13 +74,21 @@ const navigationIsReadyPromise = new Promise<void>((resolve) => {

let pendingRoute: Route | null = null;

let shouldPopAllStateOnUP = false;
let shouldPopToSidebar = false;

/**
* Inform the navigation that next time user presses UP we should pop all the state back to LHN.
*/
function setShouldPopAllStateOnUP(shouldPopAllStateFlag: boolean) {
shouldPopAllStateOnUP = shouldPopAllStateFlag;
function setShouldPopToSidebar(shouldPopAllStateFlag: boolean) {
shouldPopToSidebar = shouldPopAllStateFlag;
}

/**
* Returns shouldPopToSidebar variable used to determine whether should we pop all state back to LHN
* @returns shouldPopToSidebar
*/
function getShouldPopToSidebar() {
return shouldPopToSidebar;
}

type CanNavigateParams = {
Expand Down Expand Up @@ -265,17 +274,10 @@ type GoBackOptions = {
* In that case we want to goUp to a country picker with any params so we don't compare them.
*/
compareParams?: boolean;

/**
* Specifies whether goBack should pop to top when invoked.
* Additionaly, to execute popToTop, set the value of the global variable ShouldPopAllStateOnUP to true using the setShouldPopAllStateOnUP function.
*/
shouldPopToTop?: boolean;
};

const defaultGoBackOptions: Required<GoBackOptions> = {
compareParams: true,
shouldPopToTop: false,
};

/**
Expand Down Expand Up @@ -344,14 +346,6 @@ function goBack(backToRoute?: Route, options?: GoBackOptions) {
return;
}

if (options?.shouldPopToTop) {
if (shouldPopAllStateOnUP) {
shouldPopAllStateOnUP = false;
navigationRef.current?.dispatch(StackActions.popToTop());
return;
}
}

if (backToRoute) {
goUp(backToRoute, options);
return;
Expand All @@ -365,6 +359,38 @@ function goBack(backToRoute?: Route, options?: GoBackOptions) {
navigationRef.current?.goBack();
}

function popToSidebar() {
setShouldPopToSidebar(false);

const rootState = navigationRef.current?.getRootState();
const currentRoute = rootState?.routes.at(-1);

if (!currentRoute) {
Log.hmmm('[popToSidebar] Unable to pop to sidebar, no current root found in navigator');
return;
}

if (!isSplitNavigatorName(currentRoute?.name)) {
Log.hmmm('[popToSidebar] must be invoked only from SplitNavigator');
return;
}

const topRoute = currentRoute.state?.routes.at(0);
const lastRoute = currentRoute.state?.routes.at(-1);

const currentRouteName = currentRoute?.name as keyof typeof SPLIT_TO_SIDEBAR;
if (topRoute?.name !== SPLIT_TO_SIDEBAR[currentRouteName]) {
const params = currentRoute.name === NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR ? {...lastRoute?.params} : undefined;

const sidebarName = SPLIT_TO_SIDEBAR[currentRouteName];

navigationRef.dispatch({payload: {name: sidebarName, params}, type: CONST.NAVIGATION.ACTION_TYPE.REPLACE});
return;
}

navigationRef.current?.dispatch(StackActions.popToTop());
}

/**
* Reset the navigation state to Home page.
*/
Expand Down Expand Up @@ -616,15 +642,15 @@ const dismissModalWithReport = (navigateToReportPayload: NavigateToReportWithPol
};

/**
* Returns to the first screen in the stack, dismissing all the others, only if the global variable shouldPopAllStateOnUP is set to true.
* Returns to the first screen in the stack, dismissing all the others, only if the global variable shouldPopToSidebar is set to true.
*/
function popToTop() {
if (!shouldPopAllStateOnUP) {
if (!shouldPopToSidebar) {
goBack();
return;
}

shouldPopAllStateOnUP = false;
shouldPopToSidebar = false;
navigationRef.current?.dispatch(StackActions.popToTop());
}

Expand Down Expand Up @@ -671,7 +697,9 @@ function isOnboardingFlow() {
}

export default {
setShouldPopAllStateOnUP,
setShouldPopToSidebar,
getShouldPopToSidebar,
popToSidebar,
navigate,
setParams,
dismissModal,
Expand Down
4 changes: 2 additions & 2 deletions src/libs/Navigation/NavigationRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ function NavigationRoot({authenticated, lastVisitedPath, initialUrl, onReady}: N
return;
}

// After resizing the screen from wide to narrow, if we have visited multiple central screens, we want to go back to the LHN screen, so we set shouldPopAllStateOnUP to true.
// After resizing the screen from wide to narrow, if we have visited multiple central screens, we want to go back to the LHN screen, so we set shouldPopToSidebar to true.
// Now when this value is true, Navigation.goBack with the option {shouldPopToTop: true} will remove all visited central screens in the given tab from the navigation stack and go back to the LHN.
// More context here: https://github.com/Expensify/App/pull/59300
if (!shouldUseNarrowLayout) {
return;
}

Navigation.setShouldPopAllStateOnUP(true);
Navigation.setShouldPopToSidebar(true);
}, [shouldUseNarrowLayout]);

useEffect(() => {
Expand Down
28 changes: 0 additions & 28 deletions src/libs/Navigation/helpers/goBackFromWorkspaceCentralScreen.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2906,8 +2906,7 @@ function deleteReport(reportID: string | undefined, shouldDeleteChildReports = f
function navigateToConciergeChatAndDeleteReport(reportID: string | undefined, shouldPopToTop = false, shouldDeleteChildReports = false) {
// Dismiss the current report screen and replace it with Concierge Chat
if (shouldPopToTop) {
Navigation.setShouldPopAllStateOnUP(true);
Navigation.goBack(undefined, {shouldPopToTop: true});
Navigation.popToSidebar();
} else {
Navigation.goBack();
}
Expand Down
6 changes: 5 additions & 1 deletion src/libs/navigateAfterJoinRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import ROUTES from '@src/ROUTES';
import Navigation from './Navigation/Navigation';

const navigateAfterJoinRequest = () => {
Navigation.goBack(undefined, {shouldPopToTop: true});
if (Navigation.getShouldPopToSidebar()) {
Navigation.popToSidebar();
} else {
Navigation.goBack();
}
Navigation.setNavigationActionToMicrotaskQueue(() => {
Navigation.navigate(ROUTES.SETTINGS_WORKSPACES.route);
});
Expand Down
2 changes: 1 addition & 1 deletion src/pages/TeachersUnite/SaveTheWorldPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function SaveTheWorldPage() {
title={translate('sidebarScreen.saveTheWorld')}
shouldShowBackButton={shouldUseNarrowLayout}
shouldDisplaySearchRouter
onBackButtonPress={() => Navigation.goBack(undefined, {shouldPopToTop: true})}
onBackButtonPress={Navigation.popToSidebar}
icon={Illustrations.TeachersUnite}
shouldUseHeadlineHeader
/>
Expand Down
11 changes: 7 additions & 4 deletions src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,15 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
Navigation.dismissModal();
return;
}
if (Navigation.getShouldPopToSidebar()) {
Navigation.popToSidebar();
return;
}
if (backTo) {
Navigation.goBack(backTo as Route, {shouldPopToTop: true});
Navigation.goBack(backTo as Route);
return;
}
Navigation.goBack(undefined, {shouldPopToTop: true});
Navigation.goBack();
}, [isInNarrowPaneModal, backTo]);

let headerView = (
Expand Down Expand Up @@ -589,9 +593,8 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
}
Navigation.dismissModal();
if (Navigation.getTopmostReportId() === prevOnyxReportID) {
Navigation.setShouldPopAllStateOnUP(true);
Navigation.isNavigationReady().then(() => {
Navigation.goBack(undefined, {shouldPopToTop: true});
Navigation.popToSidebar();
});
}
if (prevReport?.parentReportID) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/AboutPage/AboutPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function AboutPage() {
title={translate('initialSettingsPage.about')}
shouldShowBackButton={shouldUseNarrowLayout}
shouldDisplaySearchRouter
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS, {shouldPopToTop: true})}
onBackButtonPress={Navigation.popToSidebar}
icon={Illustrations.PalmTree}
shouldUseHeadlineHeader
/>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/Preferences/PreferencesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function PreferencesPage() {
shouldUseHeadlineHeader
shouldShowBackButton={shouldUseNarrowLayout}
shouldDisplaySearchRouter
onBackButtonPress={() => Navigation.goBack(undefined, {shouldPopToTop: true})}
onBackButtonPress={Navigation.popToSidebar}
/>
<ScrollView contentContainerStyle={styles.pt3}>
<View style={[styles.flex1, shouldUseNarrowLayout ? styles.workspaceSectionMobile : styles.workspaceSection]}>
Expand Down
8 changes: 7 additions & 1 deletion src/pages/settings/Profile/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ function ProfilePage() {
>
<HeaderWithBackButton
title={translate('common.profile')}
onBackButtonPress={() => Navigation.goBack(route.params?.backTo, {shouldPopToTop: true})}
onBackButtonPress={() => {
if (Navigation.getShouldPopToSidebar()) {
Navigation.popToSidebar();
return;
}
Navigation.goBack(route.params?.backTo);
}}
shouldShowBackButton={shouldUseNarrowLayout}
shouldDisplaySearchRouter
icon={Illustrations.Profile}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/settings/Security/SecuritySettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function SecuritySettingsPage() {
const personalDetails = usePersonalDetails();
const {canUseMergeAccounts} = usePermissions();

const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: true});
const delegateButtonRef = useRef<HTMLDivElement | null>(null);

const [shouldShowDelegatePopoverMenu, setShouldShowDelegatePopoverMenu] = useState(false);
Expand Down Expand Up @@ -265,7 +265,7 @@ function SecuritySettingsPage() {
<HeaderWithBackButton
title={translate('initialSettingsPage.security')}
shouldShowBackButton={shouldUseNarrowLayout}
onBackButtonPress={() => Navigation.goBack(undefined, {shouldPopToTop: true})}
onBackButtonPress={Navigation.popToSidebar}
icon={Illustrations.LockClosed}
shouldUseHeadlineHeader
shouldDisplaySearchRouter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ function SubscriptionSettingsPage({route}: SubscriptionSettingsPageProps) {
>
<HeaderWithBackButton
title={translate('workspace.common.subscription')}
onBackButtonPress={() => Navigation.goBack(backTo, {shouldPopToTop: true})}
onBackButtonPress={() => {
if (Navigation.getShouldPopToSidebar()) {
Navigation.popToSidebar();
return;
}
Navigation.goBack(backTo);
}}
shouldShowBackButton={shouldUseNarrowLayout}
shouldDisplaySearchRouter
icon={Illustrations.CreditCardsNew}
Expand Down
6 changes: 3 additions & 3 deletions src/pages/settings/Troubleshoot/TroubleshootPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ function TroubleshootPage() {
const {shouldUseNarrowLayout} = useResponsiveLayout();
const illustrationStyle = getLightbulbIllustrationStyle();
const [isLoading, setIsLoading] = useState(false);
const [shouldStoreLogs] = useOnyx(ONYXKEYS.SHOULD_STORE_LOGS);
const [shouldMaskOnyxState = true] = useOnyx(ONYXKEYS.SHOULD_MASK_ONYX_STATE);
const [shouldStoreLogs] = useOnyx(ONYXKEYS.SHOULD_STORE_LOGS, {canBeMissing: true});
const [shouldMaskOnyxState = true] = useOnyx(ONYXKEYS.SHOULD_MASK_ONYX_STATE, {canBeMissing: true});
const {resetOptions} = useOptionsList({shouldInitialize: false});

const exportOnyxState = useCallback(() => {
Expand Down Expand Up @@ -108,7 +108,7 @@ function TroubleshootPage() {
title={translate('initialSettingsPage.aboutPage.troubleshoot')}
shouldShowBackButton={shouldUseNarrowLayout}
shouldDisplaySearchRouter
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS, {shouldPopToTop: true})}
onBackButtonPress={Navigation.popToSidebar}
icon={Illustrations.Lightbulb}
shouldUseHeadlineHeader
/>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/Wallet/WalletPage/WalletPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ function WalletPage({shouldListenForResize = false}: WalletPageProps) {
shouldUseHeadlineHeader
shouldShowBackButton={shouldUseNarrowLayout}
shouldDisplaySearchRouter
onBackButtonPress={() => Navigation.goBack(undefined, {shouldPopToTop: true})}
onBackButtonPress={Navigation.popToSidebar}
/>
);

Expand Down
8 changes: 6 additions & 2 deletions src/pages/workspace/WorkspaceJoinUserPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type WorkspaceJoinUserPageProps = WorkspaceJoinUserPageRoute;
function WorkspaceJoinUserPage({route}: WorkspaceJoinUserPageProps) {
const styles = useThemeStyles();
const policyID = route?.params?.policyID;
const [policy, policyResult] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`);
const [policy, policyResult] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {canBeMissing: true});
const isPolicyLoading = isLoadingOnyxValue(policyResult);
const inviterEmail = route?.params?.email;
const isUnmounted = useRef(false);
Expand All @@ -32,7 +32,11 @@ function WorkspaceJoinUserPage({route}: WorkspaceJoinUserPageProps) {
}
if (!isEmptyObject(policy) && !policy?.isJoinRequestPending && !isPendingDeletePolicy(policy)) {
Navigation.isNavigationReady().then(() => {
Navigation.goBack(undefined, {shouldPopToTop: true});
if (Navigation.getShouldPopToSidebar()) {
Navigation.popToSidebar();
} else {
Navigation.goBack();
}
Navigation.navigate(ROUTES.WORKSPACE_INITIAL.getRoute(policyID));
});
return;
Expand Down
3 changes: 1 addition & 2 deletions src/pages/workspace/WorkspaceMembersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import {removeApprovalWorkflow as removeApprovalWorkflowAction, updateApprovalWo
import {canUseTouchScreen} from '@libs/DeviceCapabilities';
import {formatPhoneNumber as formatPhoneNumberUtil} from '@libs/LocalePhoneNumber';
import Log from '@libs/Log';
import goBackFromWorkspaceCentralScreen from '@libs/Navigation/helpers/goBackFromWorkspaceCentralScreen';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {WorkspaceSplitNavigatorParamList} from '@libs/Navigation/types';
Expand Down Expand Up @@ -724,7 +723,7 @@ function WorkspaceMembersPage({personalDetails, route, policy, currentUserPerson
turnOffMobileSelectionMode();
return;
}
goBackFromWorkspaceCentralScreen(policyID);
Navigation.popToSidebar();
}}
>
{() => (
Expand Down
Loading
Loading