Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ const ONYXKEYS = {
/** Is unreported transactions loading */
IS_LOADING_UNREPORTED_TRANSACTIONS: 'isLoadingUnreportedTransactions',

/** List of transaction thread IDs used when navigating to prev/next transaction when viewing it in RHP */
TRANSACTION_THREAD_NAVIGATION_REPORT_IDS: 'transactionThreadNavigationReportIDs',

/** Timestamp of the last login on iOS */
NVP_LAST_ECASH_IOS_LOGIN: 'nvp_lastECashIOSLogin',
NVP_LAST_IPHONE_LOGIN: 'nvp_lastiPhoneLogin',
Expand Down Expand Up @@ -1190,6 +1193,7 @@ type OnyxValuesMapping = {
[ONYXKEYS.NVP_LAST_ECASH_ANDROID_LOGIN]: string;
[ONYXKEYS.NVP_LAST_IPHONE_LOGIN]: string;
[ONYXKEYS.NVP_LAST_ANDROID_LOGIN]: string;
[ONYXKEYS.TRANSACTION_THREAD_NAVIGATION_REPORT_IDS]: string[];
};

type OnyxDerivedValuesMapping = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useFocusEffect} from '@react-navigation/native';
import isEmpty from 'lodash/isEmpty';
import React, {memo, useCallback, useEffect, useMemo, useState} from 'react';
import React, {memo, useCallback, useMemo, useState} from 'react';
import {View} from 'react-native';
import Animated, {FadeIn, FadeOut} from 'react-native-reanimated';
import type {TupleToUnion} from 'type-fest';
Expand All @@ -23,11 +23,11 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import {turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import {setActiveTransactionThreadIDs} from '@libs/actions/TransactionThreadNavigation';
import ControlSelection from '@libs/ControlSelection';
import {convertToDisplayString} from '@libs/CurrencyUtils';
import {canUseTouchScreen} from '@libs/DeviceCapabilities';
import {getThreadReportIDsForTransactions} from '@libs/MoneyRequestReportUtils';
import {isFullScreenName} from '@libs/Navigation/helpers/isNavigatorName';
import {navigationRef} from '@libs/Navigation/Navigation';
import {getIOUActionForTransactionID} from '@libs/ReportActionsUtils';
import {getMoneyRequestSpendBreakdown, isIOUReport} from '@libs/ReportUtils';
Expand All @@ -39,11 +39,9 @@ import variables from '@styles/variables';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import type * as OnyxTypes from '@src/types/onyx';
import MoneyRequestReportTableHeader from './MoneyRequestReportTableHeader';
import SearchMoneyRequestReportEmptyState from './SearchMoneyRequestReportEmptyState';
import {setActiveTransactionThreadIDs} from './TransactionThreadReportIDRepository';

type MoneyRequestReportTransactionListProps = {
report: OnyxTypes.Report;
Expand Down Expand Up @@ -199,43 +197,15 @@ function MoneyRequestReportTransactionList({
const backTo = Navigation.getActiveRoute();

// Single transaction report will open in RHP, and we need to find every other report ID for the rest of transactions
// to display prev/next arrows in RHP for navigating between transactions
// to display prev/next arrows in RHP for navigation
const sortedSiblingTransactionReportIDs = getThreadReportIDsForTransactions(reportActions, sortedTransactions);
setActiveTransactionThreadIDs(sortedSiblingTransactionReportIDs);

Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID: reportIDToNavigate, backTo}));
setActiveTransactionThreadIDs(sortedSiblingTransactionReportIDs).then(() => {
Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID: reportIDToNavigate, backTo}));
});
},
[reportActions, sortedTransactions],
);

useEffect(() => {
const lastFullScreenRoute = navigationRef.getRootState()?.routes.findLast((route) => isFullScreenName(route.name));

// Only setActiveTransactionThreadIDs if current full screen report route is this report
if (lastFullScreenRoute?.name !== NAVIGATORS.REPORTS_SPLIT_NAVIGATOR && lastFullScreenRoute?.name !== NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR) {
return;
}

// Check params contain reportID
const lastRoute = lastFullScreenRoute?.state?.routes?.at(-1);
if (!lastRoute?.params || !('reportID' in lastRoute.params)) {
return;
}

// Check lastRoute is a report screen
if (lastRoute?.name !== SCREENS.SEARCH.MONEY_REQUEST_REPORT && lastRoute?.name !== SCREENS.REPORT) {
return;
}

// Check lastRoute params has reportID equal with this reportID
if (lastRoute.params.reportID !== report.reportID) {
return;
}

const sortedSiblingTransactionReportIDs = getThreadReportIDsForTransactions(reportActions, transactions);
setActiveTransactionThreadIDs(sortedSiblingTransactionReportIDs);
}, [report.reportID, reportActions, transactions]);

const {amountColumnSize, dateColumnSize, taxAmountColumnSize} = useMemo(() => {
const isAmountColumnWide = transactions.some((transaction) => isTransactionAmountTooLong(transaction));
const isTaxAmountColumnWide = transactions.some((transaction) => isTransactionTaxAmountTooLong(transaction));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import {findFocusedRoute} from '@react-navigation/native';
import React, {useEffect} from 'react';
import React, {useEffect, useMemo} from 'react';
import {useOnyx} from 'react-native-onyx';
import PrevNextButtons from '@components/PrevNextButtons';
import {clearActiveTransactionThreadIDs} from '@libs/actions/TransactionThreadNavigation';
import Navigation from '@navigation/Navigation';
import navigationRef from '@navigation/navigationRef';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import {clearActiveTransactionThreadIDs, getActiveTransactionThreadIDs} from './TransactionThreadReportIDRepository';

type MoneyRequestReportRHPNavigationButtonsProps = {
currentReportID: string;
};

function MoneyRequestReportTransactionsNavigation({currentReportID}: MoneyRequestReportRHPNavigationButtonsProps) {
const reportIDsList = getActiveTransactionThreadIDs();
const {prevReportID, nextReportID} = (() => {
if (!reportIDsList) {
const [reportIDsList = CONST.EMPTY_ARRAY] = useOnyx(ONYXKEYS.TRANSACTION_THREAD_NAVIGATION_REPORT_IDS, {
canBeMissing: true,
});

const {prevReportID, nextReportID} = useMemo(() => {
Comment thread
Kicu marked this conversation as resolved.
Resolved
if (!reportIDsList || reportIDsList.length < 2) {
return {prevReportID: undefined, nextReportID: undefined};
}

Expand All @@ -24,7 +30,7 @@ function MoneyRequestReportTransactionsNavigation({currentReportID}: MoneyReques
const nextID = currentReportIndex <= reportIDsList.length - 1 ? reportIDsList.at(currentReportIndex + 1) : undefined;

return {prevReportID: prevID, nextReportID: nextID};
})();
}, [currentReportID, reportIDsList]);

const backTo = Navigation.getActiveRoute();

Expand Down

This file was deleted.

16 changes: 9 additions & 7 deletions src/components/SelectionList/Search/ReportListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {getReportIDForTransaction} from '@libs/MoneyRequestReportUtils';
import Navigation from '@libs/Navigation/Navigation';
import {isTransactionAmountTooLong, isTransactionTaxAmountTooLong} from '@libs/SearchUIUtils';
import shouldShowTransactionYear from '@libs/TransactionUtils/shouldShowTransactionYear';
import variables from '@styles/variables';
import {setActiveTransactionThreadIDs} from '@userActions/TransactionThreadNavigation';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand Down Expand Up @@ -74,14 +76,14 @@ function ReportListItem<TItem extends ListItem>({
const openReportInRHP = (transactionItem: TransactionListItemType) => {
const backTo = Navigation.getActiveRoute();

const isFromSelfDM = transactionItem.reportID === CONST.REPORT.UNREPORTED_REPORT_ID;
const reportID = getReportIDForTransaction(transactionItem);
const siblingTransactionThreadIDs = reportItem.transactions.map(getReportIDForTransaction);

const reportID =
(!transactionItem.isFromOneTransactionReport || isFromSelfDM) && transactionItem.transactionThreadReportID !== CONST.REPORT.UNREPORTED_REPORT_ID
? transactionItem.transactionThreadReportID
: transactionItem.reportID;

Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID, backTo}));
// When opening the transaction thread in RHP we need to find every other ID for the rest of transactions
// to display prev/next arrows in RHP for navigation
setActiveTransactionThreadIDs(siblingTransactionThreadIDs).then(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, could you test with a report that will have 300 expenses for example. How long will this processing take before we navigate to the RHP? @Kicu @allroundexperts

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I can test. However I remember in the past asking about such cases in the context of other tasks, and the answer was that they won't happen.
Honestly I have no idea how the table view would behave if it got 300 transactions

Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID, backTo}));
});
};

if (!reportItem?.reportName && reportItem.transactions.length > 1) {
Expand Down
18 changes: 18 additions & 0 deletions src/libs/MoneyRequestReportUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import type {TransactionListItemType} from '@components/SelectionList/types';
import CONST from '@src/CONST';
import type {OriginalMessageIOU, Policy, Report, ReportAction, ReportMetadata, Transaction} from '@src/types/onyx';
import {convertToDisplayString} from './CurrencyUtils';
Expand All @@ -14,6 +15,7 @@ import {
isMoneyRequestReport,
isReportTransactionThread,
} from './ReportUtils';
import {isTransactionPendingDelete} from './TransactionUtils';

/**
* In MoneyRequestReport we filter out some IOU action types, because expense/transaction data is displayed in a separate list
Expand Down Expand Up @@ -45,12 +47,27 @@ function isActionVisibleOnMoneyRequestReport(action: ReportAction) {
function getThreadReportIDsForTransactions(reportActions: ReportAction[], transactions: Transaction[]) {
return transactions
.map((transaction) => {
if (isTransactionPendingDelete(transaction)) {
return;
}

const action = getIOUActionForTransactionID(reportActions, transaction.transactionID);
return action?.childReportID;
})
.filter((reportID): reportID is string => !!reportID);
}

/**
* Returns a correct reportID for a given TransactionListItemType for navigation/displaying purposes.
*/
function getReportIDForTransaction(transactionItem: TransactionListItemType) {
const isFromSelfDM = transactionItem.reportID === CONST.REPORT.UNREPORTED_REPORT_ID;

return (!transactionItem.isFromOneTransactionReport || isFromSelfDM) && transactionItem.transactionThreadReportID !== CONST.REPORT.UNREPORTED_REPORT_ID
? transactionItem.transactionThreadReportID
: transactionItem.reportID;
}

/**
* Filters all available transactions and returns the ones that belong to a specific report (by `reportID`).
* It is used as an onyx selector, to make sure that report related views do not process all transactions in onyx.
Expand Down Expand Up @@ -145,6 +162,7 @@ const getTotalAmountForIOUReportPreviewButton = (report: OnyxEntry<Report>, poli
export {
isActionVisibleOnMoneyRequestReport,
getThreadReportIDsForTransactions,
getReportIDForTransaction,
getTotalAmountForIOUReportPreviewButton,
selectAllTransactionsForReport,
isSingleTransactionReport,
Expand Down
20 changes: 20 additions & 0 deletions src/libs/actions/TransactionThreadNavigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Onyx from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';

/**
* When a single transaction report is displayed in RHP it may need extra context in case user navigated to it from MoneyRequestReportView or Reports
* This context is the list of "sibling" transaction report ids.
* These "siblings" are child report IDs of every transaction connected to the same parent Report that the original transaction is connected.
*
* We save this value in onyx, so that we can correctly display navigation UI in transaction thread RHP.
*/

function setActiveTransactionThreadIDs(ids: string[]) {
return Onyx.set(ONYXKEYS.TRANSACTION_THREAD_NAVIGATION_REPORT_IDS, ids);
}

function clearActiveTransactionThreadIDs() {
return Onyx.set(ONYXKEYS.TRANSACTION_THREAD_NAVIGATION_REPORT_IDS, null);
}

export {setActiveTransactionThreadIDs, clearActiveTransactionThreadIDs};
40 changes: 40 additions & 0 deletions tests/unit/MoneyRequestReportUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type {TransactionListItemType} from '@components/SelectionList/types';
import {getReportIDForTransaction} from '@libs/MoneyRequestReportUtils';
import CONST from '@src/CONST';

const transactionItemBaseMock = {
reportID: 'report123',
transactionThreadReportID: 'thread123',
} as TransactionListItemType;

describe('MoneyRequestReportUtils', () => {
describe('getReportIDForTransaction', () => {
it('returns transaction thread ID if its not from one transaction report', () => {
const transactionItem = {...transactionItemBaseMock};
const resultID = getReportIDForTransaction(transactionItem);

expect(resultID).toBe('thread123');
});

it('returns transaction thread ID if its from self DM', () => {
const transactionItem = {...transactionItemBaseMock, reportID: CONST.REPORT.UNREPORTED_REPORT_ID};
const resultID = getReportIDForTransaction(transactionItem);

expect(resultID).toBe('thread123');
});

it('returns expense reportID if its from one transaction report', () => {
const transactionItem = {...transactionItemBaseMock, isFromOneTransactionReport: true};
const resultID = getReportIDForTransaction(transactionItem);

expect(resultID).toBe('report123');
});

it('returns reportID if transaction thread ID is 0 - unreported', () => {
const transactionItem = {...transactionItemBaseMock, transactionThreadReportID: CONST.REPORT.UNREPORTED_REPORT_ID};
const resultID = getReportIDForTransaction(transactionItem);

expect(resultID).toBe('report123');
});
});
});