Skip to content

Commit

Permalink
Add redirectToReportBasedOnCurrentTab
Browse files Browse the repository at this point in the history
  • Loading branch information
WojtekBoman committed Nov 19, 2024
1 parent b2c1ec5 commit 0bef125
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
25 changes: 24 additions & 1 deletion src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {OnyxEntry} from 'react-native-onyx';
import type {Writable} from 'type-fest';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import Log from '@libs/Log';
import {isSplitNavigatorName} from '@libs/NavigationUtils';
import {isFullScreenName, isSplitNavigatorName} from '@libs/NavigationUtils';
import {shallowCompare} from '@libs/ObjectUtils';
import getPolicyEmployeeAccountIDs from '@libs/PolicyEmployeeListUtils';
import * as ReportConnection from '@libs/ReportConnection';
Expand Down Expand Up @@ -494,6 +494,28 @@ function removeScreenFromNavigationState(screen: Screen) {
});
}

/**
* The function redirects the user to the report screen, taking into account the currently open tab.
* When a redirect is performed from the Search tab, a report screen will be opened in RHP, otherwise the chat will be displayed in the Inbox tab.
*/
function redirectToReportBasedOnCurrentTab(reportID: string) {
const lastFullScreenRoute = navigationRef
.getRootState()
.routes.filter((route) => isFullScreenName(route.name))
.at(-1);

if (!lastFullScreenRoute) {
return;
}

if (lastFullScreenRoute.name === SCREENS.SEARCH.CENTRAL_PANE) {
navigate(ROUTES.SEARCH_REPORT.getRoute({reportID}), {forceReplace: true});
return;
}

dismissModal(reportID);
}

export default {
setShouldPopAllStateOnUP,
navigate,
Expand All @@ -519,6 +541,7 @@ export default {
navigateToReportWithPolicyCheck,
goUp,
removeScreenFromNavigationState,
redirectToReportBasedOnCurrentTab,
};

export {navigationRef};
25 changes: 14 additions & 11 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3680,10 +3680,13 @@ function requestMoney(
}
}

Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : activeReportID);
if (activeReportID) {
Report.notifyNewAction(activeReportID, payeeAccountID);
if (!activeReportID) {
Navigation.dismissModal();
return;
}

Navigation.redirectToReportBasedOnCurrentTab(activeReportID);
Report.notifyNewAction(activeReportID, payeeAccountID);
}

function sendInvoice(
Expand Down Expand Up @@ -3738,7 +3741,7 @@ function sendInvoice(
API.write(WRITE_COMMANDS.SEND_INVOICE, parameters, onyxData);

if (isSearchTopmostFullScreenRoute()) {
Navigation.dismissModal();
Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID: invoiceRoom.reportID}), {forceReplace: true});
} else {
Navigation.dismissModalWithReport(invoiceRoom);
}
Expand Down Expand Up @@ -3931,7 +3934,7 @@ function trackExpense(
API.write(WRITE_COMMANDS.TRACK_EXPENSE, parameters, onyxData);
}
}
Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : activeReportID);
Navigation.redirectToReportBasedOnCurrentTab(activeReportID);

Check failure on line 3937 in src/libs/actions/IOU.ts

View workflow job for this annotation

GitHub Actions / typecheck

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

if (action === CONST.IOU.ACTION.SHARE) {
Navigation.setNavigationActionToMicrotaskQueue(() => Navigation.navigate(ROUTES.ROOM_INVITE.getRoute(activeReportID ?? '-1', CONST.IOU.SHARE.ROLE.ACCOUNTANT)));
Expand Down Expand Up @@ -4502,7 +4505,7 @@ function splitBill({

API.write(WRITE_COMMANDS.SPLIT_BILL, parameters, onyxData);

Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : existingSplitChatReportID);
Navigation.redirectToReportBasedOnCurrentTab(existingSplitChatReportID);
Report.notifyNewAction(splitData.chatReportID, currentUserAccountID);
}

Expand Down Expand Up @@ -4569,7 +4572,7 @@ function splitBillAndOpenReport({

API.write(WRITE_COMMANDS.SPLIT_BILL_AND_OPEN_REPORT, parameters, onyxData);

Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : splitData.chatReportID);
Navigation.redirectToReportBasedOnCurrentTab(splitData.chatReportID);
Report.notifyNewAction(splitData.chatReportID, currentUserAccountID);
}

Expand Down Expand Up @@ -5138,7 +5141,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA
};

API.write(WRITE_COMMANDS.COMPLETE_SPLIT_BILL, parameters, {optimisticData, successData, failureData});
Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : chatReportID);
Navigation.redirectToReportBasedOnCurrentTab(chatReportID);
Report.notifyNewAction(chatReportID, sessionAccountID);
}

Expand Down Expand Up @@ -5316,7 +5319,7 @@ function createDistanceRequest(

API.write(WRITE_COMMANDS.CREATE_DISTANCE_REQUEST, parameters, onyxData);
const activeReportID = isMoneyRequestReport ? report?.reportID ?? '-1' : parameters.chatReportID;
Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : activeReportID);
Navigation.redirectToReportBasedOnCurrentTab(activeReportID);
Report.notifyNewAction(activeReportID, userAccountID);
}

Expand Down Expand Up @@ -6720,7 +6723,7 @@ function sendMoneyElsewhere(report: OnyxEntry<OnyxTypes.Report>, amount: number,

API.write(WRITE_COMMANDS.SEND_MONEY_ELSEWHERE, params, {optimisticData, successData, failureData});

Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : params.chatReportID);
Navigation.redirectToReportBasedOnCurrentTab(params.chatReportID);
Report.notifyNewAction(params.chatReportID, managerID);
}

Expand All @@ -6733,7 +6736,7 @@ function sendMoneyWithWallet(report: OnyxEntry<OnyxTypes.Report>, amount: number

API.write(WRITE_COMMANDS.SEND_MONEY_WITH_WALLET, params, {optimisticData, successData, failureData});

Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : params.chatReportID);
Navigation.redirectToReportBasedOnCurrentTab(params.chatReportID);
Report.notifyNewAction(params.chatReportID, managerID);
}

Expand Down
5 changes: 2 additions & 3 deletions src/pages/EditReportFieldPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import ScreenWrapper from '@components/ScreenWrapper';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import isSearchTopmostFullScreenRoute from '@libs/Navigation/isSearchTopmostFullScreenRoute';
import Navigation from '@libs/Navigation/Navigation';
import type {EditRequestNavigatorParamList} from '@libs/Navigation/types';
import * as ReportUtils from '@libs/ReportUtils';
Expand Down Expand Up @@ -69,14 +68,14 @@ function EditReportFieldPage({route}: EditReportFieldPageProps) {
goBack();
} else {
ReportActions.updateReportField(report.reportID, {...reportField, value: value === '' ? null : value}, reportField);
Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : report?.reportID);
Navigation.redirectToReportBasedOnCurrentTab(report?.reportID);
}
};

const handleReportFieldDelete = () => {
ReportActions.deleteReportField(report.reportID, reportField);
setIsDeleteModalVisible(false);
Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : report?.reportID);
Navigation.redirectToReportBasedOnCurrentTab(report?.reportID);
};

const fieldValue = isReportFieldTitle ? report.reportName ?? '' : reportField.value ?? reportField.defaultValue;
Expand Down

0 comments on commit 0bef125

Please sign in to comment.