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 16 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
75 changes: 52 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,22 @@ 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.
* TODO change content of comment
*/
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 +275,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 +347,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 +360,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("There's no current root 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 +643,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 +698,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 @@ -2899,8 +2899,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(); // it can break because of redirection to settings main
} else {
Navigation.goBack();
}
Expand Down
7 changes: 6 additions & 1 deletion src/libs/navigateAfterJoinRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import ROUTES from '@src/ROUTES';
import Navigation from './Navigation/Navigation';

const navigateAfterJoinRequest = () => {
Navigation.goBack(undefined, {shouldPopToTop: true});
// 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
15 changes: 11 additions & 4 deletions src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,18 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
return;
}
if (backTo) {
Navigation.goBack(backTo as Route, {shouldPopToTop: true});
if (Navigation.getShouldPopToSidebar()) {
Navigation.popToSidebar();
return;
}
Navigation.goBack(backTo as Route);
return;
}
if (Navigation.getShouldPopToSidebar()) {
Navigation.popToSidebar();
return;
}
Navigation.goBack(undefined, {shouldPopToTop: true});
Navigation.goBack();
}, [isInNarrowPaneModal, backTo]);

let headerView = (
Expand Down Expand Up @@ -607,9 +615,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
16 changes: 11 additions & 5 deletions src/pages/settings/Profile/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ function ProfilePage() {
const {shouldUseNarrowLayout} = useResponsiveLayout();
const {safeAreaPaddingBottomStyle} = useSafeAreaPaddings();
const scrollEnabled = useScrollEnabled();
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST);
const [privatePersonalDetails] = useOnyx(ONYXKEYS.PRIVATE_PERSONAL_DETAILS);
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST, {canBeMissing: true});
const [privatePersonalDetails] = useOnyx(ONYXKEYS.PRIVATE_PERSONAL_DETAILS, {canBeMissing: true});
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const route = useRoute<PlatformStackRouteProp<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.PROFILE.ROOT>>();
const [isLoadingApp] = useOnyx(ONYXKEYS.IS_LOADING_APP);
const [isLoadingApp] = useOnyx(ONYXKEYS.IS_LOADING_APP, {canBeMissing: true});

const getPronouns = (): string => {
const pronounsKey = currentUserPersonalDetails?.pronouns?.replace(CONST.PRONOUNS.PREFIX, '') ?? '';
Expand All @@ -64,7 +64,7 @@ function ProfilePage() {
const privateDetails = privatePersonalDetails ?? {};
const legalName = `${privateDetails.legalFirstName ?? ''} ${privateDetails.legalLastName ?? ''}`.trim();

const [isActingAsDelegate] = useOnyx(ONYXKEYS.ACCOUNT, {selector: (account) => !!account?.delegatedAccess?.delegate});
const [isActingAsDelegate] = useOnyx(ONYXKEYS.ACCOUNT, {selector: (account) => !!account?.delegatedAccess?.delegate, canBeMissing: true});
const [isNoDelegateAccessMenuVisible, setIsNoDelegateAccessMenuVisible] = useState(false);

const publicOptions = [
Expand Down 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
10 changes: 8 additions & 2 deletions src/pages/settings/Subscription/SubscriptionSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function SubscriptionSettingsPage({route}: SubscriptionSettingsPageProps) {
useEffect(() => {
openSubscriptionPage();
}, []);
const [isAppLoading] = useOnyx(ONYXKEYS.IS_LOADING_APP);
const [isAppLoading] = useOnyx(ONYXKEYS.IS_LOADING_APP, {canBeMissing: true});

if (!subscriptionPlan && isAppLoading) {
return <FullScreenLoadingIndicator />;
Expand All @@ -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
Loading
Loading