Skip to content
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

Add redirectToReportBasedOnCurrentTab #139

Open
wants to merge 3 commits into
base: rhp/reports-in-rhp
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/components/PromotedActionsBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const PromotedActions = {
text: Localize.translateLocal('common.message'),
onSelected: () => {
if (reportID) {
Navigation.dismissModal(reportID);
Navigation.dismissModalWithReportID(reportID);
return;
}

Expand Down
71 changes: 38 additions & 33 deletions 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 All @@ -17,8 +17,8 @@ import NAVIGATORS from '@src/NAVIGATORS';
import ONYXKEYS from '@src/ONYXKEYS';
import type {HybridAppRoute, Route} from '@src/ROUTES';
import ROUTES, {HYBRID_APP_ROUTES} from '@src/ROUTES';
import SCREENS, {PROTECTED_SCREENS} from '@src/SCREENS';
import type {Screen} from '@src/SCREENS';
import SCREENS, {PROTECTED_SCREENS} from '@src/SCREENS';
import type {Report} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import originalCloseRHPFlow from './closeRHPFlow';
Expand Down Expand Up @@ -433,53 +433,57 @@ function switchPolicyID(policyID?: string) {

type NavigateToReportWithPolicyCheckPayload = {report?: OnyxEntry<Report>; reportID?: string; reportActionID?: string; referrer?: string; policyIDToCheck?: string};

function navigateToReportWithPolicyCheck({report, reportID, reportActionID, referrer, policyIDToCheck}: NavigateToReportWithPolicyCheckPayload, ref = navigationRef) {
function navigateToReportWithPolicyCheck(
{report, reportID, reportActionID, referrer, policyIDToCheck}: NavigateToReportWithPolicyCheckPayload,
options: LinkToOptions = {forceReplace: false, reportPathConversionEnabled: true},
ref = navigationRef,
) {
const targetReport = reportID ? {reportID, ...ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]} : report;
const policyID = policyIDToCheck ?? getPolicyIDFromState(navigationRef.getRootState() as State<RootStackParamList>);
const policyMemberAccountIDs = getPolicyEmployeeAccountIDs(policyID);
const shouldOpenAllWorkspace = isEmptyObject(targetReport) ? true : !ReportUtils.doesReportBelongToWorkspace(targetReport, policyMemberAccountIDs, policyID);

if ((shouldOpenAllWorkspace && !policyID) || !shouldOpenAllWorkspace) {
linkTo(ref.current, ROUTES.REPORT_WITH_ID.getRoute(targetReport?.reportID ?? '-1', reportActionID, referrer));
if (shouldOpenAllWorkspace || !policyID) {
linkTo(ref.current, ROUTES.REPORT_WITH_ID.getRoute(targetReport?.reportID ?? '-1', reportActionID, referrer), options);
return;
}

const params: Record<string, string> = {
reportID: targetReport?.reportID ?? '-1',
};
linkTo(ref.current, `w/${policyID}/${ROUTES.REPORT_WITH_ID.getRoute(targetReport?.reportID ?? '-1', reportActionID, referrer)}` as Route, options);
}

if (reportActionID) {
params.reportActionID = reportActionID;
}
const dismissModal = (ref = navigationRef) => {
ref.dispatch({type: CONST.NAVIGATION.ACTION_TYPE.DISMISS_MODAL as string});
};

if (referrer) {
params.referrer = referrer;
}
type DismissModalWithReportOptions = {shouldRedirectToRhpFromSearch: boolean};

ref.dispatch(
StackActions.push(NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, {
policyID: null,
screen: SCREENS.REPORT,
params,
}),
);
}
const dismissModalWithReportData = (
reportData: Pick<NavigateToReportWithPolicyCheckPayload, 'report' | 'reportID'>,
options: DismissModalWithReportOptions = {shouldRedirectToRhpFromSearch: false},
) => {
const {shouldRedirectToRhpFromSearch} = options;
const reportID = reportData?.report?.reportID ?? reportData?.reportID ?? '-1';
const topmostFullScreenRoute = navigationRef.getRootState().routes.findLast((route) => isFullScreenName(route.name));

// @TODO In places where we use dismissModal with report arg we should do dismiss modal and then navigate to the report.
// @TODO There should be a way to not use as string.
// We left it here to limit the number of changed files.
const dismissModal = (reportID?: string, ref = navigationRef) => {
ref.dispatch({type: CONST.NAVIGATION.ACTION_TYPE.DISMISS_MODAL as string});
if (!reportID) {
// On SearchPage we don't want to switch policyID to global as a side effect of the action performed from RHP
if (topmostFullScreenRoute?.name === SCREENS.SEARCH.CENTRAL_PANE && shouldRedirectToRhpFromSearch) {
navigate(ROUTES.SEARCH_REPORT.getRoute({reportID}), {forceReplace: true, reportPathConversionEnabled: false});
return;
}
isNavigationReady().then(() => navigateToReportWithPolicyCheck({reportID}));
};
const dismissModalWithReport = (report: OnyxEntry<Report>) => {
dismissModal();
isNavigationReady().then(() => navigateToReportWithPolicyCheck({report}));

// If the target chat is opened under the RHP, it's not necessary to perform a replace action
if (getTopmostReportId() === reportID && navigationRef.getRootState().routes.at(-2)?.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR) {
dismissModal();
return;
}

navigateToReportWithPolicyCheck(reportData, {forceReplace: true, reportPathConversionEnabled: false});
};

const dismissModalWithReport = (report: OnyxEntry<Report>, options: DismissModalWithReportOptions = {shouldRedirectToRhpFromSearch: false}) => dismissModalWithReportData({report}, options);

const dismissModalWithReportID = (reportID: string, options: DismissModalWithReportOptions = {shouldRedirectToRhpFromSearch: false}) => dismissModalWithReportData({reportID}, options);

function removeScreenFromNavigationState(screen: Screen) {
isNavigationReady().then(() => {
navigationRef.dispatch((state) => {
Expand All @@ -500,6 +504,7 @@ export default {
setParams,
dismissModal,
dismissModalWithReport,
dismissModalWithReportID,
isActiveRoute,
getActiveRoute,
getActiveRouteWithoutParams,
Expand Down
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.dismissModalWithReportID(activeReportID, {shouldRedirectToRhpFromSearch: true});
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.dismissModalWithReportID(activeReportID ?? '-1', {shouldRedirectToRhpFromSearch: true});

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.dismissModalWithReportID(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.dismissModalWithReportID(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.dismissModalWithReportID(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.dismissModalWithReportID(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.dismissModalWithReportID(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.dismissModalWithReportID(params.chatReportID);
Report.notifyNewAction(params.chatReportID, managerID);
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,7 @@ function navigateToConciergeChat(shouldDismissModal = false, checkIfCurrentPageA
navigateToAndOpenReport([CONST.EMAIL.CONCIERGE], shouldDismissModal);
});
} else if (shouldDismissModal) {
Navigation.dismissModal(conciergeChatReportID);
Navigation.dismissModalWithReportID(conciergeChatReportID);
} else {
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(conciergeChatReportID), linkToOptions);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ function createTaskAndNavigate(

if (!isCreatedUsingMarkdown) {
clearOutTaskInfo();
Navigation.dismissModal(parentReportID);
Navigation.dismissModalWithReportID(parentReportID);
}
Report.notifyNewAction(parentReportID, currentUserAccountID);
}
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/TeachersUnite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function referTeachersUniteVolunteer(partnerUserID: string, firstName: string, l
};

API.write(WRITE_COMMANDS.REFER_TEACHERS_UNITE_VOLUNTEER, parameters, {optimisticData});
Navigation.dismissModal(publicRoomReportID);
Navigation.dismissModalWithReportID(publicRoomReportID);
}

/**
Expand Down Expand Up @@ -179,7 +179,7 @@ function addSchoolPrincipal(firstName: string, partnerUserID: string, lastName:
};

API.write(WRITE_COMMANDS.ADD_SCHOOL_PRINCIPAL, parameters, {optimisticData, successData, failureData});
Navigation.dismissModal(expenseChatReportID);
Navigation.dismissModalWithReportID(expenseChatReportID);
}

export default {referTeachersUniteVolunteer, addSchoolPrincipal};
2 changes: 1 addition & 1 deletion src/pages/AddPersonalBankAccountPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function AddPersonalBankAccountPage() {
const onSuccessFallbackRoute = personalBankAccount?.onSuccessFallbackRoute ?? '';

if (exitReportID) {
Navigation.dismissModal(exitReportID);
Navigation.dismissModalWithReportID(exitReportID);
} else if (shouldContinue && onSuccessFallbackRoute) {
PaymentMethods.continueSetup(onSuccessFallbackRoute);
} else {
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.dismissModalWithReportID(report?.reportID);
}
};

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

const fieldValue = isReportFieldTitle ? report.reportName ?? '' : reportField.value ?? reportField.defaultValue;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/EnablePayments/AddBankAccount/AddBankAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function AddBankAccount() {
const onSuccessFallbackRoute = personalBankAccount?.onSuccessFallbackRoute ?? '';

if (exitReportID) {
Navigation.dismissModal(exitReportID);
Navigation.dismissModalWithReportID(exitReportID);
return;
}
if (shouldContinue && onSuccessFallbackRoute) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/NewChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function NewChatPage() {
const createChat = useCallback(
(option?: OptionsListUtils.Option) => {
if (option?.isSelfDM) {
Navigation.dismissModal(option.reportID);
Navigation.dismissModalWithReportID(option.reportID ?? '-1');
return;
}
let login = '';
Expand Down
2 changes: 1 addition & 1 deletion src/pages/RoomInvitePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function RoomInvitePage({

const goBack = useCallback(() => {
if (role === CONST.IOU.SHARE.ROLE.ACCOUNTANT) {
Navigation.dismissModal(reportID);
Navigation.dismissModalWithReportID(reportID);
return;
}
Navigation.goBack(backRoute);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/TransactionDuplicate/Confirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function Confirmation() {

const resolveDuplicates = useCallback(() => {
IOU.resolveDuplicates(transactionsMergeParams);
Navigation.dismissModal(reportAction?.childReportID ?? '-1');
Navigation.dismissModalWithReportID(reportAction?.childReportID ?? '-1');
}, [transactionsMergeParams, reportAction?.childReportID]);

const contextValue = useMemo(
Expand Down
5 changes: 2 additions & 3 deletions src/pages/TransactionReceiptPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ function TransactionReceipt({route}: TransactionReceiptProps) {
if (secondToLastRoute?.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR) {
Navigation.dismissModal();
} else {
Navigation.dismissModal(
ReportUtils.isOneTransactionThread(report?.reportID ?? '-1', report?.parentReportID ?? '-1', parentReportAction) ? report?.parentReportID : report?.reportID,
);
const reportID = ReportUtils.isOneTransactionThread(report?.reportID ?? '-1', report?.parentReportID ?? '-1', parentReportAction) ? report?.parentReportID : report?.reportID;
Navigation.dismissModalWithReportID(reportID ?? '-1');
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/pages/tasks/TaskAssigneeSelectorModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function TaskAssigneeSelectorModal() {
const reportOnyx = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${route.params?.reportID}`];
if (reportOnyx && !ReportUtils.isTaskReport(reportOnyx)) {
Navigation.isNavigationReady().then(() => {
Navigation.dismissModal(reportOnyx.reportID);
Navigation.dismissModalWithReportID(reportOnyx.reportID);
});
}
return reports?.[`${ONYXKEYS.COLLECTION.REPORT}${route.params?.reportID}`];
Expand Down Expand Up @@ -179,7 +179,7 @@ function TaskAssigneeSelectorModal() {
TaskActions.editTaskAssignee(report, session?.accountID ?? -1, option?.login ?? '', option?.accountID, assigneeChatReport);
}
InteractionManager.runAfterInteractions(() => {
Navigation.dismissModal(report.reportID);
Navigation.dismissModalWithReportID(report.reportID);
});
// If there's no report, we're creating a new task
} else if (option.accountID) {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/tasks/TaskDescriptionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ function TaskDescriptionPage({report, currentUserPersonalDetails}: TaskDescripti
Task.editTask(report, {description: values.description});
}

Navigation.dismissModal(report?.reportID);
Navigation.dismissModalWithReportID(report?.reportID);
},
[report],
);

if (!ReportUtils.isTaskReport(report)) {
Navigation.isNavigationReady().then(() => {
Navigation.dismissModal(report?.reportID);
Navigation.dismissModalWithReportID(report?.reportID);
});
}
const inputRef = useRef<AnimatedTextInputRef | null>(null);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/tasks/TaskTitlePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ function TaskTitlePage({report, currentUserPersonalDetails}: TaskTitlePageProps)
Task.editTask(report, {title: values.title});
}

Navigation.dismissModal(report?.reportID);
Navigation.dismissModalWithReportID(report?.reportID);
},
[report],
);

if (!ReportUtils.isTaskReport(report)) {
Navigation.isNavigationReady().then(() => {
Navigation.dismissModal(report?.reportID);
Navigation.dismissModalWithReportID(report?.reportID);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/WorkspaceNewRoomPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function WorkspaceNewRoomPage() {
if (!(((wasLoading && !isLoading) || (isOffline && isLoading)) && isEmptyObject(errorFields))) {
return;
}
Navigation.dismissModal(newRoomReportID);
Navigation.dismissModalWithReportID(newRoomReportID ?? '-1');
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps -- we just want this to update on changing the form State
}, [isLoading, errorFields]);

Expand Down
Loading