Skip to content
Merged
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
40 changes: 31 additions & 9 deletions src/components/Search/SearchContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useCallback, useContext, useMemo, useState} from 'react';
import type {ReportActionListItemType, ReportListItemType, TransactionListItemType} from '@components/SelectionList/types';
import {isMoneyRequestReport} from '@libs/ReportUtils';
import {isReportListItemType} from '@libs/SearchUIUtils';
import {isReportListItemType, isTransactionListItemType} from '@libs/SearchUIUtils';
import CONST from '@src/CONST';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import type {SearchContext, SelectedTransactions} from './types';
Expand All @@ -23,14 +23,36 @@ const defaultSearchContext = {
const Context = React.createContext<SearchContext>(defaultSearchContext);

function getReportsFromSelectedTransactions(data: TransactionListItemType[] | ReportListItemType[] | ReportActionListItemType[], selectedTransactions: SelectedTransactions) {
return (data ?? [])
.filter(
(item): item is ReportListItemType =>
isReportListItemType(item) &&
isMoneyRequestReport(item) &&
item?.transactions?.every((transaction: {keyForList: string | number}) => selectedTransactions[transaction.keyForList]?.isSelected),
)
.map((item) => ({reportID: item.reportID, action: item.action ?? CONST.SEARCH.ACTION_TYPES.VIEW, total: item.total ?? CONST.DEFAULT_NUMBER_ID, policyID: item.policyID}));
if (data.length === 0) {
return [];
}

if (isReportListItemType(data[0]) || isMoneyRequestReport(data[0])) {
return data
.filter(
(item): item is ReportListItemType =>
isReportListItemType(item) && isMoneyRequestReport(item) && item.transactions?.every((transaction) => selectedTransactions[transaction.keyForList]?.isSelected),
)
.map((item) => ({
reportID: item.reportID,
action: item.action ?? CONST.SEARCH.ACTION_TYPES.VIEW,
total: item.total ?? CONST.DEFAULT_NUMBER_ID,
policyID: item.policyID,
}));
}

if (isTransactionListItemType(data[0])) {
return data
.filter((transaction) => transaction.keyForList != null && selectedTransactions[transaction.keyForList]?.isSelected)
.map((transaction) => ({
reportID: transaction.reportID,
action: 'action' in transaction ? transaction.action ?? CONST.SEARCH.ACTION_TYPES.VIEW : CONST.SEARCH.ACTION_TYPES.VIEW,
total: 'amount' in transaction ? transaction.amount ?? CONST.DEFAULT_NUMBER_ID : CONST.DEFAULT_NUMBER_ID,

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.

Coming from #79417, we should use modifiedAmount ?? amount instead of transaction.amount to ensure we retrieve the correct amount if it gets modified.

policyID: transaction.policyID,
}));
}

return [];
}

function SearchContextProvider({children}: ChildrenProps) {
Expand Down