Skip to content

Commit

Permalink
feat: first look on RHP navigating
Browse files Browse the repository at this point in the history
  • Loading branch information
WoLewicki committed May 24, 2023
1 parent 83b2525 commit d2b8e2d
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 23 deletions.
1 change: 0 additions & 1 deletion patches/@react-navigation+stack+6.3.16.patch
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ index 6bbce10..7f2eed3 100644
+ { route }: { route: Route<string> },
+ state: StackNavigationState<ParamListBase>
+ ) => {
+ console.log(state)
this.props.navigation.emit({
type: 'gestureStart',
target: route.key,
Expand Down
17 changes: 15 additions & 2 deletions src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'lodash';
import lodashGet from 'lodash/get';
import {CommonActions, getPathFromState, StackActions} from '@react-navigation/native';
import {getActionFromState} from '@react-navigation/core';
import Log from '../Log';
import DomUtils from '../DomUtils';
import linkTo from './linkTo';
Expand All @@ -10,6 +11,7 @@ import navigationRef from './navigationRef';
import NAVIGATORS from '../../NAVIGATORS';
import originalGetTopmostReportId from './getTopmostReportId';
import dismissKeyboardGoingBack from './dismissKeyboardGoingBack';
import getStateFromPath from './getStateFromPath';

let resolveNavigationIsReadyPromise;
const navigationIsReadyPromise = new Promise((resolve) => {
Expand Down Expand Up @@ -122,15 +124,26 @@ function setParams(params, routeKey) {

/**
* Dismisses the last modal stack if there is any
*
* @param {String | undefined} targetReportID - The reportID to navigate to after dismissing the modal
*/
function dismissModal() {
function dismissModal(targetReportID) {
if (!canNavigate('dismissModal')) {
return;
}
const rootState = navigationRef.getRootState();
const lastRoute = _.last(rootState.routes);
if (lastRoute.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR || lastRoute.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR) {
navigationRef.current.dispatch(StackActions.pop());
// if we are not in the target report, we need to navigate to it after dismissing the modal
if (targetReportID && targetReportID !== getTopmostReportId(rootState)) {
const state = getStateFromPath(ROUTES.getReportRoute(targetReportID));

const action = getActionFromState(state, linkingConfig.config);
action.type = 'REPLACE';
navigationRef.current.dispatch(action);
} else {
navigationRef.current.dispatch(StackActions.pop());
}
} else {
Log.hmmm('[Navigation] dismissModal failed because there is no modal stack to dismiss');
}
Expand Down
19 changes: 19 additions & 0 deletions src/libs/Navigation/getStateFromPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {getStateFromPath as RNGetStateFromPath} from '@react-navigation/native';
import linkingConfig from './linkingConfig';

/**
* @param {String} path - The path to parse
* @returns {Object | undefined} - It's possible that there is no navigation action for the given path
*/
function getStateFromPath(path) {
const normalizedPath = !path.startsWith('/') ? `/${path}` : path;

const state = linkingConfig.getStateFromPath ? linkingConfig.getStateFromPath(normalizedPath, linkingConfig.config) : RNGetStateFromPath(normalizedPath, linkingConfig.config);

if (!state) {
throw new Error('Failed to parse the path to a navigation state.');
}
return state;
}

export default getStateFromPath;
12 changes: 4 additions & 8 deletions src/libs/Navigation/linkTo.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import {getStateFromPath, getActionFromState} from '@react-navigation/core';
import {getActionFromState} from '@react-navigation/core';
import {Platform} from 'react-native';
import _ from 'lodash';
import NAVIGATORS from '../../NAVIGATORS';
import linkingConfig from './linkingConfig';
import getTopmostReportId from './getTopmostReportId';
import getStateFromPath from './getStateFromPath';

export default function linkTo(navigation, path, type) {
const normalizedPath = !path.startsWith('/') ? `/${path}` : path;
if (navigation === undefined) {
throw new Error("Couldn't find a navigation object. Is your component inside a screen in a navigator?");
}

const state = linkingConfig.getStateFromPath ? linkingConfig.getStateFromPath(normalizedPath, linkingConfig.config) : getStateFromPath(normalizedPath, linkingConfig.config);

if (!state) {
throw new Error('Failed to parse the path to a navigation state.');
}

let root = navigation;
let current;

Expand All @@ -26,6 +20,8 @@ export default function linkTo(navigation, path, type) {
root = current;
}

const state = getStateFromPath(path);

const action = getActionFromState(state, linkingConfig.config);

// If action type is different than NAVIGATE we can't change it to the PUSH safely
Expand Down
14 changes: 7 additions & 7 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ function requestMoney(report, amount, currency, payeeEmail, participant, comment
},
{optimisticData, successData, failureData},
);
Navigation.navigate(ROUTES.getReportRoute(chatReport.reportID));
Navigation.dismissModal(chatReport.reportID);
}

/**
Expand Down Expand Up @@ -691,7 +691,7 @@ function splitBillAndOpenReport(participants, currentUserLogin, amount, comment,
onyxData,
);

Navigation.navigate(ROUTES.getReportRoute(groupData.chatReportID));
Navigation.dismissModal(groupData.chatReportID);
}

/**
Expand Down Expand Up @@ -800,7 +800,7 @@ function deleteMoneyRequest(chatReportID, iouReportID, moneyRequestAction, shoul
);

if (shouldCloseOnDelete) {
Navigation.navigate(ROUTES.getReportRoute(iouReportID));
Navigation.dismissModal(iouReportID);
}
}

Expand Down Expand Up @@ -1144,7 +1144,7 @@ function sendMoneyElsewhere(report, amount, currency, comment, managerEmail, rec

API.write('SendMoneyElsewhere', params, {optimisticData, successData, failureData});

Navigation.navigate(ROUTES.getReportRoute(params.chatReportID));
Navigation.dismissModal(params.chatReportID);
}

/**
Expand All @@ -1160,7 +1160,7 @@ function sendMoneyWithWallet(report, amount, currency, comment, managerEmail, re

API.write('SendMoneyWithWallet', params, {optimisticData, successData, failureData});

Navigation.navigate(ROUTES.getReportRoute(params.chatReportID));
Navigation.dismissModal(params.chatReportID);
}

/**
Expand All @@ -1176,7 +1176,7 @@ function sendMoneyViaPaypal(report, amount, currency, comment, managerEmail, rec

API.write('SendMoneyViaPaypal', params, {optimisticData, successData, failureData});

Navigation.navigate(ROUTES.getReportRoute(params.chatReportID));
Navigation.dismissModal(params.chatReportID);

asyncOpenURL(Promise.resolve(), buildPayPalPaymentUrl(amount, recipient.payPalMeAddress, currency));
}
Expand All @@ -1195,7 +1195,7 @@ function payMoneyRequest(paymentType, chatReport, iouReport) {
const {params, optimisticData, successData, failureData} = getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentType);

API.write('PayMoneyRequest', params, {optimisticData, successData, failureData});
Navigation.navigate(ROUTES.getReportRoute(chatReport.reportID));
Navigation.dismissModal(chatReport.reportID);
if (paymentType === CONST.IOU.PAYMENT_TYPE.PAYPAL_ME) {
asyncOpenURL(Promise.resolve(), buildPayPalPaymentUrl(iouReport.total, recipient.payPalMeAddress, iouReport.currency));
}
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ function navigateToAndOpenReport(userLogins) {

// We want to pass newChat here because if anything is passed in that param (even an existing chat), we will try to create a chat on the server
openReport(reportID, newChat.participants, newChat);
Navigation.navigate(ROUTES.getReportRoute(reportID));
Navigation.dismissModal(reportID);
}

/**
Expand Down Expand Up @@ -1208,7 +1208,7 @@ function addPolicyReport(policy, reportName, visibility) {
},
{optimisticData, successData},
);
Navigation.navigate(ROUTES.getReportRoute(policyReport.reportID));
Navigation.dismissModal(policyReport.reportID);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/pages/tasks/TaskDescriptionPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function TaskDescriptionPage(props) {
title={props.translate('newTaskPage.task')}
shouldShowBackButton
onBackButtonPress={() => Navigation.goBack()}
onCloseButtonPress={() => Navigation.dismissModal(true)}
onCloseButtonPress={() => Navigation.dismissModal()}
/>
<Form
style={[styles.flexGrow1, styles.ph5]}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/tasks/TaskTitlePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function TaskTitlePage(props) {
title={props.translate('newTaskPage.task')}
shouldShowBackButton
onBackButtonPress={() => Navigation.goBack()}
onCloseButtonPress={() => Navigation.dismissModal(true)}
onCloseButtonPress={() => Navigation.dismissModal()}
/>
<Form
style={[styles.flexGrow1, styles.ph5]}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/wallet/WalletStatementPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class WalletStatementPage extends React.Component {
componentDidMount() {
const currentYearMonth = moment().format('YYYYMM');
if (!this.yearMonth || this.yearMonth.length !== 6 || this.yearMonth > currentYearMonth) {
Navigation.dismissModal(true);
Navigation.dismissModal();
}
}

Expand Down

0 comments on commit d2b8e2d

Please sign in to comment.