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 2 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 @@ -177,33 +177,6 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) {
const shouldShowLoadingState = !isOffline && !isDataLoaded;
const shouldShowLoadingMoreItems = !shouldShowLoadingState && searchResults?.search?.isLoading && searchResults?.search?.offset > 0;

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 @@ -217,7 +190,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 || SearchUtils.isSearchResultsEmpty(data);

if (shouldShowEmptyState) {
return (
Expand All @@ -232,6 +217,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 @@ -255,19 +267,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
13 changes: 8 additions & 5 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import ONYXKEYS from '@src/ONYXKEYS';
import type {SearchAdvancedFiltersForm} from '@src/types/form';
import INPUT_IDS from '@src/types/form/SearchAdvancedFiltersForm';
import type * as OnyxTypes from '@src/types/onyx';
import type SearchResults from '@src/types/onyx/SearchResults';
import type {SearchAccountDetails, SearchDataTypes, SearchPersonalDetails, SearchTransaction, SearchTypeToItemMap, SectionsType} from '@src/types/onyx/SearchResults';
import DateUtils from './DateUtils';
import getTopmostCentralPaneRoute from './Navigation/getTopmostCentralPaneRoute';
Expand Down Expand Up @@ -76,7 +75,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 +223,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 Expand Up @@ -318,8 +321,8 @@ function getCurrentSearchParams() {
return topmostCentralPaneRoute?.params as AuthScreensParamList['Search_Central_Pane'];
}

function isSearchResultsEmpty(searchResults: SearchResults) {
return !Object.keys(searchResults?.data).some((key) => key.startsWith(ONYXKEYS.COLLECTION.TRANSACTION));
function isSearchResultsEmpty<K extends keyof SearchTypeToItemMap>(data: SectionsType<K>) {
return data.length === 0;
}

function getQueryHashFromString(query: SearchQueryString): number {
Expand Down
Loading