diff --git a/src/components/Search.tsx b/src/components/Search.tsx index e4faa15bfb94..93792120fe99 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -137,7 +137,7 @@ function Search({query, policyIDs, sortBy, sortOrder}: SearchProps) { const ListItem = SearchUtils.getListItem(type); - const data = SearchUtils.getSections(searchResults?.data ?? {}, type); + const data = SearchUtils.getSections(searchResults?.data ?? {}, searchResults?.search ?? {}, type); const sortedData = SearchUtils.getSortedSections(type, data, sortBy, sortOrder); const onSortPress = (column: SearchColumnType, order: SortOrder) => { @@ -156,6 +156,7 @@ function Search({query, policyIDs, sortBy, sortOrder}: SearchProps) { customListHeader={ boolean; + shouldShow: (data: OnyxTypes.SearchResults['data'], metadata: OnyxTypes.SearchResults['search']) => boolean; }; const SearchColumns: SearchColumnConfig[] = [ @@ -53,17 +53,17 @@ const SearchColumns: SearchColumnConfig[] = [ { columnName: CONST.SEARCH_TABLE_COLUMNS.CATEGORY, translationKey: 'common.category', - shouldShow: () => true, + shouldShow: (data, metadata) => metadata?.columnsToShow.shouldShowCategoryColumn ?? false, }, { columnName: CONST.SEARCH_TABLE_COLUMNS.TAG, translationKey: 'common.tag', - shouldShow: () => true, + shouldShow: (data, metadata) => metadata?.columnsToShow.shouldShowTagColumn ?? false, }, { columnName: CONST.SEARCH_TABLE_COLUMNS.TAX_AMOUNT, translationKey: 'common.tax', - shouldShow: () => true, + shouldShow: (data, metadata) => metadata?.columnsToShow.shouldShowTaxColumn ?? false, isColumnSortable: false, }, { @@ -87,6 +87,7 @@ const SearchColumns: SearchColumnConfig[] = [ type SearchTableHeaderProps = { data: OnyxTypes.SearchResults['data']; + metadata: OnyxTypes.SearchResults['search']; sortBy?: SearchColumnType; sortOrder?: SortOrder; isSortingAllowed: boolean; @@ -94,7 +95,7 @@ type SearchTableHeaderProps = { shouldShowYear: boolean; }; -function SearchTableHeader({data, sortBy, sortOrder, isSortingAllowed, onSortPress, shouldShowYear}: SearchTableHeaderProps) { +function SearchTableHeader({data, metadata, sortBy, sortOrder, isSortingAllowed, onSortPress, shouldShowYear}: SearchTableHeaderProps) { const styles = useThemeStyles(); const StyleUtils = useStyleUtils(); const {isSmallScreenWidth, isMediumScreenWidth} = useWindowDimensions(); @@ -109,7 +110,7 @@ function SearchTableHeader({data, sortBy, sortOrder, isSortingAllowed, onSortPre {SearchColumns.map(({columnName, translationKey, shouldShow, isColumnSortable}) => { - if (!shouldShow(data)) { + if (!shouldShow(data, metadata)) { return null; } diff --git a/src/libs/SearchUtils.ts b/src/libs/SearchUtils.ts index 4be04ac48802..01b9a61ab73f 100644 --- a/src/libs/SearchUtils.ts +++ b/src/libs/SearchUtils.ts @@ -112,7 +112,7 @@ function shouldShowYear(data: TransactionListItemType[] | ReportListItemType[] | return false; } -function getTransactionsSections(data: OnyxTypes.SearchResults['data']): TransactionListItemType[] { +function getTransactionsSections(data: OnyxTypes.SearchResults['data'], metadata: OnyxTypes.SearchResults['search']): TransactionListItemType[] { const shouldShowMerchant = getShouldShowMerchant(data); const doesDataContainAPastYearTransaction = shouldShowYear(data); @@ -138,16 +138,16 @@ function getTransactionsSections(data: OnyxTypes.SearchResults['data']): Transac formattedMerchant, date, shouldShowMerchant, - shouldShowCategory: true, - shouldShowTag: true, - shouldShowTax: true, + shouldShowCategory: metadata?.columnsToShow.shouldShowCategoryColumn, + shouldShowTag: metadata?.columnsToShow.shouldShowTagColumn, + shouldShowTax: metadata?.columnsToShow.shouldShowTaxColumn, keyForList: transactionItem.transactionID, shouldShowYear: doesDataContainAPastYearTransaction, }; }); } -function getReportSections(data: OnyxTypes.SearchResults['data']): ReportListItemType[] { +function getReportSections(data: OnyxTypes.SearchResults['data'], metadata: OnyxTypes.SearchResults['search']): ReportListItemType[] { const shouldShowMerchant = getShouldShowMerchant(data); const doesDataContainAPastYearTransaction = shouldShowYear(data); @@ -185,9 +185,9 @@ function getReportSections(data: OnyxTypes.SearchResults['data']): ReportListIte formattedMerchant, date, shouldShowMerchant, - shouldShowCategory: true, - shouldShowTag: true, - shouldShowTax: true, + shouldShowCategory: metadata?.columnsToShow.shouldShowCategoryColumn, + shouldShowTag: metadata?.columnsToShow.shouldShowTagColumn, + shouldShowTax: metadata?.columnsToShow.shouldShowTaxColumn, keyForList: transactionItem.transactionID, shouldShowYear: doesDataContainAPastYearTransaction, }; @@ -220,8 +220,12 @@ function getListItem(type: K): SearchTypeTo return searchTypeToItemMap[type].listItem; } -function getSections(data: OnyxTypes.SearchResults['data'], type: K): ReturnType { - return searchTypeToItemMap[type].getSections(data) as ReturnType; +function getSections( + data: OnyxTypes.SearchResults['data'], + metadata: OnyxTypes.SearchResults['search'], + type: K, +): ReturnType { + return searchTypeToItemMap[type].getSections(data, metadata) as ReturnType; } function getSortedSections( diff --git a/src/types/onyx/SearchResults.ts b/src/types/onyx/SearchResults.ts index 52afaa44f8d2..676a1d3873d9 100644 --- a/src/types/onyx/SearchResults.ts +++ b/src/types/onyx/SearchResults.ts @@ -29,13 +29,25 @@ type SearchTypeToItemMap = { listItem: ListItemType; /** Returns search results sections based on search results data */ - getSections: (data: SearchResults['data']) => SectionsType; + getSections: (data: SearchResults['data'], metadata: SearchResults['search']) => SectionsType; /** Returns sorted search results sections based on search results data */ getSortedSections: (data: SectionsType, sortBy?: SearchColumnType, sortOrder?: SortOrder) => SectionsType; }; }; +/** Model of columns to show for search results */ +type ColumnsToShow = { + /** Whether the category column should be shown */ + shouldShowCategoryColumn: boolean; + + /** Whether the tag column should be shown */ + shouldShowTagColumn: boolean; + + /** Whether the tax column should be shown */ + shouldShowTaxColumn: boolean; +}; + /** Model of search result state */ type SearchResultsInfo = { /** Current search results offset/cursor */ @@ -49,6 +61,9 @@ type SearchResultsInfo = { /** Whether the search results are currently loading */ isLoading: boolean; + + /** The optional columns that should be shown according to policy settings */ + columnsToShow: ColumnsToShow; }; /** Model of personal details search result */