Skip to content

Commit 84d940b

Browse files
authored
Merge pull request #61689 from software-mansion-labs/borys3kk-refactor-shouldpoptotop-and-goback
Add popToSidebar method
2 parents 731370b + 54313fd commit 84d940b

29 files changed

+276
-96
lines changed

src/libs/Navigation/Navigation.ts

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import originalCloseRHPFlow from './helpers/closeRHPFlow';
2424
import getPolicyIDFromState from './helpers/getPolicyIDFromState';
2525
import getStateFromPath from './helpers/getStateFromPath';
2626
import getTopmostReportParams from './helpers/getTopmostReportParams';
27-
import {isFullScreenName, isOnboardingFlowName} from './helpers/isNavigatorName';
27+
import {isFullScreenName, isOnboardingFlowName, isSplitNavigatorName} from './helpers/isNavigatorName';
2828
import isReportOpenInRHP from './helpers/isReportOpenInRHP';
2929
import isSideModalNavigator from './helpers/isSideModalNavigator';
3030
import linkTo from './helpers/linkTo';
@@ -34,6 +34,7 @@ import replaceWithSplitNavigator from './helpers/replaceWithSplitNavigator';
3434
import setNavigationActionToMicrotaskQueue from './helpers/setNavigationActionToMicrotaskQueue';
3535
import switchPolicyID from './helpers/switchPolicyID';
3636
import {linkingConfig} from './linkingConfig';
37+
import {SPLIT_TO_SIDEBAR} from './linkingConfig/RELATIONS';
3738
import navigationRef from './navigationRef';
3839
import type {NavigationPartialRoute, NavigationRoute, NavigationStateRoute, RootNavigatorParamList, State} from './types';
3940

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

7475
let pendingRoute: Route | null = null;
7576

76-
let shouldPopAllStateOnUP = false;
77+
let shouldPopToSidebar = false;
7778

7879
/**
7980
* Inform the navigation that next time user presses UP we should pop all the state back to LHN.
8081
*/
81-
function setShouldPopAllStateOnUP(shouldPopAllStateFlag: boolean) {
82-
shouldPopAllStateOnUP = shouldPopAllStateFlag;
82+
function setShouldPopToSidebar(shouldPopAllStateFlag: boolean) {
83+
shouldPopToSidebar = shouldPopAllStateFlag;
84+
}
85+
86+
/**
87+
* Returns shouldPopToSidebar variable used to determine whether should we pop all state back to LHN
88+
* @returns shouldPopToSidebar
89+
*/
90+
function getShouldPopToSidebar() {
91+
return shouldPopToSidebar;
8392
}
8493

8594
type CanNavigateParams = {
@@ -265,17 +274,10 @@ type GoBackOptions = {
265274
* In that case we want to goUp to a country picker with any params so we don't compare them.
266275
*/
267276
compareParams?: boolean;
268-
269-
/**
270-
* Specifies whether goBack should pop to top when invoked.
271-
* Additionaly, to execute popToTop, set the value of the global variable ShouldPopAllStateOnUP to true using the setShouldPopAllStateOnUP function.
272-
*/
273-
shouldPopToTop?: boolean;
274277
};
275278

276279
const defaultGoBackOptions: Required<GoBackOptions> = {
277280
compareParams: true,
278-
shouldPopToTop: false,
279281
};
280282

281283
/**
@@ -344,14 +346,6 @@ function goBack(backToRoute?: Route, options?: GoBackOptions) {
344346
return;
345347
}
346348

347-
if (options?.shouldPopToTop) {
348-
if (shouldPopAllStateOnUP) {
349-
shouldPopAllStateOnUP = false;
350-
navigationRef.current?.dispatch(StackActions.popToTop());
351-
return;
352-
}
353-
}
354-
355349
if (backToRoute) {
356350
goUp(backToRoute, options);
357351
return;
@@ -365,6 +359,38 @@ function goBack(backToRoute?: Route, options?: GoBackOptions) {
365359
navigationRef.current?.goBack();
366360
}
367361

362+
function popToSidebar() {
363+
setShouldPopToSidebar(false);
364+
365+
const rootState = navigationRef.current?.getRootState();
366+
const currentRoute = rootState?.routes.at(-1);
367+
368+
if (!currentRoute) {
369+
Log.hmmm('[popToSidebar] Unable to pop to sidebar, no current root found in navigator');
370+
return;
371+
}
372+
373+
if (!isSplitNavigatorName(currentRoute?.name)) {
374+
Log.hmmm('[popToSidebar] must be invoked only from SplitNavigator');
375+
return;
376+
}
377+
378+
const topRoute = currentRoute.state?.routes.at(0);
379+
const lastRoute = currentRoute.state?.routes.at(-1);
380+
381+
const currentRouteName = currentRoute?.name as keyof typeof SPLIT_TO_SIDEBAR;
382+
if (topRoute?.name !== SPLIT_TO_SIDEBAR[currentRouteName]) {
383+
const params = currentRoute.name === NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR ? {...lastRoute?.params} : undefined;
384+
385+
const sidebarName = SPLIT_TO_SIDEBAR[currentRouteName];
386+
387+
navigationRef.dispatch({payload: {name: sidebarName, params}, type: CONST.NAVIGATION.ACTION_TYPE.REPLACE});
388+
return;
389+
}
390+
391+
navigationRef.current?.dispatch(StackActions.popToTop());
392+
}
393+
368394
/**
369395
* Reset the navigation state to Home page.
370396
*/
@@ -616,15 +642,15 @@ const dismissModalWithReport = (navigateToReportPayload: NavigateToReportWithPol
616642
};
617643

618644
/**
619-
* Returns to the first screen in the stack, dismissing all the others, only if the global variable shouldPopAllStateOnUP is set to true.
645+
* Returns to the first screen in the stack, dismissing all the others, only if the global variable shouldPopToSidebar is set to true.
620646
*/
621647
function popToTop() {
622-
if (!shouldPopAllStateOnUP) {
648+
if (!shouldPopToSidebar) {
623649
goBack();
624650
return;
625651
}
626652

627-
shouldPopAllStateOnUP = false;
653+
shouldPopToSidebar = false;
628654
navigationRef.current?.dispatch(StackActions.popToTop());
629655
}
630656

@@ -671,7 +697,9 @@ function isOnboardingFlow() {
671697
}
672698

673699
export default {
674-
setShouldPopAllStateOnUP,
700+
setShouldPopToSidebar,
701+
getShouldPopToSidebar,
702+
popToSidebar,
675703
navigate,
676704
setParams,
677705
dismissModal,

src/libs/Navigation/NavigationRoot.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ function NavigationRoot({authenticated, lastVisitedPath, initialUrl, onReady}: N
169169
return;
170170
}
171171

172-
// 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.
172+
// 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.
173173
// 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.
174174
// More context here: https://github.com/Expensify/App/pull/59300
175175
if (!shouldUseNarrowLayout) {
176176
return;
177177
}
178178

179-
Navigation.setShouldPopAllStateOnUP(true);
179+
Navigation.setShouldPopToSidebar(true);
180180
}, [shouldUseNarrowLayout]);
181181

182182
useEffect(() => {

src/libs/Navigation/helpers/goBackFromWorkspaceCentralScreen.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/libs/actions/Report.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,8 +2906,7 @@ function deleteReport(reportID: string | undefined, shouldDeleteChildReports = f
29062906
function navigateToConciergeChatAndDeleteReport(reportID: string | undefined, shouldPopToTop = false, shouldDeleteChildReports = false) {
29072907
// Dismiss the current report screen and replace it with Concierge Chat
29082908
if (shouldPopToTop) {
2909-
Navigation.setShouldPopAllStateOnUP(true);
2910-
Navigation.goBack(undefined, {shouldPopToTop: true});
2909+
Navigation.popToSidebar();
29112910
} else {
29122911
Navigation.goBack();
29132912
}

src/libs/navigateAfterJoinRequest.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import ROUTES from '@src/ROUTES';
22
import Navigation from './Navigation/Navigation';
33

44
const navigateAfterJoinRequest = () => {
5-
Navigation.goBack(undefined, {shouldPopToTop: true});
5+
if (Navigation.getShouldPopToSidebar()) {
6+
Navigation.popToSidebar();
7+
} else {
8+
Navigation.goBack();
9+
}
610
Navigation.setNavigationActionToMicrotaskQueue(() => {
711
Navigation.navigate(ROUTES.SETTINGS_WORKSPACES.route);
812
});

src/pages/TeachersUnite/SaveTheWorldPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function SaveTheWorldPage() {
5757
title={translate('sidebarScreen.saveTheWorld')}
5858
shouldShowBackButton={shouldUseNarrowLayout}
5959
shouldDisplaySearchRouter
60-
onBackButtonPress={() => Navigation.goBack(undefined, {shouldPopToTop: true})}
60+
onBackButtonPress={Navigation.popToSidebar}
6161
icon={Illustrations.TeachersUnite}
6262
shouldUseHeadlineHeader
6363
/>

src/pages/home/ReportScreen.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,15 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
330330
Navigation.dismissModal();
331331
return;
332332
}
333+
if (Navigation.getShouldPopToSidebar()) {
334+
Navigation.popToSidebar();
335+
return;
336+
}
333337
if (backTo) {
334-
Navigation.goBack(backTo as Route, {shouldPopToTop: true});
338+
Navigation.goBack(backTo as Route);
335339
return;
336340
}
337-
Navigation.goBack(undefined, {shouldPopToTop: true});
341+
Navigation.goBack();
338342
}, [isInNarrowPaneModal, backTo]);
339343

340344
let headerView = (
@@ -589,9 +593,8 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
589593
}
590594
Navigation.dismissModal();
591595
if (Navigation.getTopmostReportId() === prevOnyxReportID) {
592-
Navigation.setShouldPopAllStateOnUP(true);
593596
Navigation.isNavigationReady().then(() => {
594-
Navigation.goBack(undefined, {shouldPopToTop: true});
597+
Navigation.popToSidebar();
595598
});
596599
}
597600
if (prevReport?.parentReportID) {

src/pages/settings/AboutPage/AboutPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function AboutPage() {
142142
title={translate('initialSettingsPage.about')}
143143
shouldShowBackButton={shouldUseNarrowLayout}
144144
shouldDisplaySearchRouter
145-
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS, {shouldPopToTop: true})}
145+
onBackButtonPress={Navigation.popToSidebar}
146146
icon={Illustrations.PalmTree}
147147
shouldUseHeadlineHeader
148148
/>

src/pages/settings/Preferences/PreferencesPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function PreferencesPage() {
5353
shouldUseHeadlineHeader
5454
shouldShowBackButton={shouldUseNarrowLayout}
5555
shouldDisplaySearchRouter
56-
onBackButtonPress={() => Navigation.goBack(undefined, {shouldPopToTop: true})}
56+
onBackButtonPress={Navigation.popToSidebar}
5757
/>
5858
<ScrollView contentContainerStyle={styles.pt3}>
5959
<View style={[styles.flex1, shouldUseNarrowLayout ? styles.workspaceSectionMobile : styles.workspaceSection]}>

src/pages/settings/Profile/ProfilePage.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ function ProfilePage() {
152152
>
153153
<HeaderWithBackButton
154154
title={translate('common.profile')}
155-
onBackButtonPress={() => Navigation.goBack(route.params?.backTo, {shouldPopToTop: true})}
155+
onBackButtonPress={() => {
156+
if (Navigation.getShouldPopToSidebar()) {
157+
Navigation.popToSidebar();
158+
return;
159+
}
160+
Navigation.goBack(route.params?.backTo);
161+
}}
156162
shouldShowBackButton={shouldUseNarrowLayout}
157163
shouldDisplaySearchRouter
158164
icon={Illustrations.Profile}

0 commit comments

Comments
 (0)