Skip to content

Commit

Permalink
Merge pull request #46867 from Expensify/cmartins-fixCrashMissingReport
Browse files Browse the repository at this point in the history
Fix crash for missing report
  • Loading branch information
carlosmiceli committed Aug 7, 2024
2 parents 6a6c242 + d8ce455 commit 0d3e172
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 43 deletions.
81 changes: 40 additions & 41 deletions src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,33 +188,6 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) {
turnOffMobileSelectionMode();
}, [isSearchResultsEmpty, prevIsSearchResultEmpty]);

const toggleTransaction = (item: TransactionListItemType | ReportListItemType) => {
if (SearchUtils.isTransactionListItemType(item)) {
if (!item.keyForList) {
return;
}

setSelectedTransactions(prepareTransactionsList(item, selectedTransactions));
return;
}

if (item.transactions.every((transaction) => selectedTransactions[transaction.keyForList]?.isSelected)) {
const reducedSelectedTransactions: SelectedTransactions = {...selectedTransactions};

item.transactions.forEach((transaction) => {
delete reducedSelectedTransactions[transaction.keyForList];
});

setSelectedTransactions(reducedSelectedTransactions);
return;
}

setSelectedTransactions({
...selectedTransactions,
...Object.fromEntries(item.transactions.map(mapTransactionItemToSelectedEntry)),
});
};

if (shouldShowLoadingState) {
return (
<>
Expand All @@ -228,7 +201,19 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) {
);
}

const shouldShowEmptyState = !isDataLoaded || SearchUtils.isSearchResultsEmpty(searchResults);
const type = SearchUtils.getSearchType(searchResults?.search);

if (searchResults === undefined || type === undefined) {
Log.alert('[Search] Undefined search type');
return null;
}

const ListItem = SearchUtils.getListItem(type);
const data = SearchUtils.getSections(searchResults.data, searchResults.search, type);
const sortedData = SearchUtils.getSortedSections(type, data, sortBy, sortOrder);
const sortedSelectedData = sortedData.map((item) => mapToItemWithSelectionInfo(item, selectedTransactions));

const shouldShowEmptyState = !isDataLoaded || data.length === 0;

if (shouldShowEmptyState) {
return (
Expand All @@ -243,6 +228,33 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) {
);
}

const toggleTransaction = (item: TransactionListItemType | ReportListItemType) => {
if (SearchUtils.isTransactionListItemType(item)) {
if (!item.keyForList) {
return;
}

setSelectedTransactions(prepareTransactionsList(item, selectedTransactions));
return;
}

if (item.transactions.every((transaction) => selectedTransactions[transaction.keyForList]?.isSelected)) {
const reducedSelectedTransactions: SelectedTransactions = {...selectedTransactions};

item.transactions.forEach((transaction) => {
delete reducedSelectedTransactions[transaction.keyForList];
});

setSelectedTransactions(reducedSelectedTransactions);
return;
}

setSelectedTransactions({
...selectedTransactions,
...Object.fromEntries(item.transactions.map(mapTransactionItemToSelectedEntry)),
});
};

const openReport = (item: TransactionListItemType | ReportListItemType) => {
let reportID = SearchUtils.isTransactionListItemType(item) ? item.transactionThreadReportID : item.reportID;

Expand All @@ -266,19 +278,6 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) {
setOffset(offset + CONST.SEARCH.RESULTS_PAGE_SIZE);
};

const type = SearchUtils.getSearchType(searchResults?.search);

if (type === undefined) {
Log.alert('[Search] Undefined search type');
return null;
}

const ListItem = SearchUtils.getListItem(type);

const data = SearchUtils.getSections(searchResults?.data ?? {}, searchResults?.search ?? {}, type);
const sortedData = SearchUtils.getSortedSections(type, data, sortBy, sortOrder);
const sortedSelectedData = sortedData.map((item) => mapToItemWithSelectionInfo(item, selectedTransactions));

const toggleAllTransactions = () => {
const areItemsOfReportType = searchResults?.search.type === CONST.SEARCH.DATA_TYPES.REPORT;
const flattenedItems = areItemsOfReportType ? (data as ReportListItemType[]).flatMap((item) => item.transactions) : data;
Expand Down
8 changes: 6 additions & 2 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ function isSearchDataType(type: string): type is SearchDataTypes {
return searchDataTypes.includes(type);
}

function getSearchType(search: OnyxTypes.SearchResults['search']): SearchDataTypes | undefined {
function getSearchType(search: OnyxTypes.SearchResults['search'] | undefined): SearchDataTypes | undefined {
if (!search) {
return undefined;
}

if (!isSearchDataType(search.type)) {
return undefined;
}
Expand Down Expand Up @@ -220,7 +224,7 @@ function getReportSections(data: OnyxTypes.SearchResults['data'], metadata: Onyx
};
if (reportIDToTransactions[reportKey]?.transactions) {
reportIDToTransactions[reportKey].transactions.push(transaction);
} else {
} else if (reportIDToTransactions[reportKey]) {
reportIDToTransactions[reportKey].transactions = [transaction];
}
}
Expand Down

0 comments on commit 0d3e172

Please sign in to comment.