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

Fix crash for missing report #46867

Merged
merged 3 commits into from
Aug 7, 2024
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
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
Loading